Search code examples
javajarembedded-resourcefilepathgetresource

How can i get .txt file in my jar?


I want to say I search a lot even google and stackoverflow. There are many topics about this but I dont find exactly what I need.

My problem is :

I want to define file which is in my own package folder.File's format is .txt. And file is in xxx\src\xxxx\myTXTFile.txt I write this code to reach my txt:

  File f = new File(xxx.class.getResource("pr.txt").getFile());

When I run this code on netbeans yes it works.But when I compile and click .jar file it reports:

File not found 

What can I do?


Solution

  • In the IDE the file still resides also outside the zip-format jar. Hence was found. For files inside the jar use:

    OutputStream f = xxx.class.getResourceAsStream("pr.txt");
    

    File does not work.