1

I want to read the last 2 lines in some files, and if the content of second last line matches a specific string, then delete the last line only.

Also, after the above operation, 2 lines of data have to be appended to the end of the modified file. I saw other questions on SO that deal with different parts of my problem, but is there an easy way of doing all of the above with minimal code, preferably in a single function? (I can combine the different functions available at SO but that would be messy...)

Arvind
  • 6,404
  • 20
  • 94
  • 143
  • How big are the files, assuming they're text and not binary? – lobster1234 Oct 07 '11 at 07:10
  • Perhaps this [answer](http://stackoverflow.com/questions/686231/java-quickly-read-the-last-line-of-a-text-file/7322581#7322581) could help you out. – Emil Oct 07 '11 at 08:31

3 Answers3

3

I would recommend you to do it "in memory". It's easy to read line by line into a List, check the last lines and update the lines and write it back to the file.


Example:

public static void main(String[] args) throws IOException {

    String fileName = "test.txt";

    List<String> lines = new ArrayList<String>();

    // read the file into lines
    BufferedReader r = new BufferedReader(new FileReader(fileName));
    String in;
    while ((in = r.readLine()) != null)
        lines.add(in);
    r.close();

    // check your condition
    String secondFromBottom = lines.get(lines.size() - 2);
    if (secondFromBottom.matches("Hello World!")) {
        lines.remove(lines.size() - 1);
        lines.add("My fixed string");
    }  

    // write it back
    PrintWriter w = new PrintWriter(new FileWriter(fileName));
    for (String line : lines)
        w.println(line);
    w.close();
}

Note: No exception handling is done in the example above... you need to handle cases where the file for example doesn't contain two lines and other problems!

dacwe
  • 43,066
  • 12
  • 116
  • 140
  • 1
    If you have to deal with large files you'd better not store all lines in memory. Since only the last two lines are relevant you can write the previous lines to the output already while reading the input. – raymi Oct 07 '11 at 08:01
  • @raymi: This is *"the simple solution"* and the way I would start to do it! If the files are that big maybe the question would say something about it.. – dacwe Oct 07 '11 at 08:14
  • @dacwe- Thank you, the files I am parsing contain max 5000 lines each, and your solution does what I want to do with those files... – Arvind Oct 07 '11 at 12:21
0

As Gandalf said you can:

  • take RandomAccessFile,
  • use method seek(long) to jump forward and read those lines. But you won't know exactly how big the jump should be.
  • to delete last lines you need the position of begin of last line so before reading each line store their file pointer position (method getFilePointer()). Deleting to that position you use setLength(long).

My example of reading and deleting last lines you have here: Deleting the last line of a file with Java

Useful can be also: Quickly read the last line of a text file?

Community
  • 1
  • 1
Waldemar Wosiński
  • 1,490
  • 17
  • 28
0

If you have really big files and perfomance is an issue the way to go is to use a RandomAccessFile and read backwards looking for the line termination bytes to determine where the last two lines begin. Otherwise use dacwe's approach.

Gandalf
  • 2,350
  • 20
  • 28