I have to write some data of size 1GB to a file using bufferedReader and StringBuffer. As my file size is huge stringBuffer is not supporting throws out of memory exception.
// the input is streamed into a pipe line inputStream = runtimeProcess.getInputStream(); bufferReader = new BufferedReader(new InputStreamReader(inputStream)); char[] charBuffer = new char[BUFFER]; while ((count = bufferReader.read(charBuffer, 0, BUFFER)) != -1) { backupData.append(charBuffer, 0, count); outputWriter = new BufferedWriter(new FileWriter(file)); // backup data is written to a file outputWriter.write(backupData.toString().replaceAll(databaseName, backupDatabaseName)); backupData.delete(0, backupData.length()); }
But if i do so, only the last set of data is written to the file.
But i need to begin the writing to file as it reads.
When you create a FileWriter object this way, the file on disk is created (if it doesn't exist) or truncated to zero length (if it does.) So each time through the loop, you're erasing all the data you've previously written.
All you need to do is move this statement outside the read loop; create just one BufferedWriter/FileWriter, and call write() on it many times. Then you won't need the StringBuffer anymore!
Be sure to call close() on the BufferedWriter when you are done, to ensure that all the data gets written to disk and the file resources are released.
I was going to see if using the nio would be an alternative since he was dealing with such large volumes (I'm no expert but I'm told that it's a performance increase). I got a little side tracked instead of answering the Q directly
prabhu pandurangan
Ranch Hand
Joined: May 23, 2008
Posts: 118
posted
0
@All,
I need to overwrite an already existing file and change some string in it, thats it. Hos shall it be done.
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35241
7
posted
0
The complete file will be overwritten if you proceed as Ernest suggested.
Are you saying that you only need to change a small part of it? If so, there are no ready-made classes to do this. Your code would need to open the existing file, read its contents, write it to a second file -carefully replacing the old parts with the new parts- and then delete the first file, and rename the second to the first.
If the part to be replaced has EXACTLY the same length as the part replacing it, you can accomplish this directly by using the RandomFile class.