Results 1 to 12 of 12
Like Tree3Likes
  • 1 Post By PhHein
  • 1 Post By wsaryada
  • 1 Post By tamilarasi

Thread: Java I/O, trouble reading the 2nd line from a file

  1. #1
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Java I/O, trouble reading the 2nd line from a file

    I'm trying to read all lines from a file and display them on a standard output.
    Java Code:
    import java.io.*;
    
    public class Streaming
    {
    	public static void main(String[] args)
    	{
    		try {
    
    			FileOutputStream f = new FileOutputStream("Stream File.txt");
    			PrintWriter out = new PrintWriter(f, true);
    			out.println("My Name is Kagiso");
    			out.println("You'll simply fall in love with java");
    			out.close();
    
    			LineNumberReader  bf = new LineNumberReader(new FileReader("Stream File.txt"));
    
    			System.out.println(bf.readLine());       // this will only output the 1st line what do i do to get the second line ?
    
    		}
    
    		catch(IOException io)
    
    
    		{
    			System.out.println("Error! Error! Error!");
    			io.printStackTrace();
    		}
    	}
    }
    //And what if I only want to read only a second line from a file?
    Last edited by Kagiso; 03-18-2013 at 11:18 AM.

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Java I/O, trouble reading the 2nd line from a file

    You only read one line from the file. You need to loop as long as there are more lines. There are plenty of examples in various tutorials. Regarding reading line 2 only: look at the LineNumberReader API doc, or implement your own line counter.
    Kagiso likes this.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,419
    Rep Power
    20

    Default Re: Java I/O, trouble reading the 2nd line from a file

    Kagiso, please go through Guide For New Members and BB Code List - Java Programming Forum - Learn Java Programming and edit your post accordingly.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Java I/O, trouble reading the 2nd line from a file

    Consider to use the new API provided by JDK 7 java.nio package. Look for the java.nio.file.Files. Using this class you can read all lines from a file like:

    Java Code:
    List<String> lines = Files.readAllLines(Paths.get(filename));
    Kagiso likes this.

  5. #5
    tamilarasi is offline Member
    Join Date
    Nov 2012
    Location
    India
    Posts
    70
    Rep Power
    0

    Default Re: Java I/O, trouble reading the 2nd line from a file

    you don't create any method to print the all line in text file..

    So you first create the while loop like below and check the bf.readLine())!=null and store the value into variable and also print that variable.


    String data;
    while((data=bf.readLine())!=null)
    {
    System.out.println(data); // this will only output the 1st line what do i do to get the second line ?

    }

    you also refer this link:

    Reading a text file using FileReader & BufferedReader : Unexpected output ! (Beginning Java forum at JavaRanch)
    Kagiso likes this.
    Regards
    Android developer at Trinay Technology Solutions,http://www.trinaytech.com,5705750475

  6. #6
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: Java I/O, trouble reading the 2nd line from a file

    Quote Originally Posted by tamilarasi View Post
    you don't create any method to print the all line in text file..

    So you first create the while loop like below and check the bf.readLine())!=null and store the value into variable and also print that variable.


    String data;
    while((data=bf.readLine())!=null)
    {
    System.out.println(data); // this will only output the 1st line what do i do to get the second line ?

    }

    you also refer this link:

    Reading a text file using FileReader & BufferedReader : Unexpected output ! (Beginning Java forum at JavaRanch)
    Have tried it... it will print all the lines from a file. Leaving me with one question, what if my file had fifty lines and i only wanted to print the last line?

    because when the file has two lines I could write something like this
    Java Code:
    bf.readLine(); // which will read the first line from the file
    System.out.println(bf.readLne()); // which will print the second line
    it means invoking readLine() again and again will allow me to read all lines
    Last edited by Kagiso; 03-18-2013 at 11:44 AM.

  7. #7
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    384
    Rep Power
    5

    Default Re: Java I/O, trouble reading the 2nd line from a file

    Using the method stated by wsaryada in #4 returns a String List. You can then reference any element within as you would do for any List.

    Regards.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: Java I/O, trouble reading the 2nd line from a file

    Quote Originally Posted by Ronin View Post
    Using the method stated by wsaryada in #4 returns a String List. You can then reference any element within as you would do for any List.

    Regards.
    Except you would be loading the entire document into memory, which is generally not a Good Thing.
    For small test apps OK, but in normal work then that would often end up being a bug in waiting.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    384
    Rep Power
    5

    Default Re: Java I/O, trouble reading the 2nd line from a file

    True. Kagiso, do you know exactly which line you want to print out or does this change from time to time?

    Regards.

  10. #10
    tamilarasi is offline Member
    Join Date
    Nov 2012
    Location
    India
    Posts
    70
    Rep Power
    0

    Default Re: Java I/O, trouble reading the 2nd line from a file

    Quote Originally Posted by Kagiso View Post
    Have tried it... it will print all the lines from a file. Leaving me with one question, what if my file had fifty lines and i only wanted to print the last line?

    because when the file has two lines I could write something like this
    Java Code:
    bf.readLine(); // which will read the first line from the file
    System.out.println(bf.readLne()); // which will print the second line
    it means invoking readLine() again and again will allow me to read all lines
    can you please ask me clearly what you want exactly?
    Regards
    Android developer at Trinay Technology Solutions,http://www.trinaytech.com,5705750475

  11. #11
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Java I/O, trouble reading the 2nd line from a file

    Quote Originally Posted by Tolls View Post
    Except you would be loading the entire document into memory, which is generally not a Good Thing.
    For small test apps OK, but in normal work then that would often end up being a bug in waiting.
    Yes, you are right Tolls. I should have said that in the first place. Use this method when we sure to read all lines in a single operation and works with small files. Never used this method to read large text files.

  12. #12
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: Java I/O, trouble reading the 2nd line from a file

    thanks for all the advises and opinions,I was just curious.
    I'll have a look at the method you suggested

    thanx everyone

Similar Threads

  1. Java- Writing a file and reading a file line by line
    By Nazneen Ali in forum New To Java
    Replies: 7
    Last Post: 07-20-2011, 07:56 AM
  2. Having trouble opening and reading a txt file
    By DBaskov in forum New To Java
    Replies: 14
    Last Post: 01-29-2011, 02:55 AM
  3. Trouble with Try Catch blocks and file reading.
    By theBurgh22 in forum New To Java
    Replies: 2
    Last Post: 11-30-2010, 01:11 AM
  4. Having trouble reading external txt file to an array
    By Metastar in forum New To Java
    Replies: 18
    Last Post: 07-21-2010, 11:29 PM
  5. Reading in data from file line by line
    By bluekswing in forum New To Java
    Replies: 1
    Last Post: 10-02-2007, 12:19 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
  •