Getting a specific line in a file?

Discussion in 'Plugin Development' started by Skionz, Nov 16, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Skionz

    Is there any way to get a specific line saved in a file without iterating through each line?
    Here is what I am currently doing
    Code:
    String toGet = "Line to get";
    BufferedReader reader = new BufferedReader(new FileReader(this.path));
    for (String line; (line = reader.readLine()) != null;) {
        if(line.equals(toGet) {
            //The line equals toGet
        }
    }
     
  2. Offline

    Konato_K

    Not as far I know, you'll need to iterate through all of them.
     
  3. Offline

    Skionz

    Konato_K Damn it gets fairly slow at like 100 lines. Thanks though.
     
  4. Offline

    Konato_K

    Skionz Try to use a different method of storage then
     
  5. Offline

    Skionz

    Konato_K Yea I would use a database in a different situation but I'm writing an API for file storage so I am just trying to make it more efficient.
     
  6. Offline

    Konato_K

    Skionz A more "efficient" way (in terms of speed) it's to use one file for whatever you're trying to do (let's say you want to store data of players, so you create a file for each), but this will cause a high disk usage.

    Also if you know git, the object files are stored in folders named with the first two letters of it, not sure if you get what I mean
     
    Skionz likes this.
  7. Offline

    RawCode

    More efficient way to make each line to have fixed size, like 128 or 666 bytes.
    In case of 100th line you simply read file with offset of 128*100 with length of 128.

    Normally i would suggest to read zip or FAT32 format specifications, but it's unlikely for you to implement allocation table and payload.
     
Thread Status:
Not open for further replies.

Share This Page