Question

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?

Était-ce utile?

La 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.

Autres conseils

You can reach your txt-File with this code:

File f = new File("xxxx/myTXTFile.txt");

or you must save your txtfile in a Tempfolder:

        InputStream is = getClass().getClassLoader().getResourceAsStream("xxxx/myTXTFile.txt");
        OutputStream os = new FileOutputStream(new File("/tmp/tempTXTFile.txt"));

        while ((i = is.read(buffer)) != -1)
        {
            output.write(buffer, 0, i);
        }

        output.close();

        is.close();

        File f = new File("/tmp/tempTXTFile.txt");
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top