public static void main(String[] args) throws IOException {
System.out.println(System.getProperty("user.dir"));//这里输出 D:\workspace
System.setProperty("user.dir","D:/temp");
System.out.println(System.getProperty("user.dir"));//这里输出 D:\temp
File file = new File("HELLO.txt");
System.out.println(file.getAbsolutePath()); //这里输出 D:\temp\HELLO.txt
System.out.println(file.createNewFile()); //这里输出 true
System.out.println(file.getAbsolutePath()); //这里输出 D:\temp\HELLO.txt
}
以上代码为什么最后的文件还是创建在了 D:\workspace 中
create 文件和运行JVM时目录有关。
感觉好像是个bug。如果想要你预期的效果 file.getCanonicalFile().createNewFile()