我需要在一个临时文件中写入一些数据,这个文件存储在一个目录答:我用File.createTempFile方法来做到这一点。但是,有一个线程会定期轮询目录A以检查是否有临时文件需要处理。设置文件不可读
// create a temporary file that will contain the data
newTmpFile = File.createTempFile("prefix", recoverFileExt, new File(
recoverDirectory));
// the file is set to non readable, so the recovery thread cannot
// access it
newTmpFile.setReadable(false);
//write data into the file
// the file is written, it is set to readable so the recovery thread
// can now access it
newTmpFile.setReadable(true);
的问题是,我不想做写入操作之前,恢复线程访问该文件。所以,我使用这种机制:我创建该文件,将其设置为不可读,写入,然后将其设置为可读并关闭它。问题是,在文件创建后,文件仍然可读,线程可以访问它。
所以,我想知道是否有设置该文件作为非可读在其创建的可能性,或者如果你有其他的解决方案。
感谢
另请注意,如果目录位于同一文件系统上,则可以在目录之间快速重命名文件。 (这不是NFS推荐的,但NFS并不真正用于“数据完整性”。) –
我接受其设施的答案。但是,我知道使用文件名进行同步不是一个好习惯。 –