Results 1 to 12 of 12
  1. #1
    Spooge is offline Member
    Join Date
    Oct 2010
    Posts
    10
    Rep Power
    0

    Angry Cannot find file for FileReader

    Entering a file name listings.txt to be used in this program. I am consistently getting a "file not found" error no matter where I put the file.


    //This was to determine where the program is looking for files
    File appBase = new File("FindMe.txt"); //current directory
    String path = appBase.getAbsolutePath();
    System.out.println(path);

    //This is to get the file name and put it into a Filereader
    //(file is to be predetermined as listings.txt)
    Scanner console = new Scanner(System.in);
    System.out.println("Enter the name of your listing file:");
    String listingsFile = console.nextLine();
    FileReader reader = new FileReader(listingsFile);
    //etc etc....


    // The listings.txt file IS IN the absolute directory indicated by the program. I // triplechecked.
    //Error message:
    Exception in thread "main" java.io.FileNotFoundException: listings.txt (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.jav a:106)
    at java.io.FileInputStream.<init>(FileInputStream.jav a:66)
    at java.io.FileReader.<init>(FileReader.java:41)
    at exercise.Main.main(Main.java:39)
    Java Result: 1

  2. #2
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    The file must be in the root directory. Where the src, .settings, and bin folders are located on your hard drive. I ran it with the same code and got no error. If you still get an error, try creating a completely new class, recompiling, and saving again.
    Sincerely, Joshua Green
    Please REP if I help :)

  3. #3
    Spooge is offline Member
    Join Date
    Oct 2010
    Posts
    10
    Rep Power
    0

    Question

    Tried this again, this time using Eclipse rather than Netbeans as my IDE. Same problem:

    "The import java.io.FileReader is never used"
    During debugging "Source not found."

    The listings.txt file IS IN the right directory!
    I am looking in windows explorer right now, it is there with the bin and src directories, and is in the same tree that the absolute path lines of code said.

    Why the heck is it not being found?

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

    Default

    Create and run a program similar to this one:
    Java Code:
    public class Fu3 {
       public static void main(String[] args) {
          System.out.println(System.getProperty("user.dir"));
       }
    }
    This: System.out.println(System.getProperty("user.dir")) ;
    will tell you where Java is looking when it's looking for the file.

  5. #5
    Spooge is offline Member
    Join Date
    Oct 2010
    Posts
    10
    Rep Power
    0

    Default Did that...

    I did exactly as you said; the directory indicated is precisely where the listings.txt file is located. I verified this again through Windows Explorer. It's there!

    Next suggestion??

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

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    I verified this again through Windows Explorer

    This involves too many assumptions.

    Do what what Fubarable suggested and output the value of "user.dir". Output the absolute path associated with what the user entered. Output the contents of the parent of this directory. Post the program output: diagnostic messages and runtime errors.

  8. #8
    Spooge is offline Member
    Join Date
    Oct 2010
    Posts
    10
    Rep Power
    0

    Default

    The indicated line of code:

    run:
    C:\School items\Java Stuff\Exercise2
    BUILD SUCCESSFUL (total time: 0 seconds)

    Therefore, I know exactly where the listings.txt file should be. And it IS THERE.

    When running the program:
    run:
    C:\School items\Java Stuff\Exercise2\FindMe.txt
    Enter the name of your listing file:
    C:\\School items\\Java Stuff\\Exercise2\\listings.txt
    Exception in thread "main" java.io.FileNotFoundException: C:\School items\Java Stuff\Exercise2\listings.txt (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.jav a:106)
    at java.io.FileInputStream.<init>(FileInputStream.jav a:66)
    at java.io.FileReader.<init>(FileReader.java:41)
    at taskstream2.Main.main(Main.java:39)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 17 seconds)

    If I could post a screenshot of the directory, I would. The listings.txt file is there! (no, not a hidden or system file or anything. Just a plain old notepad text file.

    And, as I have just discovered, I am to notify potential replyers that this question is also posted elsewhere (Java Ranch) and I have not received any replies there other than the assumption that the file is not where the program is looking. Same suggestions, same effect.

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

    Default

    Is this a String that you're entering at the command line?:
    C:\\School items\\Java Stuff\\Exercise2\\listings.txt

    With the double backslashes?

  10. #10
    Spooge is offline Member
    Join Date
    Oct 2010
    Posts
    10
    Rep Power
    0

    Default

    I've tried that double backslash way as well as single slashes, and of course the simple file name: listings.txt. All have the same error message. (I even tried the basic c:\listings.txt variations after copying the listings.txt file to that location too).
    Thank you.

  11. #11
    Spooge is offline Member
    Join Date
    Oct 2010
    Posts
    10
    Rep Power
    0

    Default Think I got it...

    I'm thinking I renamed the file listings.txt to verify the name; forgetting it retained the extension... so it was actually being called listings.txt.txt on the drive.
    Guess that's why I'm a nub.

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

    Default

    Quote Originally Posted by Spooge View Post
    I'm thinking I renamed the file listings.txt to verify the name; forgetting it retained the extension... so it was actually being called listings.txt.txt on the drive.
    Guess that's why I'm a nub.
    There's no crime in being a newbie as long as you don't give up. Thanks for giving us the solution to this problem as it's always good to get closure. Good luck with this project and your programming education.

Similar Threads

  1. Replies: 1
    Last Post: 02-05-2010, 05:53 PM
  2. Replies: 0
    Last Post: 02-05-2010, 04:49 PM
  3. FileReader help
    By emp in forum New To Java
    Replies: 1
    Last Post: 07-28-2009, 05:41 AM
  4. find file with * on the path
    By itaipee in forum New To Java
    Replies: 7
    Last Post: 07-08-2009, 09:05 AM
  5. add FileReader to GUI
    By VinTiger in forum New To Java
    Replies: 8
    Last Post: 05-11-2009, 01:23 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
  •