0

How to read file from end to the beginning my code,

  try                
   {
      String strpath="/var/date.log";
      FileReader fr = new FileReader(strpath);
      BufferedReader br = new BufferedReader(fr);
      String ch;
      String[] Arr;
      do 
      {
        ch = br.readLine();
        if (ch != null)
        out.print(ch+"<br/>");   
      } 
      while (ch != null);
      fr.close();
    }
    catch(IOException e){
    out.print(e.getMessage());
  }
Kai
  • 38,985
  • 14
  • 88
  • 103
Salman Raza
  • 1,635
  • 6
  • 18
  • 18
  • there isn't any error in it i wanna read file from end to the start this my code for just derading and printing file – Salman Raza Dec 28 '11 at 09:57
  • how about just reading the file and reversing it when you're done? – Matten Dec 28 '11 at 09:59
  • That shouldn't be a big problem. But if you don't know how to do it, just stick to the `RandomAccessFile`-class as proposed by Sergey Grinev and Nrj . Sounds like the right approach – Matten Dec 28 '11 at 10:04
  • See also: [Java: Quickly read the last line of a text file?](http://stackoverflow.com/questions/686231), [Java : Read last n lines of a HUGE file](http://stackoverflow.com/questions/4121678) – hippietrail Nov 05 '12 at 18:23
  • This question seems to have been duplicated. Its duplicate received far more answers, and some very useful ones at that: [How to read file from end to start (in reverse order) in Java?](https://stackoverflow.com/questions/8664705/how-to-read-file-from-end-to-start-in-reverse-order-in-java) – Daniel Werner Feb 16 '23 at 13:09

3 Answers3

3

You can use RandomAccessFile class. Either just read in loop from file length to 0, or use some convenience 3rd party wraps like http://mattfleming.com/node/11

Sergey Grinev
  • 34,078
  • 10
  • 128
  • 141
2

If you need print lines in reverse order:

  1. Read all lines to list
  2. Reverse them
  3. Write them back

Code:

List<String> lines = new ArrayList<String>();
String curLine;
while ( (curLine= br.readLine()) != null) {
  lines.add(curLine);
}
Collections.reverse(lines);
for (String line : lines) {
  System.out.println(line);
}
Mikita Belahlazau
  • 15,326
  • 2
  • 38
  • 43
1

If you don't want to use temporary data(for reversing the file) you should use RandomAccessFile class.

In other case you can read and store the whole file in memory, then reversing it contents.

List<String> data = new LinkedList<String>();

If you need lines in reverse order, insted of:

out.print(ch+"<br/>");

do

data.add(ch);

And after reading the whole file you can use

Collections.reverse(data);

If you need every symbol to be in reverse order, you can use type Character instead of String and read not the whole line but only one symbol. After that simply reverse your data.

P.S. To print (to system output stream for example) you should iterate over each item in collection.

for (String line : data) {
   out.println(line);
}

If you use just out.print(data) this will call data.toString() method and print out its result. Standart implementation of toString() will not work as you expected. It will return something like object type and number.

Yegoshin Maxim
  • 872
  • 2
  • 21
  • 54