java.io.IOException可能的原因是什么:“文件名,目录名或卷标语法不正确” [英] What are possible reasons for java.io.IOException: "The filename, directory name, or volume label syntax is incorrect"
问题描述
文件targetFile =新建文件(targetPath + File.separator) +文件名);
...
targetFile.createNewFile();
fileInputStream = new FileInputStream(fileToCopy);
fileOutputStream = new FileOutputStream(targetFile);
byte [] buffer = new byte [64 * 1024];
int i = 0;
while((i = fileInputStream.read(buffer))!= -1){
fileOutputStream.write(buffer,0,i);
}
对于一些用户, targetFile.createNewFile
导致这个异常:
java.io.IOException:文件名,目录名或卷标语法是java.io.WinNTFileSystem.createFileExclusively(Native Method)上的
$ java.io.File.createNewFile(File.java:850)
文件名和目录名称似乎是正确的。在执行复制代码之前,目录 targetPath
甚至被检查,文件名如下所示: AB_timestamp.xml
用户对 targetPath
具有写入权限,可以使用操作系统复制文件而不会出现问题。
由于我无法访问机器,所以发生这种情况,并且无法在自己的机器上重现问题,我转向您提示此异常的原因。
尝试这样做,因为在目标路径和文件名之间的路径中更多地调整目录分隔符:
文件targetFile = new File(targetPath,filename);
I am trying to copy a file using the following code:
File targetFile = new File(targetPath + File.separator + filename);
...
targetFile.createNewFile();
fileInputStream = new FileInputStream(fileToCopy);
fileOutputStream = new FileOutputStream(targetFile);
byte[] buffer = new byte[64*1024];
int i = 0;
while((i = fileInputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, i);
}
For some users the targetFile.createNewFile
results in this exception:
java.io.IOException: The filename, directory name, or volume label syntax is incorrect
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(File.java:850)
Filename and directory name seem to be correct. The directory targetPath
is even checked for existence before the copy code is executed and the filename looks like this: AB_timestamp.xml
The user has write permissions to the targetPath
and can copy the file without problems using the OS.
As I don't have access to a machine this happens on yet and can't reproduce the problem on my own machine I turn to you for hints on the reason for this exception.
Try this, as it takes more care of adjusting directory separator characters in the path between targetPath and filename:
File targetFile = new File(targetPath, filename);
这篇关于java.io.IOException可能的原因是什么:“文件名,目录名或卷标语法不正确”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!