问题描述
在文件读写的时报了两个错
java.io.IOException: 系统找不到指定的路径。
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(File.java:56)
java.io.FileNotFoundException: F:\workplace.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\4\4\3.xml (系统找不到指定的路径。)
String path = "c:/test2/34/2.txt";
File file = new File(path);
System.out.println(file.getParent());
System.out.println("dd" + file.exists());
//添加这段代码
if (file.getParent() != null && !new
File(file.getParent()).exists()) {
System.out.println("mkdirs");
new File(file.getParent()).mkdirs();
}
//到这
boolean createNewFile;
try {
createNewFile = file.createNewFile();
System.out.println("createNewFile" + createNewFile);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("dd1" + file.exists());
if (!file.isFile()) {
System.out.println("文检创建失败");
}
try {
new FileOutputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}