2

InputStream として利用可能な新しいデータがあるたびに新しいデータを生成するコードがあります。毎回同じファイルが上書きされます。ファイルが書き込まれる前に 0 kb になることがあります。Web サービスは、これらのファイルを定期的に読み取ります。ファイルが 0 バイトの場合は避ける必要があります。

どうやってこれを行うのですか?この場合、ロックは役に立ちますか? ブラウザーがロックされているファイルを読み取るためにアクセスした場合、ブラウザーは、ロックが解除されてファイルが再び読み取れるようになるまで、キャッシュから古いデータを表示し続けます。

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();
}
4

4 に答える 4

2

(同じファイル システム内の) 一時ファイルへの書き込みを試み、ファイルの書き込みが完了したら、File.renameTo() を使用してその場所に移動します。基礎となるファイル システムがアトミックな移動操作をサポートしている場合 (ほとんどの場合)、必要な動作が得られるはずです。Windows で実行している場合は、読み取り後にファイルを閉じる必要があります。そうしないと、ファイルの移動に失敗します。

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();
       }
   }
}
于 2009-10-24T06:59:45.750 に答える
1
FileWriter fWriter = new FileWriter(fileName,true);

上記を使用してみてください:-)

于 2010-12-18T09:36:56.553 に答える
0
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();
    }

これは必要ありません:

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

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

これは、ファイルに対する作業がクラス Data のメソッドであるためです。

于 2009-10-24T02:41:08.347 に答える
0

あなたの要件はあまり明確ではありません。毎回新しい名前のファイルを書きたいですか、それとも同じファイルに追加したいですか、それとも同じファイルを上書きしたいですか? とにかく、3 つのケースはすべて簡単で、API から管理できます。

問題が、Web サービスがまだ完了していないファイルを読み取っている、つまり書き込み段階にある場合です。Web サービスでは、ファイルが読み取り専用かどうかを確認してから、ファイルを読み取る必要があります。書き込みフェーズでは、書き込みが終了したら、ファイルを読み取り専用に設定します。

同じファイルを再度上書きしているため、0Kb ファイルが発生します。上書きすると、すべてのデータがクリーンアップされてから、新しいコンテンツの書き込みが開始されます。

于 2009-10-24T02:47:46.040 に答える