aspose file tools*
The moose likes Java in General and the fly likes Writing to a file in Java..? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Writing to a file in Java..?" Watch "Writing to a file in Java..?" New topic
Author

Writing to a file in Java..?

prabhu pandurangan
Ranch Hand

Joined: May 23, 2008
Posts: 120
Dear Members,

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.

Regards,
Prabhu.
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

Hi Prabhu,

What version of the JDK are you using?


Cheers, Martijn - Blog,
Twitter, PCGen, Ikasan, My The Well-Grounded Java Developer book!,
My start-up.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Don't know what Martijn's question is for, but regardless of the answer, the problem regarding the file is here:

outputWriter = new BufferedWriter(new FileWriter(file));

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.


[Jess in Action][AskingGoodQuestions]
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

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: 120
@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: 41461
    
  51
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.


Ping & DNS - my free Android networking tools app
prabhu pandurangan
Ranch Hand

Joined: May 23, 2008
Posts: 120
@Ulf

Im doing the same as you suggessted, but the old file is not written to the new file.

My code:
--------



Please correct me if im wrong in logic.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41461
    
  51
You need to close the reader and the writer before trying to delete or rename the underlying files.
prabhu pandurangan
Ranch Hand

Joined: May 23, 2008
Posts: 120
@Ulf,

Ya i forgot to do it, ya i have done with it.. just now i pointed it out.

Thank you ulf.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Writing to a file in Java..?