2

I'm facing a situation where I have to write 300 MB of data into XML. Using Streams I have perfectly executed the task without facing OutOfMemory exception.

Now I have to convert the FileOutputStream object into actual data and pass it as String to other method.

I have tried and I'm not getting the expected result. Kindly let me know how to convert FileOutputStream object to String value?

7
  • So you need to use the data you just wrote to pass data to another object or is the data elsewhere too? Commented Apr 2, 2013 at 8:34
  • I have to read this FileOutputStream object and take that string value and pass it to another method for other process. Commented Apr 2, 2013 at 8:37
  • so you want: read line from file and this line pass to another methods argument, true? Commented Apr 2, 2013 at 9:10
  • what are you using to write data to XML? Commented Apr 2, 2013 at 9:42
  • @linski - I'm using stream to write data into the XML. Commented Apr 2, 2013 at 10:10

3 Answers 3

3

If your String is too big and you are trying to keep it in memory at a time, it will always cause you OutOfMemoryError independent of the fact which reader you use.

So, the problem is not your Reader. The problem is your String object (the one you want to pass).

There is definitely something wrong with your architecture, if you need to store/pass such a huge file as a String variable.

Your mistake is that you are trying to put your XML text in a Java String variable. You should avoid storing big objects in memory, that's the key point here.

Generally texts are a subject to compression with the compression ratio of 0.1 - 0.01.

Try to use ZipOutputStream/ZipInputStream (or some similar libraries) - they work fine with streams reading/writing to files.

In my practice it works perfectly with huge XML files.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your reply. As you mentioned we need to change the architecture. There is no othe option. +1 for explaining it clearly.
0

How are you writing in file? FileOutputStream creation is OK, but when you write using FileOutputStream you have to pass something to it. When you are passing the data to FileOutputStream.write(some_data), why don't you write the same in a StringBuffer at the same time?

2 Comments

StringBuffer internally uses append method. When writing huge data it will give you OutOfMemory exception.
Are you trying to append whole data at a time? How are you writing in the outputStream, all at a once or byte by byte? In case you are writing byte by byte, inside the same loop if you appent to StringBuffer does it throw exception? you can also check this: link
0

You cannot read from a FileOutputStream. Try FileReader to read the data from a file.

Couldnt you use the data you originally used to create the file? If not you could use an XML Parser like SAX or JDOM to read the data

4 Comments

Now I have FileOutputStream object and I have to convert it to a string. How we can do that?
I am confused as to what you want to do. Have you written to a file yet? What are you expecting by converting the FileOutputStream object to a String? Why do you need to do this?
Actually the method which I'm doing this logic should return a string value, which will be used in some other methods. I tried fileOutputStream.toString(), which is returning the object. But I need a string value to be returned.
What is the reason for this String value? Why does it need it? It's hard to get exactly what you ask with the limited information above

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.