After exhausting the internet looking for the proper way to write a stream of data within a running JAR file, I have no idea how to take a stream of data, (say a string) and output to a *.txt
file next to the *.jar
. Here's my writing attempt:
InputStream stream = new ByteArrayInputStream("some string".getBytes("UTF-8"));
OutputStream resStreamOut = new FileOutputStream(new File("/dir-next-to-jar/some.txt"));
int readBytes;
byte[] buffer = "some string".getBytes();
while ((readBytes = stream.read(buffer)) > 0) {
resStreamOut.write(buffer, 0, readBytes);
}
This is how I've already successfully loaded a local file next to the JAR into the JAR:
InputStream stream = ThisClassName.class.getClass().getResourceAsStream("/dir-next-to-jar/some.txt");
So, how do I write new data to the same file I loaded into a stream to the JAR?
It seems like the obvious solution would be to reverse the action I took loading the *.text
file into the JAR, but this is proving to be impossible.
Note: I use a String
as the data type of interest here, but in reality, I'm interested in writing a JSONObject
and BufferedImage
out from the JAR to a file.
Please help!!!