我正在尝试创建一个文件,但是文件路径是通过使用一些内部变量和标签的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
答案 0 :(得分:0)
这将对您有所帮助:
String path = "D://abc" + filename+ ".txt";
System.out.println("Path--- " + path);
File file = new File(path);
file.createNewFile()
答案 1 :(得分:0)
您需要先创建文件夹(目录):
String s_path = text_dir + "/" + time_stmp + "_" + "Session" + "_" + file_name;
File ssw = new File(s_path);
ssw.getParentFile().mkdirs();
ssw.createNewFile();
答案 2 :(得分: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()
将使用默认系统路径分隔符。