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?

有帮助吗?

解决方案

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.

其他提示

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");
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top