0

So currently my netbeans project folders looks like this:

  • Block_Breaker <--Project
    • build
    • dist
      • Block_Breaker.jar
    • nbproject
    • src
      • packageONE
      • packageTWO
        • data.txt
    • manifest.mf
    • applet.policy
    • build.xml

I want to know how can i acces a data.txt file in packageTWO(when i run Block_Breaker through a jar file and not netbeans). Normally if run through netbeans the following code will work:

FileWriter x=new FileWriter("src/packageTWO/data.txt"); PrintWriter pr=new PrintWriter(x); 

But if i run a jar file that netbeans created it doesnt work.

2 Answers 2

2

You can't write to that file once it is packaged into a jar file.

Yet reading is still possible using one of the following:

<YourClass>.class.getClassLoader().getResourceAsStream("packageTWO/data.txt"); // or this.getClass().getResourceAsStream("/packageTWO/data.txt"); 

witch gives you an InputStream witch you can use to retrieve the content of the file.

If you are required to wite to that file then the simplest way is not to pack it into the jar but have it standalone some where on the filesystem.

More infos about getResourceAstream in the javadoc

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

2 Comments

Yeah i should have known that myself sorry for being such a dumbass.
@user1952989 heh, don't be so harsh on yourself ;-)
0

This is because your .jar file does not include a folder named src/

Please use ClassLoader.getResource to load resources.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.