Question:
Opening files that were included in the jar file JAVA?
Michael 007
2009-04-25 12:21:53 UTC
Hi, I'm making a program to open text files and read them and search for certain string. The problem is when I create the File and FileReader class, I have to put the files that I'm reading in the same directory as the jar file. I want to package everything inside the jar file how would I go about reading the text files inside the jar file?
Three answers:
?
2009-04-25 12:38:58 UTC
Use the classloader:



InputStream stream =

getClass().

getResourceAsStream("yourfile.txt");



or if running from a static method:



InputStream stream =

YourClass.class.

getResourceAsStream("yourfile.txt");



You can then put that InputStream in a Scanner to do whatever you like:



Scanner scanner = new Scanner(stream);



(Note: deonejuan's code doesn't work. First of all, he never opens the file, so you'd get a NullPointerException when you attempt to open the Scanner. Also, you can't use a FileReader like he was attempting to do in his commented out code. A file inside a jar is not a "File", it is a jar element and you'd get a FileNotFoundException.



If you want to open a file that was located via getResource(), you'd do this:



URL url = getClass().getResource("blah.txt");

InputStream stream = url.openStream();

Scanner scanner = new Scanner(stream);

)
?
2016-12-05 21:01:05 UTC
To make a launchable .jar demands you write a take place report, that's surely a text cloth report with a path to the class with the main important. Then, you may use the jdk jar application with a pair of the innovations flagged in the command line. this is executed, even though it takes too a lot time. as a replace, get the NetBeans IDE to place in writing code and once you're chuffed with your application, use the save very final build command and the .jar is able to distribute. you will study quicker and write greater beneficial code making use of NetBeans.
deonejuan
2009-04-25 12:37:27 UTC
it's the same as using the path to the .class files. Be aware that you can READ from a .jar. You can NOT WRITE to a .jar file.



here is an example, we assume the .txt file is next to program.class, otherwise you must use dos commands to navigate within the .jar folder structure



private ArrayList loadData(String filename) {



data = new ArrayList();

FileReader fin = null;

try {

URL url = getClass().getResource("../../Data/"+ filename);

System.out.println(url.getPath());

// System.out.println(url.getPath());

// fin = new FileReader(url.toURI().getPath());

} catch (Exception e) {

System.out.println("Problem loading file " + filename + ": " + e);

return null;

}



Scanner sc = new Scanner(fin);

while (sc.hasNext()) {

data.add(sc.nextLine());

}

sc.close();

try {

fin.close();



} catch (IOException ex) {

System.out.println("problem closing FileReader in");

}

return data;

}

==============

to have java write and read outside the .jar takes Serializable interface or Preference API (java6)



And note, java can handle .zip without any problems


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...