仅当一个文件不存在时,如何在 Java 中创建一个文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1556127/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to create a file in Java only if one doesn't already exist?
提问by Jason S
I'm trying to implement the following operation in Java and am not sure how:
我正在尝试在 Java 中实现以下操作,但不确定如何:
/*
* write data (Data is defined in my package)
* to a file only if it does not exist, return success
*/
boolean writeData(File f, Data d)
{
FileOutputStream fos = null;
try
{
fos = atomicCreateFile(f);
if (fos != null)
{
/* write data here */
return true;
}
else
{
return false;
}
}
finally
{
fos.close(); // needs to be wrapped in an exception block
}
}
Is there a function that exists already that I can use for atomicCreateFile()
?
是否已经存在我可以使用的功能atomicCreateFile()
?
edit:Uh oh, I'm not sure that File.createNewFile() is sufficient for my needs. What if I call f.createNewFile()
and then between the time that it returns and I open the file for writing, someone else has deleted the file? Is there a way I can both create the file and open it for writing + lock it, all in one fell swoop? Do I need to worry about this?
编辑:哦,我不确定 File.createNewFile() 是否足以满足我的需要。如果我打电话f.createNewFile()
,然后在它返回和我打开文件进行写入之间,其他人删除了该文件怎么办?有没有一种方法可以同时创建文件并打开它进行写入+锁定它,一举完成?我需要担心这个吗?
采纳答案by Taylor Leese
File.createNewFile()
only creates a file if one doesn't already exist.
File.createNewFile()
仅当文件不存在时才创建文件。
EDIT: Based on your new description of wanting to lock the file after it's created you can use the java.nio.channels.FileLock
object to lock the file. There isn't a one line create and lock though like you are hoping. Also, see this SO question.
编辑:根据您在创建文件后想要锁定文件的新描述,您可以使用该java.nio.channels.FileLock
对象来锁定文件。尽管像您希望的那样,没有一行创建和锁定。另外,请参阅此 SO问题。
回答by OscarRyz
Atomicallycreates a new, empty file named by this abstract pathname if and only ifa file with this name does not yet exist. The check for the existence of the file and the creation of the file if it does not exist are a single operation that is atomic with respect to all other filesystem activities that might affect the file.
当且仅当具有此名称的文件尚不存在时,以原子方式创建以该抽象路径名命名的新空文件。检查文件是否存在以及如果文件不存在则创建文件是单个操作,对于可能影响该文件的所有其他文件系统活动而言是原子操作。
EDIT
编辑
Jason, as for your concern, if you keep reading the link we sent you there is a NOTE about that.
杰森,至于您的担忧,如果您继续阅读我们发送给您的链接,就会有关于此的注意事项。
Note:this method should not be used for file-locking, as the resulting protocol cannot be made to work reliably. The FileLockfacility should be used instead.
注意:此方法不应用于文件锁定,因为无法使生成的协议可靠地工作。的的FileLock设施应改为使用。
I think you should really read that part too:
我认为你也应该真正阅读那部分:
回答by Geo
Why can't you test using File#exists?
为什么不能使用File#exists 进行测试?
回答by cosbor11
//myFile should only be created using this method to ensure thread safety
public synchronized File getMyFile(){
File file = new File("path/to/myfile.ext");
if(!file.exists()){
file.getParentFile().mkdirs();
file.createNewFile();
}
return file;
}
回答by Stephan
Java 7 version with Files#createFile:
带有Files#createFile 的Java 7 版本:
Path out;
try {
out = Files.createFile(Paths.get("my-file.txt"));
} catch (FileAlreadyExistsException faee) {
out = Paths.get("my-file.txt");
}