Java (Linux) 中的文件路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1722730/
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-12 21:53:32  来源:igfitidea点击:

File paths in Java (Linux)

javalinuxfile-io

提问by craftsman

I have created a Java application that loads some configurations from a file conf.propertieswhich is placed in src/folder.

我创建了一个 Java 应用程序,它从conf.properties放置在src/文件夹中的文件加载一些配置。

When I run this application on Windows, it works perfectly. However when I try to run it on Linux, it throws this error:

当我在 Windows 上运行此应用程序时,它运行良好。但是,当我尝试在 Linux 上运行它时,它会引发此错误:

java.io.FileNotFoundException: src/conf.properties (No such file or directory)

采纳答案by Bj?rn

If you've packaged your application to a jar file, which in turn contains the properties file, you should use the method below. This is the standard way when distributing Java-programs.

如果您已将应用程序打包为 jar 文件,该文件又包含属性文件,则应使用以下方法。这是分发 Java 程序时的标准方式。

URL pUrl = this.getClass().getResource("/path/in/jar/to/file.properties");

Properties p = new Properties();
p.load(pUrl.openStream());

The / in the path points to the root directory in the jar file.

路径中的 / 指向 jar 文件中的根目录。

回答by Myles

I would check your slashes, windows often uses '\' vs linux's '/' for file paths.

我会检查您的斜杠,Windows 通常使用 '\' 与 linux 的 '/' 作为文件路径。

EDIT: Since your path looks fine, maybe file permissions or executing path of the app is different?

编辑:由于您的路径看起来不错,也许应用程序的文件权限或执行路径不同?

回答by Woot4Moo

Try the double slash, after doing things in JBoss I often had to refactor my code to use the double slashes

试试双斜线,在JBoss中做完之后,我经常不得不重构我的代码才能使用双斜线

回答by dustmachine

What you want to do is check out System.getProperties()and look for file.separator. The static File.pathSepratorwill also get you there.

你想要做的是检查System.getProperties()并寻找file.separator。静电File.pathSeprator也会让你到达那里。

This will allow you to build a path that is native for whatever system you're running on.

这将允许您为正在运行的任何系统构建一个本机路径。

(If indeed that is the problem. Sometimes I like to get the current directory just to make sure the directory I thinkI'm running in is the directory I'm reallyrunning in.)

(如果确实是这个问题。有时我喜欢获取当前目录只是为了确保我认为我正在运行的目录是我真正运行的目录。)

回答by ypnos

You should check the working directory of your application. Perhaps it is not the one you assume and that's why 'src' directory is not present.

您应该检查应用程序的工作目录。也许它不是您假设的那个,这就是“src”目录不存在的原因。

An easy check for this is to try the absolute path (only for debugging!).

一个简单的检查是尝试绝对路径(仅用于调试!)。

回答by Andrzej Doyle

Check your permissions. If you (or rather, the user that the Java process is running under) doesn't have appropriate permissions to read the file, for example, you would get this error message.

检查您的权限。例如,如果您(或者更确切地说,运行 Java 进程的用户)没有读取文件的适当权限,您将收到此错误消息。

This is a typical Windows -> Linux migration problem. What does ls -l src/conf.propertiesshow when run from a prompt?

这是一个典型的 Windows -> Linux 迁移问题。ls -l src/conf.properties从提示符运行时显示什么?

Additionally, check capitalisation. Windows isn't case-sensitive, so if the file was actually called e.g. CONF.properties it would still be found, whereas the two would be considered different files on Linux.

此外,检查大小写。Windows 不区分大小写,因此如果文件实际上被称为例如 CONF.properties,它仍然会被找到,而这两者在 Linux 上将被视为不同的文件。

回答by Jay R.

I would also check what your current working directory is if your path to that file is relative. You just need to make a File test = new File(".");and then print that files canonical path name.

如果该文件的路径是相对的,我还会检查您当前的工作目录是什么。您只需要制作一个File test = new File(".");然后打印该文件的规范路径名。

If you are referencing any other locations like user.diror something to that effect by using System.getProperty(), you'll want to at least verify that the directory you are using as the relative root is where you think it is.

如果您user.dir通过使用 System.getProperty()引用任何其他类似或类似的位置,您至少需要验证您用作相对根的目录是否是您认为的位置。

Also, as Mylesnoted, check the slashes used as file path separators. Although you can always use the "/" and it works.

此外,正如Myles 所指出的,检查用作文件路径分隔符的斜杠。尽管您始终可以使用“/”并且它可以工作。

And if you are referencing the path absolutely, you'll have trouble going between one OS and another if you do something silly like hard-code the locations.

如果您绝对引用路径,那么如果您执行诸如对位置进行硬编码之类的愚蠢操作,您将无法在一个操作系统和另一个操作系统之间切换。

回答by JuanZe

Instead of

代替

String PROP_FILENAME="src/conf.properties";

use

String PROP_FILENAME="src" + File.separator + "conf.properties";

Check the API for more detail: http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html

检查 API 以获取更多详细信息:http: //java.sun.com/j2se/1.5.0/docs/api/java/io/File.html

回答by anish

check your slashes and colons in my case i set my PS1 to following value

在我的情况下检查您的斜杠和冒号,我将 PS1 设置为以下值

PS1='\n[\e[1;32m]$SYSNAME(\u)@[\e[1;33m]\w [\e[1;36m](\d \T) [!]\e[0m]\n\$ '

PS1='\n[\e[1;32m]$SYSNAME(\u)@[\e[1;33m]\w [\e[1;36m](\d \T) [!]\e[ 0m]\n\$ '

i am trying to read from the env .such as system.getenv

我正在尝试从 env 中读取。例如 system.getenv

Java was throwing exception

Java抛出异常

java.lang.IllegalArgumentException: Malformed \uxxxx encoding

java.lang.IllegalArgumentException: 格式错误的 \uxxxx 编码