Java Paths.get .... readAllBytes(path)) 不适用于相对路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41275278/
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
Java Paths.get .... readAllBytes(path)) not working with relative path
提问by rainer
I am new to Java and trying to build an FX application. One of my function aims at replacing certain strings with others. The script works fine as long as I define the absolute path of the target file, but breaks when I work with the relative path.
我是 Java 新手,正在尝试构建 FX 应用程序。我的功能之一旨在用其他字符串替换某些字符串。只要我定义了目标文件的绝对路径,脚本就可以正常工作,但是当我使用相对路径时会中断。
The problem is in the method "readAllBytes", that only works with the complete path. But I need relative path, since the folder location will vary.
问题出在“readAllBytes”方法中,该方法仅适用于完整路径。但我需要相对路径,因为文件夹位置会有所不同。
The target file is in the project folder. Is there any other method I can use to read the file content, that does not require the absolute path?
目标文件位于项目文件夹中。有没有其他方法可以用来读取文件内容,不需要绝对路径?
Thanks a lot in advance. Below is the snippet:
非常感谢。下面是片段:
if (checkbox.isSelected()) {
//this works .....
Path path = Paths.get("//home/../../../../Target.fxml")
Charset charset = StandardCharsets.UTF_8;
String content = new String(Files.readAllBytes(path));
content = content.replaceAll("text_old" , "text_new");
Files.write(path, content.getBytes(charset));
//this doesn't work...
Path path = Paths.get("Target.fxml");
Caused by: java.nio.file.NoSuchFileException: Target.fxml
回答by fasseg
The exception root cause java.nio.file.NoSuchFileException: Target.fxml
does mean that the file does not exist at the given location.
异常根本原因java.nio.file.NoSuchFileException: Target.fxml
确实意味着该文件在给定位置不存在。
If you're doing Paths.get("Target.fxml")
you are looking in the current working directory for the file Target.fxml
. But since the file is located in src/javafxapplication/Target.fxml
and the program is run from a different directory Target.fxml
can not be found.
如果您正在这样做,Paths.get("Target.fxml")
您将在当前工作目录中查找文件Target.fxml
. 但由于该文件位于src/javafxapplication/Target.fxml
和程序从不同的目录运行Target.fxml
无法找到。
You can check the working directory of your application using e.g.:
您可以使用以下命令检查应用程序的工作目录:
System.out.println(System.getProperty("user.dir")));
This might very well be the classes
directory. If you want to e.g. point from classes
to the src
folder you can use the following path:
这很可能是classes
目录。如果您想例如点classes
到src
文件夹,您可以使用以下路径:
Paths.get("../src/javafxapplication/Target.fxml")
This is however bad practice since the src
folder is normally not part of your distribution package.You should probably copy the Target.fxml
to another location or use Build Tools like Apache Maven to create a jar file which does include the Target.fxml
and read the contents from the jar file using ClassLoader.getResource()
.
然而,这是不好的做法,因为该src
文件夹通常不是您的分发包的一部分。您可能应该将 复制Target.fxml
到另一个位置或使用构建工具(如 Apache Maven)创建一个 jar 文件,该文件包含Target.fxml
并使用ClassLoader.getResource()
.
回答by rainer
Got it working after all, thanks to Fasseg and others who found the time and patience to look into this. Here is the final code:
终于让它工作了,感谢 Fasseg 和其他人找到了时间和耐心来研究这个问题。这是最终的代码:
Path path = Paths.get("src/javafxapplication2/PopupFXML.fxml");
Charset charset = StandardCharsets.UTF_8;
String content = new String(Files.readAllBytes(path));
content = content.replaceAll("old_text" , "new_text");
Files.write(path, content.getBytes(charset));