Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

When reading files using Java with Eclipse, where should the text files go that are being read?

Title says it all. I keep getting FileNotFoundExceptions regardless of the text file being right there in the same folders as the .java and .class files.

1 Answer

Relevance
  • 1 decade ago
    Favorite Answer

    An IDE databases all the paths to the parts that make a java application. Thus, you can run code in an IDE that won't crank on the desktop. Further, the CWD is the folder level holding the /src and /class when the IDE runs java.

    Put the .txt in the same folder as the .class. Here is boilerplate that will drill for you

    try {

    URL url = getClass().getResource("mySpiffyBook.txt" );

    File file = new File( url.toURI() );

    if( file.isFile() ) {

    System.out.println( file.getPath() );

    }

    } catch (Exception e ) {

    System.out.println( "Cannot find" );

    e.printStackTrace();

    }

    //run that as an experiment.

    A simple file / open / read program will work from cmmd-line, CD into a folder. Then the path is no-doubt "SpiffyText.txt". The problem has always been the JRE ideal of what CWD is vs. what the human thinks it is.

Still have questions? Get your answers by asking now.