Before, when I used file in my fileWriter it worked, but now, since I’m using getResourceAsStream instead of file, how can I make it work?
FileWriter fstream = new FileWriter(new File("filename"), true);
Now, when I pass
InputStream is = getResourceAsStream("filename");
FileWriter fstream = new FileWriter(is, true);
I had to change it, because when I create a runnable jar with the maven assembly plugin there is no src/main/resources in the jar
EDIT:
thanks to casablanca and others for pointing out my mistakes, follow up :
I need to write to a file, but append to it, with preserving the original content. And I need to know to which file I’m writing of course.
InputStreamrepresents an input stream, so it is not suitable as ouput. You cannot write to anInputstream.getResourceAsStreamreturn a stream for reading a resource, not for writing to it.I’m affraid there is no easy way to write to resource loaded via the
ClassLoader. One solution would be to read it asProperties, and then use thestoremethod to write it to a resource file, obtaining the output stream by other means. You can obtain the URI of the resource file using the classloader, but there is no guarantee you can write to it directly (for example if it is bundled in a signed jar).