macOsX
macOsX

Reputation: 457

How to retrieve String value from FileOutputStream object?

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?

Upvotes: 2

Views: 3095

Answers (3)

Alex Kreutznaer
Alex Kreutznaer

Reputation: 1170

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.

Upvotes: 3

Skepi
Skepi

Reputation: 478

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

Upvotes: 0

Abhinaba Basu
Abhinaba Basu

Reputation: 351

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?

Upvotes: 0

Related Questions