Java File.createNewFile() 抛出 IOException 没有这样的文件或目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1525060/
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
File.createNewFile() thowing IOException No such file or directory
提问by A Hymanson
I have a method that writes to a log file. If the file exists it should append to it, if not then I want it to create a new file.
我有一种写入日志文件的方法。如果文件存在,它应该附加到它,如果不存在,那么我希望它创建一个新文件。
if (!file.exists() && !file.createNewFile()) {
System.err.println("Error with output file: " + outFile
+ "\nCannot create new file.");
continue;
}
I have that to check that a file can be created. file is a java.io.File object. createNewFile is throwing an IOException: No such file or directory. This method has been working perfectly since I wrote it a few weeks ago and has only recently starting doing this although I don't know what I could have changed. I have checked, the directory exists and I have write permissions for it, but then I thought it should just return false if it can't make the file for any reason.
我用它来检查是否可以创建文件。文件是一个 java.io.File 对象。createNewFile 抛出 IOException: 没有这样的文件或目录。自从我几周前写了这个方法以来,这个方法一直运行得很好,直到最近才开始这样做,虽然我不知道我可以改变什么。我已经检查过,该目录存在并且我对它有写权限,但后来我认为如果它因任何原因无法制作文件,它应该只返回 false。
Is there anything that I am missing to get this working?
有什么让我无法正常工作的吗?
采纳答案by Nicholas Jordan
normally this is something you changed recently, first off your sample code is if not file exists and not create new file - you are trying to code away something - what is it?
通常这是您最近更改的内容,首先您的示例代码是如果文件不存在并且不创建新文件 - 您正在尝试编码某些东西 - 它是什么?
Then, look at a directory listing to see if it actually exists and do a println / toString() on the file object and getMessage() on the exception, as well as print stack trace.
然后,查看目录列表以查看它是否确实存在,并对文件对象执行 println / toString() 并对异常执行 getMessage() 并打印堆栈跟踪。
Then, start from zero knowledge again and re factor from the get-go each step you are using to get here. It's probably a duh you stuck in there somewhere while conceptualizing in code ( because it was working ) - you just retrace each step in detail, you will find it.
然后,再次从零知识开始,从一开始就重新考虑您用来到达这里的每一步。这可能是你在代码中概念化时卡在某个地方的废话(因为它正在工作) - 你只需详细地回溯每一步,你就会找到它。
回答by Miserable Variable
Perhaps the directory the file is being created in doesn't exist?
也许正在创建文件的目录不存在?
回答by Thomas Jung
This could be a threading issue(checking and creating together are not atomic: !file.exists() && !file.createNewFile()
) or the "file" is already a directory.
这可能是一个线程问题(一起检查和创建不是原子性的:)!file.exists() && !file.createNewFile()
或者“文件”已经是一个目录。
Try (file.isFile()
) :
试试 ( file.isFile()
) :
if (file.exists() && !file.isFile()){
//handle directory is there
}else if(!file.createNewFile()) {
//as before
}
回答by Glen
According to the [java docs](http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html#createNewFile()) createNewFile
will create a new file atomically for you.
根据 [java docs]( http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html#createNewFile())createNewFile
将为您自动创建一个新文件。
Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist.
Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist.
Given that createNewFile
is atomic and won't over-write an existing file you can re-write your code as
鉴于这createNewFile
是原子的并且不会覆盖现有文件,您可以将代码重写为
try {
if(!file.createNewFile()) {
System.out.println("File already exists");
}
} catch (IOException ex) {
System.out.println(ex);
}
This may make any potential threading issues, race-conditions, etc, easier to spot.
这可能会使任何潜在的线程问题、竞争条件等更容易被发现。
回答by Omry Yadan
try to ensure the parent directory exists with:
尝试确保父目录存在:
file.getParentFile().mkdirs()
回答by Rakesh Juyal
You are certainly getting this Exception 'The system cannot find the path specified'
您肯定会收到此异常 “系统找不到指定的路径”
Just print 'file.getAbsoluteFile()' , this will let you know what is the file you wanted to create.
只需打印 'file.getAbsoluteFile()' ,这会让您知道要创建的文件是什么。
This exception will occur if the Directory where you are creating the file doesn't exist.
如果您创建文件的目录不存在,则会发生此异常。
回答by Rakesh Juyal
I think the exception you get is likely the result from the file check of the atomic method file.createNewFile()
. The method can't check if the file does exist because some of the parent directories do not exist or you have no permissions to access them. I would suggest this:
我认为你得到的异常很可能是原子方法的文件检查的结果file.createNewFile()
。该方法无法检查文件是否存在,因为某些父目录不存在或您无权访问它们。我建议这样做:
if (file.getParentFile() != null && !file.getParentFile().mkDirs()) {
// handle permission problems here
}
// either no parent directories there or we have created missing directories
if (file.createNewFile() || file.isFile()) {
// ready to write your content
} else {
// handle directory here
}
If you take concurrency into account, all these checks are useless because in every case some other thread is able to create, delete or do anything else with your file. In this case you have to use file locks which I would not suggest doing ;)
如果考虑并发性,所有这些检查都是无用的,因为在每种情况下,其他线程都可以创建、删除或对您的文件执行任何其他操作。在这种情况下,您必须使用我不建议这样做的文件锁;)
回答by dianakarenms
In my case was just a lack of permission:
就我而言,只是缺乏许可:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
回答by Ganesan J
//Create New File if not present
if (!file.exists()) {
file.getParentFile().mkdirs();
file.createNewFile();
Log.e(TAG, "File Created");
}