日期:2014-05-20  浏览次数:21352 次

新手求助 File(File parent, String child)和separator分隔符的使用
代码如下:
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)
求高手讲解,感激不尽!!

------解决方案--------------------
Java code
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()
探讨
引用:

改成下面的就可以了
Java code

import java.io.*;
public class IoTest {
public static void main(String[] args) throws Exception {
File parent = new File("D:");
String child =Fil……