txt file is not found when generating file. jar

Asked

Viewed 769 times

5

I have an application in Java, this application has images, and I had the same problem: when generating the arch. jar(executable) the images did not appear, but if it ran straight into Netbeans, it would appear. I solved the problem by putting the image inside a folder I created (data) inside the scr folder, and in the code I did:

new ImageIcon(getClass().getResource("/dados/img.jpg"));

This solved the problem, now the business is with file . txt, how do I do it? I need to read a txt, in Netbeans it finds the right file, but the generated jar file is not found

EDIT

File arq = new File("src/dados/arq.txt");

1 answer

11


If the txt is inside the jar, File will not work, because the txt file only exists inside the jar, and does not exist outside it, because File works with addresses(URL) of files in the current operating system. For this, rescue using the following:

InputStream in = getClass().getResourceAsStream("/dados/arq.txt");
BufferedReader input = new BufferedReader(new InputStreamReader(in));

Then just sweep the Bufferedreader using a repeat loop.

Reference:

How to read a file from a jar file?

Including a text file Inside a jar file and Reading it

  • 1

    Worked thanks

Browser other questions tagged

You are not signed in. Login or sign up in order to post.