Input stream to output stream

Hi!!I have an object of type InputStream and i want to convert it into an object of OutputStream.Can anyone suggest me some solution for that?
[163 byte] By [javinaa] at [2007-11-27 10:02:19]
# 1
What?What is it, exactly, that you want to do? Do you want to write to the file you just read from? If so close the InputStream and open an OutputStream. Other Input to Output scenarios are likely to run analog to this one, so answer the question and maybe you can be helped.
masijade.a at 2007-7-13 0:36:37 > top of Java-index,Java Essentials,Java Programming...
# 2
http://www.google.com/search?sourceid=navclient&ie=UTF-8&rlz=1T4TSHB_enUS224US224&q=io+java+tutorials
pberardi1a at 2007-7-13 0:36:37 > top of Java-index,Java Essentials,Java Programming...
# 3
yeahI have an XML file as an inputstream. I wans to be able to write into that file.
javinaa at 2007-7-13 0:36:37 > top of Java-index,Java Essentials,Java Programming...
# 4
Then reread reply number one.
masijade.a at 2007-7-13 0:36:37 > top of Java-index,Java Essentials,Java Programming...
# 5
Didn't get you.Can u give me a sample code.
javinaa at 2007-7-13 0:36:37 > top of Java-index,Java Essentials,Java Programming...
# 6

You can't change an InputStream to an OutputStream. You need to close the FileInputStream, then open a FileOutputStream. If you simply wish to add to the end of the file, then you can (AFAIK) open in append mode. If you wish to "modify" the current contents of the file, then you need to write a new file, as you are reading the current file, making the modifications as you go, then close both Streams, remove the current file and rename the newfile to the original name.

masijade.a at 2007-7-13 0:36:37 > top of Java-index,Java Essentials,Java Programming...
# 7
no you can't change input stream into output stream. for this you first have to close the inputstream and then open the file into output stream
rajeev123a at 2007-7-13 0:36:37 > top of Java-index,Java Essentials,Java Programming...
# 8
Well actually i have my project package that i run inside an eclipse application. Now i want to be able to write a XML file inside that package .
javinaa at 2007-7-13 0:36:37 > top of Java-index,Java Essentials,Java Programming...
# 9
Okay?And your problem is?
masijade.a at 2007-7-13 0:36:37 > top of Java-index,Java Essentials,Java Programming...
# 10
The problem is that the only way by which i can find out anything inside my package is by using MyClass.Class.getResourceasStream(String) which gives me an inputStream.Now i can't figure out that how will i be able to write any XMl file inside a particular pakcage.
javinaa at 2007-7-13 0:36:37 > top of Java-index,Java Essentials,Java Programming...
# 11

Try

URL url = MyClass.class.getResource("whatever other file already exists");

File f = new File(url.toUri());

FileOutputStream fos = new FileOutputStream (f.getParent() + File.separator + "fileName");

Should work as long as the package is not in a jarfile.

masijade.a at 2007-7-13 0:36:37 > top of Java-index,Java Essentials,Java Programming...
# 12
when i run this code iam getting run time error as java.lang.IllegalArgumentException: URI scheme is not "file.
javinaa at 2007-7-13 0:36:37 > top of Java-index,Java Essentials,Java Programming...