Sorry to ask but what are you actually trying to achieve? Reading text file from JAR is simple, but you will just do it on the same machine where JAR is used as the file is mostly just some part of configuration or commentary since you cannot update it without extracting JAR and packing it back again...
peter_budo
Code tags enforcer
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
you need to create file through your code.
rushikesh jadha
Junior Poster in Training
89 posts since Dec 2011
Reputation Points: 4
Solved Threads: 11
well, that is because you haven't created the text file there. is there a possibility that the elements will be different on another machine, or during another run of the application, or will it always have the same values?
also good to know, is whether every instance of the application will need to get all the same elements in those comboboxes or not.
stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
that's because that is the only place you've created that txt file with values. it's possible to, if you want every instance (even over several computers) to get the same values, to either hardcode them, or have each instance create it's own .txt file or to store the information in a centralized DB.
stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
well yes, but will it contain exactly the same content, or can this depend on by who or where the application is used?
stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
You would normally use the classloader's getResourceAsStream method - which has the advantage of immediately giving you an open InputStream on the file in the current jar. You'll find the doc in the usual places, but briefly:
in the code of a class that was loaded from the same jar...
InputStream in = this.getClass().getResourceAsStream("/myTextFile.txt");
If the file is in a folder within the jar then that would be "/myDirectory/myTextFile.txt"
When developing your code this also works outside a jar - as long as the file is in the same place relative to where the .class file was loaded from.
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
Adding to a file in a jar is a more complex problem. Would it make sense to hold an initial/default list in the jar, but put each user's additions into a separate file in the user's home dir?
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
it might also be easier to have the list stored in a class instead of a text file. Enum's spring to mind :)
stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
hello all
the text file's content is the same until user add item into it,and the code
used in a stand alone machine
thank
denny
Well I guess you could store the text file within your packaged jar file, either by adding it during compile time by looking here: http://javaworkshop.sourceforge.net/chapter5.html#Creating+and+Viewing+a+Jar+File or by renaming your jar to *.zip extracting it placing text file within jar file and then rezipping the file and renaming to *.jar. You could then access the text file like so:
import java.io.*;
import java.util.*;
public class JavaApplication44 {
public static void main(String args[]) throws IOException {
List<String> list = JavaApplication44.readTextFromJar("d.txt");//text file is in smae folder as main class
Iterator<String> it = list.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
public static List<String> readTextFromJar(String s) {
InputStream is = null;
BufferedReader br = null;
String line;
ArrayList<String> list = new ArrayList<String>();
try {
is = JavaApplication44.class.getResourceAsStream(s);
br = new BufferedReader(new InputStreamReader(is));
while (null != (line = br.readLine())) {
list.add(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
}
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return list;
}
}
once the user adds more data to the file you can just write it to an external location, or alternatively you could write a method that would extract and update the file and repackage the updated file, but depending on your needs thats overkill..
DavidKroukamp
Practically a Master Poster
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169