tags:

views:

58

answers:

3

Before when I use file in my fileWritter 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 runable jar with mave 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 it, with preserving the original content. And I need to know to which file I'm writing to of course.

+3  A: 

First of all, to write to a stream, you need a generic stream writer, not a file writer. Secondly, writing means output, so you need an output writer. So the class you're looking for is OutputStreamWriter.

getResourceAsStream returns an InputStream which you can only use for reading, which means you can only use a class such as InputStreamReader.

Update:

You already have the correct code for appending to a file, using FileWriter. However, getResourceAsStream returns a read-only resource, so there is no straightforward method to write data back to it.

casablanca
@casablanca thank you for your answer, what should I give to OutputStreamWritter(obviously OutputStream), where do I get it from? does this support appending file?I need to append file without removing the original contents.
Gandalf StormCrow
+2  A: 

InputStream represents an input stream, so it is not suitable as ouput. You cannot write to an Inputstream. getResourceAsStream return 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 as Properties, and then use the store method 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).

tonio
@tonio I could figure out that but which class should I use to get resource for writting?
Gandalf StormCrow
If you really want to write to a file, obtain the URI of the resource, using [findResource](http://download.oracle.com/docs/cd/E17476_01/javase/1.4.2/docs/api/java/lang/ClassLoader.html#findResource(java.lang.String)), and write to it.
tonio
+1 for further explanation, when I try getClass.getClassLoader().findResouce("/Myfile.txt"); again I get compile error like findResource doesn't exist, what about this?
Gandalf StormCrow
Well, my bad. In fact, `findResource` is protected, and you should use [`getResource`](http://download.oracle.com/docs/cd/E17476_01/javase/1.4.2/docs/api/java/lang/ClassLoader.html#getResource(java.lang.String)) instead.
tonio
A: 

if you read the docu, here, you will see, that there is no constructor for your InputStream. Thats why i does not compile. Since you did not ask anything, thats it.

InsertNickHere
@InsertNickHere forgot to ask, how can I get OutputStream? to use for OutputStreamWritter?
Gandalf StormCrow