File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Writing to a file in Java..?

 
prabhu pandurangan
Ranch Hand
Posts: 147
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3275
5
Eclipse IDE Java Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Prabhu,

What version of the JDK are you using?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Martijn Verburg
author
Bartender
Posts: 3275
5
Eclipse IDE Java Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 147
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@All,

I need to overwrite an already existing file and change some string in it, thats it. Hos shall it be done.
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
prabhu pandurangan
Ranch Hand
Posts: 147
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to close the reader and the writer before trying to delete or rename the underlying files.
 
prabhu pandurangan
Ranch Hand
Posts: 147
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Ulf,

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

Thank you ulf.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic