以下两种创建文件的方法有什么区别?
new File(System.getProperty("user.dir"));
new File("");
Run Code Online (Sandbox Code Playgroud)
Java将第一个标识为目录,第二个标识既不是文件也不是目录!为什么会这样?
码:
public class MainClass {
public static void main(String[] args) throws Exception {
System.out.println("File Created with CurrentDir taken From System Props");
File f1 = new File(System.getProperty("user.dir"));
System.out.println("Absolute Path: " + f1.getAbsolutePath());
System.out.println("isDirectory: " + f1.isDirectory());
System.out.println("isFile: " + f1.isFile());
System.out.println();
System.out.println("File Created with Empty String Path");
File f2 = new File("");
System.out.println("Absolute Path: " + f2.getAbsolutePath());
System.out.println("isdirectory: " + f2.isDirectory());
System.out.println("isFile: " + f2.isFile());
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
File Created with CurrentDir taken …Run Code Online (Sandbox Code Playgroud)