关于系统属性: user.dir

mayandbrisa 2009-05-23 11:38:14

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 中
...全文
266 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
calibure 2009-05-24
  • 打赏
  • 举报
回复
视乎file.getAbsolutePath() 和"user.dir"有关。
create 文件和运行JVM时目录有关。
感觉好像是个bug。

如果想要你预期的效果 file.getCanonicalFile().createNewFile()

62,635

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧