0

so i developed a java application and trying to read the file in, i get error message saying the file configuration.txt doesnt exist.

here is the file reading code:

    public static void main(String[] args) throws FileNotFoundException {

    //reading configuration file
    Scanner readConfigurationFile = new Scanner(new File("configuration.txt"));

yes, i do have the imported necessary IO library and utilities included.

i moved the file everywhere (in the package, or in the project folder, or the bin folder, etc..) and kept testing, but it didnt work.

the only way it works is when i moved it to the desktop and i put the path of the file on the desktop in the code (so C:\User\....\configuration.txt)

but thats not how i want it, because others have to run it without changing code.

any suggestions/help? i have looked everywhere online and tried different methods but they didnt work. and some would complicate my code so i just avoided.

thanks in advance

2
  • 2
    This should tell you where it's looking: System.out.println(new File("configuration.txt").getAbsolutePath()); Commented Mar 7, 2016 at 8:00
  • @shmosel THANK YOU!!! lol its supposed to go right inside the first folder to the project, where the build and manifest are Commented Mar 7, 2016 at 21:26

2 Answers 2

2

If your configuration file is intended to live inside the application (it will be deployed inside the jar or war) you should not use it as a File but as a Resource.

Say its relative path is org/example/files/configuration.txt (*). You can use it that way:

InputStream is = getClass().getClassLoader().loadRessourceAsStream(
    "org/example/files/configuration.txt");
Scanner readConfigurationFile = new Scanner(is);

(*) It could be src/main/resources/org/... if you use maven, or src/org/... if you use ant or eclipse notive compiler.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the info :) you can keep it as configuration.txt (no need for directory path) as long as you put it under the first project folder you access from netbeans folder
0

If you place it in your package folder you need the relative path to it.

Taking as example this structure

  • src
    • main
      • java
        • com
          • example
            • Main.java
            • configuration.txt

You would access it

new File("src/main/java/com/example/configuration.txt");

even if you access it from Main.java

3 Comments

Correct, but you should warn that accessing a file inside source tree will lead to problem if you later use a jar or war...
@SergeBallesta I would, but I do not know what you are referring to more precisely :) I just gave this example for him not to try using a relative path to the class file
thanks for the info, i figured it out its supposed to go in the first folder

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.