java. io. What is the possible cause of IOException: “incorrect file name, directory name or volume label syntax”

I tried 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,i);
}

For some users, targetfile Createnewfile caused 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)

The file name and directory name seem to be correct Before executing the copy code, the directory targetpath is even checked to exist, and the file name is as follows: ab_ timestamp. xml

The user has write permission to targetpath and can use the operating system to copy files without problems

This happens because I can't access the machine, and I can't reproduce the problem on my own machine. I turn to you to prompt the cause of this exception

Solution

Try this because you need to adjust the directory separator more in the path between the target path and the file name:

File targetFile = new File(targetPath,filename);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>