Java 如何使用相对路径而不是绝对路径?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4210239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 14:36:10  来源:igfitidea点击:

How to use relative path instead of absolute path?

javafile-iofilesystems

提问by Blackbinary

So I am having a strange issue with Java.

所以我在 Java 上遇到了一个奇怪的问题。

I'm reading a writing files, so the path is important to me. I would like all files to be written and read from the relative path (i.e in the folder with the rest of my class and java files).

我正在阅读一个写作文件,所以路径对我很重要。我希望所有文件都从相对路径中写入和读取(即在我的类和 java 文件的其余部分所在的文件夹中)。

I write files like so:

我这样写文件:

FileWriter fw = new FileWriter(outfile,true);
fw.write(data);
fw.close();

where outfile is something like 'out.txt' (i.e. the name of the file we want the output to go in).

其中 outfile 类似于“out.txt”(即我们希望输出进入的文件名)。

The problem is, the file is created in /home/me/instead of the path of my other files.

问题是,该文件是在/home/me/而不是我的其他文件的路径中创建的。

Am i doing something wrong? Or how can i get the files stored in the relative directory?

难道我做错了什么?或者如何获取存储在相对目录中的文件?

回答by Eric.K.Yung

FileWriter fw = new FileWriter("./" + outfile,true);

or

或者

FileWriter fw = new FileWriter("~/" + outfile,true);

I would try that.

我会试试的。

回答by Marcus Whybrow

Java will save files relative to the current working directory. Are you inside you home directory when you run the program?

Java 将保存相对于当前工作目录的文件。当你运行程序时,你在你的主目录中吗?

If you always want files to be saved relative to the location of the Java files and notthe current working directory, you need to find that directory and prepend it to get an absolute path.

如果您总是希望相对于 Java 文件的位置而不是当前工作目录保存文件,则需要找到该目录并在其前面添加绝对路径。

回答by oxbow_lakes

set the user.dirproperty on the command line, or just run from the directory you wnt everything to be relative to (by cd-ing)

user.dir在命令行上设置属性,或者只是从您希望所有内容都与之相关的目录中运行(通过 cd-ing)

回答by Ellis

The working directory is where you run the program from i.e. where you call java YourClass, so when you want to write files relative to the class that's executing you'll have to know the current directory and append the relative path from there e.g.

工作目录是你运行程序的地方,即你调用 java YourClass 的地方,所以当你想写相对于正在执行的类的文件时,你必须知道当前目录并从那里附加相对路径,例如

So if you have a class test.YourClass and you run the application from the source root i.e

因此,如果您有一个类 test.YourClass 并且您从源根目录运行该应用程序,即

java test.YourClass

java test.YourClass

you can find the absolute path to the directory your class file is in from the the user.dir system property and the classes package name as follows:

您可以从 user.dir 系统属性和类包名称中找到类文件所在目录的绝对路径,如下所示:

public class YourClass {

public void printPath() {
    String path = String.format("%s/%s", System.getProperty("user.dir"), this.getClass().getPackage().getName().replace(".", "/"));
    System.out.println(path);
}

}

}