在 Java 中,新创建的文件的默认位置是什么? [英] In Java, what is the default location for newly created files?

查看:79
本文介绍了在 Java 中,新创建的文件的默认位置是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Java 中,新创建的文件的默认位置是什么?

In Java, what is the default location for newly created files?

推荐答案

如果是应用程序的当前目录.如果例如您使用

If the current directory of the application. If e.g. you create a File by using

new FileOutputStream("myfile")

然后在当前"目录下创建,可以调用

then it is created in the "current" directory, which can be retrieved by calling

System.getProperty("user.dir");

但是,如果您通过调用本机方法(非常不可能!)更改当前目录,则不会更新该属性.可以看作是应用程序的初始当前目录.

However if you change the current directory by calling native methods (very unlikely!), the property is not updated. It can be seen as the initial current directory of the application.

如果您在批处理文件中启动 Java 应用程序,并双击指向它的链接,当前目录将是批处理文件所在的目录,但这可以在链接中更改.

If you start your Java app in a batch file, and doubleclick on the link to it, the current directory will be the directory where the batch file resided, but this can be changed in the link.

如果您从命令行启动 Java 应用程序,则您已经知道所在的目录.

If you start your Java app from the command line, you already know the directory you are in.

如果您从 IDE 启动 Java 应用程序,当前目录通常是项目根目录,但这通常可以在启动配置中进行配置.

If you start your Java app from the IDE, the current directory is usually the project root, but this can usually be configured in the launch configuration.

2017 年 8 月更新:

您也可以始终使用 new File(".").getAbsolutePath() 找到当前正确的位置.

You could also always find the current correct location with new File(".").getAbsolutePath().

这篇关于在 Java 中,新创建的文件的默认位置是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆