0

我正在尝试创建一个文件,但文件路径是通过使用一些内部变量和标签的 String Concats 构造的,我收到以下错误:

Exception in thread "main" 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(Unknown Source)
    at CopyEJ.CopyEJ.main(CopyEJ.java:133)

是否有构建此类文件的标准方法?

String s_path = text_dir + "\\" + time_stmp + "_" + "Session" + "_" + file_name;


        File ssw = new File(s_path);

        ssw.createNewFile();  //Errors Out here
4

3 回答 3

0

如果您使用的是 Java 1.7,则可以将其重写如下:

Path s_path = Paths.get(text_dir, time_stmp + "_" + "Session" + "_" + file_name);
Files.createDirectories(s_path.getParent());  
File ssw = s_path.toFile();
ssw.createNewFile();

Paths.get()将使用默认系统路径分隔符。

于 2013-06-26T09:01:19.567 回答
0

这将帮助您:

String path = "D://abc" + filename+ ".txt";
System.out.println("Path--- " + path);
File file = new File(path);
file.createNewFile()
于 2013-06-26T04:57:37.590 回答
0

您需要先创建文件夹(目录):

String s_path = text_dir + "/" + time_stmp + "_" + "Session" + "_" + file_name;
File ssw = new File(s_path);

ssw.getParentFile().mkdirs();
ssw.createNewFile();
于 2013-06-26T04:57:57.040 回答