What are possible reasons for java.io.IOException: “The filename, directory name, or volume label syntax is incorrect”

后端 未结 13 1438
一个人的身影
一个人的身影 2020-12-10 02:12

I am trying to copy a file using the following code:

File targetFile = new File(targetPath + File.separator + filename);
...
targetFile.createNewFile();
file         


        
相关标签:
13条回答
  • 2020-12-10 02:48

    Try to create the file in a different directory - e.g. "C:\" after you made sure you have write access to that directory. If that works, the path name of the file is wrong.

    Take a look at the comment in the Exception and try to vary all the elements in the path name of the file. Experiment. Draw conclusions.

    0 讨论(0)
  • 2020-12-10 02:52

    Here is the test program I use

    import java.io.File;
    public class TestWrite {
    
        public static void main(String[] args) {
            if (args.length!=1) {
                throw new IllegalArgumentException("Expected 1 argument: dir for tmp file");
            }
            try  {
                File.createTempFile("bla",".tmp",new File(args[0]));
            } catch (Exception e) {
                System.out.println("exception:"+e);
                e.printStackTrace();
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-10 02:56

    Remove any special characters in the file/folder name in the complete path.

    0 讨论(0)
  • 2020-12-10 02:59

    A very similar error:- " ... java.io.IOException: The filename, directory name, or volume label syntax is incorrect" was generated in Eclipse for me when the TOMCAT home setting had a training backslash.

    The minor edit suggested at:- http://www.coderanch.com/t/556633/Tomcat/java-io-IOException-filename-directory fixed it for me.

    0 讨论(0)
  • 2020-12-10 03:05

    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);
    
    0 讨论(0)
  • 2020-12-10 03:05

    Do you check that the targetPath is a directory, or just that something exists with that name? (I know you say the user can copy it from the operating system, but maybe they're typing something else).

    Does targetPath end with a File.separator already?

    (It would help if you could log and tell us what the value of targetPath and filename are on a failing case)

    0 讨论(0)
提交回复
热议问题