如何在java中指定文件路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1693020/
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
How to specify filepath in java?
提问by Yatendra Goel
I have created a java application for "Debian Linux." Now I want that that application reads a file placed in the directory where the jar file of that application is specified. So what to specify at the argument of the File Object?
我为“Debian Linux”创建了一个 Java 应用程序。现在我希望该应用程序读取放置在指定该应用程序的 jar 文件的目录中的文件。那么在文件对象的参数中指定什么?
File fileToBeReaded = new File(...);
What to specify as argument for the above statement to specify relative filepath representing the path where the jar file of the application has been placed?
要指定什么作为上述语句的参数来指定表示应用程序 jar 文件所在路径的相对文件路径?
采纳答案by Carl Smotricz
If you know the name of the file, of course it's simply
如果你知道文件名,当然是简单的
new File("./myFileName")
If you don't know the name, you can use the File object's list() method to get a list of files in the current directory, and then pick the one you want.
如果您不知道名称,可以使用 File 对象的 list() 方法获取当前目录中的文件列表,然后选择您想要的文件。
回答by Geo
I think this should do the trick:
我认为这应该可以解决问题:
File starting = new File(System.getProperty("user.dir"));
File fileToBeRead = new File(starting,"my_file.txt");
This way, the file will be searched in the user.dir
property, which will be your app's working directory.
这样,将在user.dir
属性中搜索该文件,该属性将是您的应用程序的工作目录。
回答by tangens
You could ask your classloader to give you the location of the jar:
你可以让你的类加载器给你 jar 的位置:
getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
...but I'd suggest to put the file you are looking for inside your jar file and read it as a resource (getClass().getResourceAsStream( "myFile.txt" )
).
...但我建议将您要查找的文件放在 jar 文件中,并将其作为资源 ( getClass().getResourceAsStream( "myFile.txt" )
)读取。
回答by BalusC
Using relative paths in java.io.File
is fully dependent on the current working directory. This differs with the way you execute the JAR. If you're for example in /foo
and you execute the JAR by java -jar /bar/jar/Bar.jar
then the working directory is still /foo
. But if you cd
to /bar/jar
and execute java -jar Bar.jar
then the working directory is /bar/jar
.
使用相对路径java.io.File
完全依赖于当前的工作目录。这与您执行 JAR 的方式不同。例如,如果您在其中/foo
执行 JAR,java -jar /bar/jar/Bar.jar
那么工作目录仍然是/foo
. 但是如果你cd
要/bar/jar
执行java -jar Bar.jar
然后工作目录是/bar/jar
.
If you want the root path where the JAR is located, one of the ways would be:
如果您想要 JAR 所在的根路径,其中一种方法是:
File root = new File(Thread.currentThread().getContextClassLoader().getResource("").toURI());
This returns the root path of the JAR file (i.o.w. the classpath root). If you place your resource relative to the classpath root, you can access it as follows:
这将返回 JAR 文件的根路径(类路径根)。如果将资源相对于类路径根放置,则可以按如下方式访问它:
File resource = new File(root, "filename.ext");
Alternatively you can also just use:
或者,您也可以使用:
File resource = new File(Thread.currentThread().getContextClassLoader().getResource("filename.ext").toURI());
回答by Brendan
Are you asking about escape character issues?
您是在询问转义字符问题吗?
If that is the case then use forward slashes instead of backward slashes like
如果是这种情况,则使用正斜杠而不是像这样的反斜杠
"C:/Users/You/Desktop/test.txt"
"C:/Users/You/Desktop/test.txt"
instead of
代替
"C:\Users\You\Desktop\test.txt"
"C:\Users\You\Desktop\test.txt"