Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: reading a file line by line

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default reading a file line by line

    import java.io.File;
    		import java.io.FileNotFoundException;
    	//	import java.io.FileReader;
    		//import java.io.IOException;
    		import java.util.Scanner;
    public class test1 {
     
    	/**
    	 * @param args
    	 * @throws FileNotFoundException 
    	 */
    	public static void main(String[] args) throws FileNotFoundException {
     
     
    					Scanner scan = new Scanner(new File("F:\\data\\file.txt"));
     
    					while(scan.hasNext()){
    						 String line = scan.nextLine();
    						 System.out.println(line);
    					}
     
     
    			}
    }
    when i execute the above code, am getting the error as below.Pls let me know what's wrong with my code.
    at java.io.FileInputStream.open(Native Method)
    	at java.io.FileInputStream.<init>(FileInputStream.java:120)
    	at java.util.Scanner.<init>(Scanner.java:636)
    	at test1.main(test1.java:23)


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: reading a file line by line

    You've left out the most important part of the stack trace, the Exception itself. Please copy the entire stack trace, including what type of Exception it was.

    The type of Exception (FileNotFoundException, NullPointerException, etc) will tell you quite a bit about what's going wrong, as well as give you something to google.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: reading a file line by line

    It seems to work for me. Check yours is finding the file.

  4. #4
    Member
    Join Date
    Feb 2013
    Posts
    45
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: reading a file line by line

    This code is work for me. you are get FileNotFoundException.
    So you check your file path.
    Put correct file path.
    Regards
    Android developer
    Trinay Technology Solutions
    http://www.trinaytech.com
    5705750475

  5. #5
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: reading a file line by line

    I have put the correct path,but still it isn't working.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,147
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: reading a file line by line

    Please post the FULL text of the error message.
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    shenaz (March 20th, 2013)

  8. #7
    Member
    Join Date
    Feb 2013
    Posts
    45
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: reading a file line by line

    refer this link:File I/O
    Regards
    Android developer
    Trinay Technology Solutions
    http://www.trinaytech.com
    5705750475

  9. The Following User Says Thank You to Tamilarasi For This Useful Post:

    shenaz (March 20th, 2013)

  10. #8
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: reading a file line by line

    Also post the text file you are reading from (if it's long, just 10 lines will do).

  11. #9
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: reading a file line by line

    Thank you all for responding.I solved the problem i was facing.

  12. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,147
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: reading a file line by line

    Please mark this thread as solved.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 10
    Last Post: September 16th, 2011, 07:49 PM
  2. Trouble Reading Line in File
    By Mandraix in forum What's Wrong With My Code?
    Replies: 9
    Last Post: April 4th, 2011, 10:47 PM
  3. Reading a file line by line using the Scanner class
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: April 17th, 2009, 07:34 AM
  4. Reading a file line by line using the Scanner class
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: April 17th, 2009, 07:34 AM
  5. How to Read a file line by line using BufferedReader?
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 06:32 AM