Results 1 to 13 of 13
  1. #1
    emyk is offline Member
    Join Date
    Mar 2011
    Posts
    19
    Rep Power
    0

    Default How to read current previous and next line from a text file

    I have a script that reads current and next line from a text file and I am trying to modify this script so that it reads three lines (current prev next or current and next two lines ).

    Any help is greatley appreciated.

    //Construct the BufferedWriter object
    File file = new File("C:/text.txt");
    bufferedWriter = new BufferedWriter(new FileWriter(file));
    reader = new BufferedReader(new InputStreamReader(in1));

    for (String next, line = reader.readLine();

    line != null; line = next)
    {
    next = reader.readLine();
    System.out.println("Current line: " + line);
    System.out.println("Next line: " + next);
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,926
    Rep Power
    8

    Default

    Seems like you'll have to read them all in, keeping track of all three and updating them as you go.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    emyk is offline Member
    Join Date
    Mar 2011
    Posts
    19
    Rep Power
    0

    Default

    KevinWorkman - 'Keeping track' is where I need help :) I know I may need to use some sort of simple loop to go through the entire file line by line and then to save the three lines into a memory as variables as I found them. But I have no clue on how to go about this.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,926
    Rep Power
    8

    Default

    What part of this are you having trouble with? I'm not exactly sure what your goal is, but if you just want to keep track of a previous, current, and next, wouldn't three String variables do the trick?

    Although, your "next" variable would actually have to be the last line you read, your "current" variable would be the line read before that, and your "last" would have to be the one read two lines before the last line you read.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    emyk is offline Member
    Join Date
    Mar 2011
    Posts
    19
    Rep Power
    0

    Default

    Let us say I am rading one line at a time and my current line (readline) is at line 3 and the file I am reading has 4 lines, so what I am trying to do is that when reading line 3 I need to be able to compare line 3 (current line) with line 2 (prev) and line 4 (next).

    So if If I store line3 to three variables when I am reading line 3 I will end up having three variables with same value. I am sure I need to do more than that.

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,926
    Rep Power
    8

    Default

    When you read a line:

    Set previous equal to current.
    Set current equal to next.
    Set next equal to the line you just read.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    emyk is offline Member
    Join Date
    Mar 2011
    Posts
    19
    Rep Power
    0

    Default

    I tried different combinations of the order of the String varaibles, but I still not getting the three expected lines

    BufferedReader reader = null;

    reader = new BufferedReader(new InputStreamReader(in1));
    for (String next, line = reader.readLine(); line != null; line = next) {
    next = reader.readLine();

    System.out.println("Current line: " + line);
    System.out.println("Next line: " + next);

    String Previous = line;
    String Current = next;
    String Next = Current;
    //next = line;

    System.out.println("Prev: " + Previous);
    System.out.println("Curr: " + Current);
    System.out.println("Next: " + Next);
    }
    Here are my four lines:
    Jacksonville,Florida,200,20110404
    Jacksonville,Florida,200,20110405 -->Prev
    Jacksonville,Florida,200,20110406 -->Current
    Jacksonville,Florida,200,20110405 -->Next

  8. #8
    emyk is offline Member
    Join Date
    Mar 2011
    Posts
    19
    Rep Power
    0

    Default

    Here is a working script. This script reads four line at a time:

    Java Code:
            String line; 
            String line1;
            String line2;
            String line3;
             try {
               FileReader input = new FileReader("C:\\text.csv");
    
            BufferedReader bufRead = new BufferedReader(input);
    			
                  //String line1; // String that holds current file line
                 int count = 0;	// Line number of count 
                 int count1 = 0;
                 int count2 = 0;
                 int count3 = 0;
                // Read first line
                line = bufRead.readLine();
                count++;
                count1++;
                count2++;
    			// Read through file one line at time. Print line # and line
                while (line != null){
                     System.out.println("1st Line:" + count+": "+line);
                     line1 = bufRead.readLine();
                     line2 = bufRead.readLine();
                     line3 = bufRead.readLine();
                     line = bufRead.readLine();
                     
                     System.out.println("2nd Line: " +count1+": "+line1);
                     System.out.println("3rd Line: " +count2+": "+line2);
                     System.out.println("4rd Line: " +count3+": "+line3);
                     count++;
                               }
                
                bufRead.close();
    			
            }catch (ArrayIndexOutOfBoundsException e){
              	System.out.println("Usage: java ReadFile filename\n");			
    
    		}catch (IOException e){
    			// If another exception is generated, print a stack trace
                e.printStackTrace();
            }
    Last edited by Fubarable; 04-21-2011 at 09:26 PM. Reason: code tags added

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by emyk View Post
    Here is a working script. This script reads four line at a time:

    Wouldn't it be more practical to read one line at a time?

    Also, I added code tags added to your post above to help make it readable.

    To the OP, in the future, to do this yourself, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:

    Java Code:
    [code]
      // your code goes here
      // notice how the top and bottom tags are different
    [/code]
    Best of luck

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    And also, what if the file's lind count isn't a multiple of three?

  11. #11
    emyk is offline Member
    Join Date
    Mar 2011
    Posts
    19
    Rep Power
    0

    Default

    Fubarable - that explains why it is only reading 12 lines while the file I am reading has 16 lines :) Now I back to square one.

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Myself, I'd do what the others recommend, read one line at a time and set the three String variables accordingly. But you can only have the current line and the previous two lines, not the next line [edit: as Kevin already mentioned]. The only way to get the "next" line that I can think of is to read all of the lines into an ArrayList<String> and then after your done, iterate through the list getting the ith line, the (i-1)th line, and the (i+1)th line, assuming that you've accounted for both edges -- the beginning and the end of the list.
    Last edited by Fubarable; 04-21-2011 at 11:20 PM.

  13. #13
    emyk is offline Member
    Join Date
    Mar 2011
    Posts
    19
    Rep Power
    0

Similar Threads

  1. [SOLVED] Delete Current line from file
    By Azndaddy in forum New To Java
    Replies: 2
    Last Post: 04-06-2012, 08:00 AM
  2. Replies: 1
    Last Post: 02-13-2011, 03:09 PM
  3. Stepping through a text file line by line
    By evanlivingston in forum New To Java
    Replies: 10
    Last Post: 01-29-2011, 04:30 AM
  4. Replies: 8
    Last Post: 12-28-2010, 02:24 PM
  5. Replies: 4
    Last Post: 09-10-2009, 10:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •