• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Writing to a file in Java..?

 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 3285
13
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Prabhu,

What version of the JDK are you using?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 3285
13
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic