views:

45

answers:

4

For javame.

Since getResourceAsStream() is for reading files, is there a getResourceAsStream() equivalent for output stream / writing files?

Note: the file is inside the project's folders and will soon be packaged into a jarfile.

A: 

Yes. create a File-Object and then call getOutputStream on this file object.

Tobias P.
The FileConnection class doesn't recognize a path of a file inside the project's folder or jar. =X
cancelledout
+1  A: 

It would not make sense to have that.
What would the getResourceAsStream get, since the resource does not exist yet?

In addition getResourceAsStream is typically used to access a file within a JAR or WAR, you cannot write there.

getResourceAsStream is typically used to load configuration and properties files.
Here is a good tutorial (a little dated) on how to use it.

Romain Hippeau
Thanks for your reply. That's why I'm wondering if there's an equivalent to that... something like setResourceAsStream? IDK. I need to write to the xml file inside a jar.
cancelledout
@cancelledout No can do - Look at the many posts and you will see that you cannot modify or delete a jar unless you create a new one and overwrite the old one. You will need to pick another destination. I already answered this question for you in http://stackoverflow.com/questions/3058435/how-to-write-to-an-xml-file-inside-a-jar-file
Romain Hippeau
A: 

FileOutputStream is also available for java me.

FileOutputStream out = new FileOutputStream("output.txt");
out.write( ... );
stacker
I will try this out as soon as I get back to the office. Thanks for your reply. :D
cancelledout
+1  A: 

getResoureAsStream() deals with resources. Returning an output stream doesn't make sense because that implies you are trying to overwrite your resources on the fly.

Besides, for most resources residing in a JAR or remote locations, you can't write to it anyway.

ZZ Coder
What I want to do is to overwrite a file inside a jar. Is this possible?
cancelledout
No. You can't overwrite a file inside a jar. To replace anything, you have to create a new jar.
ZZ Coder