Java文件编写器覆盖

Java文件编写器覆盖,java,synchronization,overwrite,filewriter,Java,Synchronization,Overwrite,Filewriter,我有一段代码,每当有新数据作为InputStream可用时,它就会生成新数据。每次都会覆盖相同的文件。有时文件在写入之前会变成0 kb。Web服务定期读取这些文件。我需要避免文件为0字节的情况 它是如何做到这一点的?锁在这种情况下有用吗?如果浏览器进入以读取已锁定的文件,浏览器是否会继续显示缓存中的旧数据,直到解除锁定并可再次读取文件 try{ String outputFile = "output.html"; FileWriter fWriter = new FileWriter(

我有一段代码,每当有新数据作为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();
}
这是不需要的:

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

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

这是因为处理文件是类数据的一种方法

您的要求不是很清楚。是否每次都要写入一个新名称文件,还是要附加到同一个文件,还是要重写同一个文件?无论如何,这三种情况都很简单,通过API您可以管理它

如果问题是web服务正在读取尚未完成的文件,即处于写入阶段。在web服务中,您应该检查文件是否为只读,然后只有您读取该文件。在写入阶段,一旦写入完成,将文件设置为只读


发生0Kb文件是因为您再次覆盖同一文件。覆盖将清除所有数据,然后开始写入新内容。

尝试写入临时文件(在同一文件系统中),一旦文件写入完成,使用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();
       }
   }
}

尝试使用上面的:-)

!?假设所有访问该文件的程序都在同一个JVM中,因此“synchronized”关键字可以防止多个例程同时使用该文件。JVM之外的进程呢?在电脑之外?(如果文件是通过网络共享的)Jason,我想做点什么,让op想到将文件展平的概念(我不知道它叫什么,我一直这么做),通过填充复制粘贴缓冲区并展开循环来排序代码,我会得到sync()op注意到后锁定-我现在正忙于JCE,在op处理零长度无事可做的问题时留在他身边。。。这是另一个像你在我的。。。嘿,网站所有者->我们能在所有繁重的脚本中得到一个代码编辑器吗???。。。。。真的,真的会有帮助(非常好!)我想你正在寻找
File.length()
。此方法返回以字节为单位的文件大小。
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();
       }
   }
}
FileWriter fWriter = new FileWriter(fileName,true);