代码如下:
import java.io.*;
public class IoTest {
public static void main(String[] args) throws Exception {
File parent = new File("D:");
String child ="1"+File.separator+"1.txt";
File f = new File(parent, child);
f.createNewFile();
}
}
我想在D盘创建个1文件夹,然后在里面建个1.txt文件,为什么显示:
Exception in thread "main" java.io.IOException: 系统找不到指定的路径。
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(File.java:883)
at a.IoTest.main(IoTest.java:9)
求高手讲解,感激不尽!!
------解决方案--------------------
File root = new File("D:");
File path = new File(root,"1");
if(!path.exists()) path.mkdirs();
File file = new File(path,"1.txt");
boolean success = file.createNewFile();
------解决方案--------------------
嗯,当时没注意,参照3楼 huntor的方法就可以了,调用mkdirs()