Java reading nth line

我是研究僧i 提交于 2020-01-02 04:08:08

问题


I am trying to read a specific line from a text file, however I don't want to load the file into memory (it can get really big).

I have been looking but every example i have found requires either to read every line (this would slow my code down as there are over 100,000 lines) or load the whole thing into an array and get the correct element (file will have alot of lines to input).

An example of what I want to do:

String line = File.getLine(5);

"code is not actual code, it is made up to show the principle of what i want"

Is there a way to do this?

-----Edit-----

I have just realized this file will be written too in between reading lines (adding to the end of the file).


回答1:


Is there a way to do this?

Not unless the lines are of a fixed number of bytes each, no.

You don't have to actually keep each line in memory - but you've got to read through the whole file to get to the line you want, as otherwise you won't know where to start reading.




回答2:


You have to read the file line by line. Otherwise how do you know when you have gotten to line 5 (as in your example)?

Edit:

You might also want to check out Random Access Files which could be helpful if you know how many bytes per line, as Jon Skeet has said.




回答3:


The easiest way to do this would be to use a BufferedReader (http://docs.oracle.com/javase/1.5.0/docs/api/java/io/BufferedReader.html), because you can specify your buffer size. You could do something like:

BufferedReader in = new BufferedReader(new FileReader("foo.in"), 1024);

in.readLine();
in.readLine();
in.readLine();
in.readLine();
String line = in.readLine();



回答4:


1) read a line which the user selects,

If you only need to read a user-selected line once or infrequently (or if the file is small enough), then you just have to read the file line-by-line from the start, until you get to the selected line.

If, on the other hand you need to read a user-selected line frequently, you should build an index of line numbers and offsets. So, for example, line 42 corresponds to an offset of 2347 bytes into the file. Ideally, then, you would only read the entire file once and store the index--for example, in a map, using the line numbers as keys and offsets as values.

2) read new lines added since the last read. i plan to read the file every 10 seconds. i have got the line count and can find out the new line numbers but i need to read that line

For the second point, you can simply save the current offset into the file instead of saving the current line number--but it certainly wouldn't hurt to continue building the index if it will continue to provide a significant performance benefit.

  1. Use RandomAccessFile.seek(long offset) to set the file pointer to the most recently saved offset (confirm the file is longer than the most recently saved offset first--if not, nothing new has been appended).
  2. Use RandomAccessFile.readLine() to read a line of the file
  3. Call RandomAccessFile.getFilePointer() to get the current offset after reading the line and optionally put(currLineNo+1, offset) into the index.
  4. Repeat steps 2-3 until reaching the end of the file.

But don't get too carried away with performance optimizations unless the performance is already a problem or is highly likely to be a problem.




回答5:


The only way to do this is to build an index of where each line is (you only need to record the end of each line) Without a way to randomly access a line based on an index from the start, you have to read every byte before that line.

BTW: Reading 100,000 lines might take only one second on a fast machine.




回答6:


If performance is a big concern here and you are frequently reading random lines from a static file then you can optimize this a bit by reading through the file and building an index (basically just a long[]) of the starting offset of each line of the file.

Once you have this you know exactly where to jump to in the file and then you can read up to the next newline character to retrieve the full line.




回答7:


Here is a snippet of some code I had which will read a file and write every 10th line including the first line to a new file (writer.) You can always replace the try section with whatever you want to do. To change the number of lines to read just change the 0 in the if statement "lc.endsWith("0")" to whatever line you want to read. But if the file is being written to as you read it, this code will only work with the data that is contained inside the file when you run this code.

            LineNumberReader  lnr = new LineNumberReader(new FileReader(new File(file)));
            lnr.skip(Long.MAX_VALUE);
            int linecount=lnr.getLineNumber();
            lnr.close();

        for (int i=0; i<=linecount; i++){

            //read lines
            String line = bufferedReader.readLine();
            String lc = String.valueOf(i);

            if (lc.endsWith("0")){

                try{

                    writer.append(line+"\n");
                    writer.flush();

                    }catch(Exception ee){
                }
            }
        }



回答8:


For small files:

String line = Files.readAllLines(Paths.get("file.txt")).get(n);

For large files:

String line;
try (Stream<String> lines = Files.lines(Paths.get("file.txt"))) {
    line = lines.skip(n).findFirst().get();
}

Java 7:

String line;
try (BufferedReader br = new BufferedReader(new FileReader("file.txt"))) {
    for (int i = 0; i < n; i++)
        br.readLine();
    line = br.readLine();
}

Source: Reading nth line from file



来源:https://stackoverflow.com/questions/16201045/java-reading-nth-line

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!