用Java构造文件路径

用Java构造文件路径,java,eclipse,Java,Eclipse,我试图创建一个文件,但文件路径是通过使用一些内部变量和标签的字符串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.

我试图创建一个文件,但文件路径是通过使用一些内部变量和标签的字符串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
这将帮助您:

String path = "D://abc" + filename+ ".txt";
System.out.println("Path--- " + path);
File file = new File(path);
file.createNewFile()

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


如果您使用的是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();

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

请调试并观察s\u路径变量的路径看起来文件名、目录名或卷标语法不正确:)请使用system.getParameter(“file.separator”)和system.getParameter(“path.separator”)而不是“\\”字符串…考虑使用<代码> /< /代码>,因为它与操作系统无关,在所有操作系统上都能工作。
Path s_path = Paths.get(text_dir, time_stmp + "_" + "Session" + "_" + file_name);
Files.createDirectories(s_path.getParent());  
File ssw = s_path.toFile();
ssw.createNewFile();