java io 文件路径问题首先我写出代码:public class test {public static void main(String[]args) throws
java io 文件路径问题 首先我写出代码:
public class test { public static void main(String[]args) throws Exception{ File file = new File("d.txt");//错误的 //File file = new File("H:\\eclipse\\workspace\\Socket\\d.txt");//正确的 System.out.println(file.getAbsolutePath()); if(!file.getParentFile().exists()){ file.getParentFile().mkdirs(); }else{ System.out.println("oh,yse !"); } } }
这个代码,存在于H:\\eclipse\\workspace\\Socket文件夹中, 为什么File file = new File("d.txt");就报错, File file = new File("H:\\eclipse\\workspace\\Socket\\d.txt");就正确? 不是说,直接写文件名就默认所在工程的路径么? 那么,为什么File file = new File("d.txt");却父类文件getParentFile()为null? 而写全路径H:\\eclipse\\workspace\\Socket\\d.txt就有了? ------------- 注:我用的是Eclipse [解决办法] File file = new File("d.txt");//错误的
[解决办法] new File("").getPath()看看当前目录绝对路径。把d.txt放到此目录下试试。 [解决办法]
首先我写出代码:
public class test { public static void main(String[]args) throws Exception{ File file = new File("d.txt");//错误的 //File file = new File("H:\\eclipse\\workspace\\Socket\\d.txt");//正确的 System.out.println(file.getAbsolutePath()); if(!file.getParentFile().exists()){ file.getParentFile().mkdirs(); }else{ System.out.println("oh,yse !"); } } }