Overwriting Java FileWriter

I have a piece of code that generates new data whenever there is new data available as an InputStream. The same file is overwritten every time. Sometimes the file becomes 0 kb before it is written. The web service reads these files at regular intervals. I need to avoid the case where the file is 0 bytes.

How to do it? Will help block in this case? If the browser logs in to read a locked file, the browser will continue to show old data from the cache until the lock is released and the file is readable again.

try{
String outputFile = "output.html";     
FileWriter fWriter = new FileWriter(outputFile);
//write the data ...

fWriter .flush();


outputFile = "anotheroutput.html";     
fWriter = new FileWriter(outputFile);
//write the data ...

fWriter .flush();
fWriter.close();
}
catch(Exception e)
{
 e.prinStackTrace();
}

      

+2


source to share


4 answers


Try writing to a temporary file (on the same filesystem), and once the file has finished writing, move it into place using File.renameTo (). If the underlying filesystem supports atomic move operations (most of them), you should get the desired behavior. If you are working in windows, you will need to make sure you close the file after reading, otherwise the file transfer will fail.



public class Data
{
    private final File file;
    protected  Data(String fileName) {
        this.file = new File(filename);
    }

   /* above is in some class somehwere 
    *  then your code brings new info to the file
    */

   // 
   public synchronized accessFile(String data) {
       try {
           // Create temporary file
           String tempFilename = UUID.randomUUID().toString() + ".tmp";
           File tempFile = new File(tempFilename);

           //write the data ...
           FileWriter fWriter = new FileWriter(tempFile);
           fWriter.write(data);
           fWriter.flush();
           fWriter.close();

           // Move the new file in place
           if (!tempFile.renameTo(file)) {
               // You may want to retry if move fails?
               throw new IOException("Move Failed");
           }
       } catch(Exception e) {
           // Do something sensible with the exception.
           e.prinStackTrace();
       }
   }
}

      

+2


source


FileWriter fWriter = new FileWriter(fileName,true);

      



try using above :-)

+1


source


public class Data
{
  String fileName;
  protected  Data(String fileName)
  {
     this.fileName= fileName;
     return; // return from constructor often not needed.
  }

   /* above is in some class somehwere 
    *  then your code brings new info to the file
    */

  // 
  public synchronized accessFile(String data)
  {
    try
    {
       // File name to be class member.
       FileWriter fWriter = new FileWriter(fileName);
       //write the data ...
       fWriter.write(data);
       fWriter .flush();
       fWriter .close();
       return;
    }
    catch(Exception e)
    {
       e.prinStackTrace();
    }

      

it is not required:

 outputFile = "anotheroutput.html";     
 fWriter = new FileWriter(outputFile);
 //write the data ...

fWriter .flush();
fWriter.close();

      

because working on a file is a method of the Data class p>

0


source


Your requirement is not very clear. Do you want to write a new file names each time or do you want to add to the same file or do you want to write the same file? Anyway, all three cases are simple and you can manipulate it from the API.

If the problem is that the web service is reading a file that is not yet complete, that is, it is in the writing phase. In your webservice, you have to check if the file is read-only and only you are reading the file. In the writing phase, after the write is complete, set the file to read-only.

The 0Kb file happens because you are overwriting the same file again. Overwriting clears all data and then starts writing new content.

0


source







All Articles