I know that this question is a duplicate of : 1) Java - Writing to txt in a JAR file 2) How to read/write file in jar 3) How can modify/edit a file data which is inside a jar?
But I've read it all and I still not have a concrete answer.
If I have a .jar file with this tree:
(dir)META-INF -> MANIFEST.MF
(dir)test -> test.txt
-> Test.class
I can read the .txt file with this code:
public String readFromJARFile(String filename)throws IOException {
InputStream is = getClass().getResourceAsStream(filename);
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
StringBuffer sb = new StringBuffer();
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
br.close();
isr.close();
is.close();
return sb.toString();
}
If I need to write into that .txt file, can I do it? If yes, how? If not, why?
Thanks.