DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    May 2005
    Posts
    5

    Unhappy Help saving data to text file

    I'm writing a code wich generates a lot of data, and I want to save this to a text file.

    The basic code that I'm working with is

    Code:
    	
    static void writeFile(String text)
    	{
    	try
    	{
    		File file = new File(SaveFile);
    		FileWriter fw = new FileWriter(file);
    		BufferedWriter writer = new BufferedWriter(fw);
    		writer.write(text);
    		writer.close();
    	}
    	catch (Exception ex)
    	{
    	ex.printStackTrace();
    	}
    The problem is, however, that there is a lot of data to save.
    I have tried three different methods, none of which work:

    1: Save all data to a string named saveData, then pass this to writeFile at the end of the program.
    Problem: saveData gets too big, and I get java.lang.OutOfMemoryError

    2: Break apart that method: place the File initializations at the start of the program, use writer.write(text) everytime I get data, close at the end of the program
    Problem: Needs to be in its own try-catch method

    3: Use if-then statements to say
    If text = "open" initialize file,
    If text = "close" close file,
    Otherwise write to file
    Problem: Since creating the file is in an "If-Then" statement, claims file hasn't been initialized.

    So what can I do? All I want to do is save a bunch of data to a text file. Any help much appreciated.

    Thanks.

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Why can't you write the data to the file as it is generated/collected ?
    If your strategy requires that all the data has to be ready for output
    before you start writing to file, - then you should reconsider that strategy.

    Code:
    BufferedWriter writer=null;  // class global variable
    
    try {
    writer=new BufferedWrite(new FileWriter(new file(path)));
    } catch (IOException ioe) {
      ioe.printStackTrace();
      return;  // bail out
    }
    // then do the data collection, and whereever it is logically ok, just
    // write the data to the file, alternatively, just pass the writer pointer
    // to the methods that are in control of the data I/O
    .
    .
    .
    try {
      writer.close();
    } catch (IOException ioe) {
      ioe.printStackTrace();
    }
    PS: you might want to try collecting the data in a StringBuffer instead of
    a String, as that class is much better suited for such a task.
    Last edited by sjalle; 06-06-2005 at 10:34 AM.
    eschew obfuscation

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links