0
    FileInputStream fstream = new FileInputStream("\\file path");
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
     while (br.ready()) {

                                line = br.readLine();
    }

Please let me know how to read a file from the last line to first provided the row number is not fixed and is varying with time? I know the above is useful for reading it from first row...

user2017714
  • 21
  • 1
  • 4
  • 1
    First of all, we do not know what br is, please insert the line where you declared br. – Loic O. Jan 28 '13 at 10:22
  • @magnus A good guess would probably be that br is a BufferedReader wrapping a FileReader. – Alderath Jan 28 '13 at 10:23
  • BufferedReader br = new BufferedReader(new InputStreamReader(in)); – user2017714 Jan 28 '13 at 10:23
  • 2
    Why not just read the file from first line to last line, save every line in an collection (List for example) and reverse that list. Once you have read the whole file into a suitable data structure you can do what ever you want. Only drawback to that approach is that handling large text files (a few 100 MB) might use to much resources. – Jens Jan 28 '13 at 10:23
  • @jens: Yes. you are correct if the number of rows is fixed and of less in size then your option fits for it........ But in my case the file is changing its data on real time and i want to read it from the last line to a particular time stamped token to avoid unnecessary iterations – user2017714 Jan 28 '13 at 10:25
  • @Alderath: I know ;) but the question isn't well formulated, giving more information would be a first step to getting an answer :) – Loic O. Jan 28 '13 at 10:25
  • @magnus: i just updated my code. Any how this will work for first to last row reading..... – user2017714 Jan 28 '13 at 10:29
  • Reverse file access is tough because there are no util libraries designed for the task, so you have to go very low level. What are you trying to actually achieve, are you monitoring for particular patterns that you need to know about or post logging analysis? You may fnd it easier to rethink your problem by implementing your own logger that can perform analysis of the messages as they're logged rather than after the event. – codeghost Jan 28 '13 at 10:55
  • Please don't use DataInputStream to read text http://vanillajava.blogspot.co.uk/2012/08/java-memes-which-refuse-to-die.html – Peter Lawrey Jan 30 '13 at 20:34

7 Answers7

0

read the file into a list, and process that list backwards... files and streams are usually designed to work forward; so doing this directly with streams might turn out a lite awkward. Only advised when the files are really huge...

FrankB
  • 349
  • 4
  • 10
  • :if it is a normal file with limited or few no of line... ya i agree with your answer. But my file normally having 64000 or even more lines ... If i take this much of data in to list and processing ... it will kill the performance of application and will take at least of 3-5 mins of time to do it – user2017714 Jan 28 '13 at 10:33
0

You cannot read a Buffer backwards, you can however count the lines of your buffer as explained in the link below

http://www.java2s.com/Code/Java/File-Input-Output/Countthenumberoflinesinthebuffer.htm

And afterwards select your line using this code:

FileInputStream fs= new FileInputStream("someFile.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fs));
for(int i = 0; i < 30; ++i)
  br.readLine();
String lineIWant = br.readLine();

As you can see, you iterate, reading each line(and doing nothing) before you get to the one you want (here we got 31 lines passed and #32 is the one read). If your file is huge this will take a lot of time.

Other way to to this is to input everything in a List and then with a sizeof() and a for() you can select everything you want.

Loic O.
  • 486
  • 2
  • 7
  • 22
  • if it is a normal file with limited or few no of line... ya i agree with your answer. But my file normally having 64000 or even more lines ... If i take this much of data in to list and processing ... it will kill the performance of application and will take at least of 3-5 mins of time to do it – user2017714 Jan 28 '13 at 10:36
0

If you know the length of each line then you can work out how many lines there are by looking at the size of the file and dividing by the length of each line. (this of course ignores any possible metadata in the file)

You can then use some maths to get the start byte of the last line. Once you have then you can then open a RandomAccessFile on the file and then use seek to go to that point. Then using readline you can then read the last line

This does assume though that the lines are all the same length.

RNJ
  • 15,272
  • 18
  • 86
  • 131
  • this is erroneous solution that you are providing and any how it won't fit for my case ... – user2017714 Jan 28 '13 at 10:50
  • why erroneous? Have I misunderstood your question? If it wont fit for your case then please update your questions with more detail – RNJ Jan 28 '13 at 10:57
  • i clearly commented in prev comments... Any how the reason is as follows: The number of lines will be changing in real time and is more than 64000 and count continues... If i follow your suggestion it will be erroneous. Bcoz of if i divide the file size by the length of each line will result imperfect result and not fit for me and any body........ – user2017714 Jan 28 '13 at 11:12
0

You can use FileUtils

and use this method

static List<String> readLines(File file) 
          Reads the contents of a file line by line to a 
          List of Strings using the default encoding for the VM.

This will return a List then use Collections.reverse()

Then simply iterate it to get the file lines in reverse order

Bhavik Shah
  • 5,125
  • 3
  • 23
  • 40
0

This might be helpfull for you [1]: http://mattfleming.com/node/11

vicky
  • 561
  • 1
  • 7
  • 17
0

Just save info backwards, that's all I did.just read Pryor to save and use \n

Rory
  • 23
  • 3
-1

You can save the lines in a list (in my code a arraylist) and "read" the lines backwards from the arraylist:

try
    {

        FileInputStream fstream = new FileInputStream("\\file path");
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String line = "";
        ArrayList<String> lines = new ArrayList<String>();

        //Read lines and save in ArrayList 
        while (br.ready())
        {
            lines.add(br.readLine());
        }

        //Go backwards through the ArrayList
        for (int i = lines.size(); i >= 0; i--)
        {
            line = lines.get(i);
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
phoenix
  • 49
  • 7
  • It's bad form to copy another poster answer, especially when the OP has already said it is not a suitable solution. – codeghost Jan 28 '13 at 10:43
  • Do you think it will work with out effecting the performance of the app if the number of lines are arround 64000 lines and is reading the files in real time.............. – user2017714 Jan 28 '13 at 10:45
  • sorry ... I was too slow to respond ... when I wrote my answer the others were not there ^^ – phoenix Jan 28 '13 at 10:45
  • Please don't use DataInputStream to read text http://vanillajava.blogspot.co.uk/2012/08/java-memes-which-refuse-to-die.html – Peter Lawrey Jan 30 '13 at 20:34
  • yes ... i just copy the code of user2017714 ... I use a BufferedReader combined with a FileReader – phoenix Jan 31 '13 at 07:24