Results 1 to 10 of 10
  1. #1
    Rohaan is offline Member
    Join Date
    Jan 2010
    Posts
    7
    Rep Power
    0

    Default Read text file with eclipse

    i am trying to get my program to read a text file. i am getting a FileNotFoundException
    at these lines

    Java Code:
    java.io.File file = new java.io.File("Questions.txt");		
    		Scanner input = new Scanner(file);
    How do i get eclipse to find the file? What folder is the file supposed to go into?

    please help

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    797
    Rep Power
    6

    Default

    Have u checked Questions.txt is there in ur current directory.If not ,then give the complete path

    If the files are limited check manaully or else check with this method before putting into scanner object.

    System.out.println(file.exists());
    Ramya:cool:

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by RamyaSivakanth View Post
    If not ,then give the complete path

    But look out. If you use backslashes always give two ex. "c:\\path1\\path2\\file.name". To avoid this and other problems i usually use slashes ex. "c:/path1/path2/file.name".

  4. #4
    Rohaan is offline Member
    Join Date
    Jan 2010
    Posts
    7
    Rep Power
    0

    Default

    I created the text file using eclipse in the src folder under the package containing my program. the complete path is

    C:/Users/Rohaan Gupta/workspace/Trivia/src

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

    Default

    To see where Java is looking for your file, put in your main method this statement:

    Java Code:
    System.out.println(System.getProperty("user.dir"));
    then if it outputs
    C:\Users\Rohaan Gupta\workspace\Trivia
    you'll know that you'll want call your file src/Questions.txt:
    Java Code:
    java.io.File file = new java.io.File("src/Questions.txt");		
    Scanner input = new Scanner(file);

  6. #6
    Rohaan is offline Member
    Join Date
    Jan 2010
    Posts
    7
    Rep Power
    0

    Default

    thank you, it is working now

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

    Default

    Question: are you using packages (other than the default package)? It doesn't look like you are, and you should.

  8. #8
    Rohaan is offline Member
    Join Date
    Jan 2010
    Posts
    7
    Rep Power
    0

    Default

    i am using the default package. what difference does this make?

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

  10. #10
    Rohaan is offline Member
    Join Date
    Jan 2010
    Posts
    7
    Rep Power
    0

    Default

    Oh i see. i am following a book called "introduction to java programing"not online lessons. the book does not explain much about packages and all examples use the default package.

    i have copied it to a package called trivia2 but now i am getting this error
    Exception in thread "main" java.lang.Error: Unresolved compilation problem:

    at trivia2.Trivia2.main(Trivia2.java:66)

    which points to this

    Java Code:
    public static void main(String[] args)throws Exception {
    		JFrame frame = new Trivia2();
            
    								
    		frame.setTitle("Trivia Quiz");
    		frame.setSize(400,200);
    		
    		frame.setVisible(true);
    		
    		
    		
    		
    	}
    what does this error mean?

Similar Threads

  1. Can't read from a text file
    By kevin8160 in forum New To Java
    Replies: 2
    Last Post: 02-07-2010, 06:45 PM
  2. A more efficient way to read from a text file
    By matzahboy in forum New To Java
    Replies: 13
    Last Post: 10-14-2009, 11:35 AM
  3. Read from a text file?
    By aaronfsimons in forum New To Java
    Replies: 3
    Last Post: 05-01-2009, 04:42 AM
  4. read from text file
    By rayda in forum New To Java
    Replies: 5
    Last Post: 04-10-2009, 03:51 AM
  5. How to read a text file from a Java Archive File
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 09:13 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
  •