import java.io.*;
public class Test3 {
public static void main (String [] args) throws Exception{
String str = "E:/Test/IO/Copy.java";//"IO/Copy.java"
File fi = new File(str);
boolean b = fi.isDirectory();
boolean b1 = fi.isFile();
System.out.println(str);
System.out.println(b);
System.out.println(b1);
}
}
这个结果 我能明白
E:/Test/IO/Copy.java
false
true这个结果就不明白了 请解释下
IO/Copy.java
true
false
public class Test3 {
public static void main (String [] args) throws Exception{
String str = "E:/Test/IO/Copy.java";//"IO/Copy.java"
File fi = new File(str);
boolean b = fi.isDirectory();
boolean b1 = fi.isFile();
System.out.println(str);
System.out.println(b);
System.out.println(b1);
}
}
这个结果 我能明白
E:/Test/IO/Copy.java
false
true这个结果就不明白了 请解释下
IO/Copy.java
true
false
那么,楼主要找到程序的当前路径才可以。
也就是说,程序所认为的当前路径,很可能不是“E:/Test/”。
楼主可以通过System.out.println(new File("").getAbsolutePath());
查看当前路径是什么,然后,添加适当的相对路径,就能得到第一个的结果了。
String str = "E:/Test/IO/Copy.java";//这样写能找得到。
String str = "IO/Copy.java"//这样写肯定找不到。这样写的最终路径是,classpath+/io/Copy.java