java.nio.file.NoSuchFileException:为什么 nio 不创建文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30908816/
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
java.nio.file.NoSuchFileException: why nio not creating file
提问by Raakh
I am using java.nio.file package and tried to create file with the following code.
我正在使用 java.nio.file 包并尝试使用以下代码创建文件。
private static void printReport(String filename, String str)throws Exception{
ErrorCheck ec = new ErrorCheck();
String fileName = "/var/Emails_log/"+filename;
Path filePath = Paths.get(fileName);
File file = new File(fileName);
final BufferedWriter out = Files.newBufferedWriter(filePath, StandardCharsets.UTF_8, StandardOpenOption.APPEND);
try{
final Path tmp = filePath.getParent();
if (tmp != null){ // null will be returned if the path has no parent
Files.createDirectories(tmp);
}
else{
out.write(str);
out.write('\n');
}
}catch (Exception e){
ec.errorMsg("ERROR: GSW.SendEmail.Exception =>",e);
}
finally {
if (out != null) {
out.flush();
out.close();
}
}
}
This throws following Exception:
这会引发以下异常:
java.nio.file.NoSuchFileException: /var/Emails_log/GSWvalidSentAddresses.txt
at sun.nio.fs.UnixException.translateToIOException(UnixException.java:86)
at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102)
at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107)
at sun.nio.fs.UnixFileSystemProvider.newByteChannel(UnixFileSystemProvider.java:214)
at java.nio.file.spi.FileSystemProvider.newOutputStream(FileSystemProvider.java:430)
at java.nio.file.Files.newOutputStream(Files.java:170)
at java.nio.file.Files.newBufferedWriter(Files.java:2720)
at SendEmail.printReport(SendEmail.java:114) SendEmail.send(SendEmail.java:87)
My question is why file is not created?
我的问题是为什么没有创建文件?
Please advise
请指教
Thanks in anticipation
感谢期待
采纳答案by T.J. Crowder
Updated Answer:
更新答案:
Now that you've shown the full code, there are two major problems:
现在您已经展示了完整的代码,有两个主要问题:
You're trying to open the file before ensuring that the directories leading up to it exist, and
You're using
StandardOpenOption.APPEND
, but that won't createa file; it will append to an existingfile.
您尝试在确保文件所在的目录存在之前打开该文件,并且
您正在使用
StandardOpenOption.APPEND
,但这不会创建文件;它将附加到现有文件。
...along with a large number of issues around best practices relative to the number of lines of actual code.
...以及与实际代码行数相关的最佳实践的大量问题。
See comments:
看评论:
private static void printReport(String filename, String str) throws Exception /* No, it doesn't. The only calls you had outside your catch-all `try` don't throw exceptions. */ {
ErrorCheck ec = new ErrorCheck(); // Recommend not creating this until/unless you need it
String fileName = "/var/Emails_log/"+filename; // VERY poor practice having two locals that only differ by the capitalization of one character in the middle (`filename` and `fileName`)
Path filePath = Paths.get(fileName);
// File file = new File(fileName); <== Removed, since you never use it for anything
try {
// Make sure the directories exist
Files.createDirectories(filePath.getParent()); // No need for your null check, so I removed it; based on `fileName`, it will always have a parent
// Open the file, creating it if it doesn't exist
try (
final BufferedWriter out = Files.newBufferedWriter(
filePath,
StandardCharsets.UTF_8,
StandardOpenOption.CREATE,
StandardOpenOption.APPEND)
) {
// Write to out here
}
} catch (Exception e) {
// Log-and-continue isn't generally best practice; and if you're going to do it
ec.errorMsg("ERROR: GSW.SendEmail.Exception =>",e); // <== Um...send mail failed? This isn't sending mail, it's putting something in a file.
}
// Using the try-with-resources, we don't have to worry about the flush and close calls
}
But here's how I would suggest you write it:
但这是我建议你写的方式:
private static void printReport(String filename, String str) throws IOException {
Path filePath = Paths.get("/var/Emails_log/" + filename);
// Make sure the directories exist
Files.createDirectories(filePath.getParent());
// Open the file, creating it if it doesn't exist
try (
final BufferedWriter out = Files.newBufferedWriter(
filePath,
StandardCharsets.UTF_8,
StandardOpenOption.CREATE,
StandardOpenOption.APPEND)
) {
// Write to out here, perhaps outputting `str`?
}
}
...and handle exceptions in the calling layer. Note that, again, because we're using try-with-resources
, the close
is automatic (both when there's an exception and when there isn't).
...并在调用层处理异常。请再次注意,因为我们使用try-with-resources
的close
是,是自动的(无论是在有异常时还是没有异常时)。
Or if you really want to do log-and-continue:
或者,如果你真的想做日志并继续:
private static void printReport(String filename, String str) {
try {
Path filePath = Paths.get("/var/Emails_log/" + filename);
// Make sure the directories exist
Files.createDirectories(filePath.getParent());
// Open the file, creating it if it doesn't exist
try (
final BufferedWriter out = Files.newBufferedWriter(
filePath,
StandardCharsets.UTF_8,
StandardOpenOption.CREATE,
StandardOpenOption.APPEND)
) {
// Write to out here, perhaps outputting `str`?
}
}
catch (Exception e) {
new ErrorCheck().errorMsg("ERROR: GSW.SendEmail.Exception =>", e); // <== But surely this message is suspect? I don't see anything sending email here.
}
}
Original Answer:
原答案:
You haven't shown the code that's actually failing, which is a call to newBufferedWriter
(thisone, or thisone). newBufferedWriter
takes OpenOption
s, the standard set of which are available from StandardOpenOption
. Make sure you've specified StandardOpenOption.CREATE
or StandardOpenOption.CREATE_NEW
,
您还没有显示实际失败的代码,这是对newBufferedWriter
(this或this)的调用。newBufferedWriter
需要OpenOption
s,其标准集可从StandardOpenOption
. 确保您已指定StandardOpenOption.CREATE
或StandardOpenOption.CREATE_NEW
,
If you use one of those flags, and the code in your question isbeforethe newBufferedWriter
call, barring some other problem (permissions) it should work:
如果您使用这些标志之一,并在你的问题的代码是之前的newBufferedWriter
电话,除了一些其他问题(权限),它应该工作:
String fileName = "/var/Emails_log/"+filename;
Path filePath = Paths.get(fileName);
final Path tmp = filePath.getParent();
if (tmp != null) { // <== Note: There's no point to this check, given
// your filename above, the path WILL have a parent.
// You could remove the `if` and just use
// `Files.createDirectories(tmp)` unless the `fileName`
// is actually coming from somewhere else and so could
// be a root (roots don't have parents)
Files.createDirectories(tmp);
}
else {
out.write(str); // I assume this is for logging or display?
out.write('\n'); // Specifically, that it's *not* trying to write
// to the file you're trying to create.
}
try (BufferedWriter writer = Files.newBufferedWriter(filePath, StandardOpenOption.CREATE)) {
// ------------------------------------------------------------^^^^^^^^^^^^^^^^^^^^^^^^^^
// Write to the file here
}