Java Paths.get .... readAllBytes(path))不使用相对路径 [英] Java Paths.get .... readAllBytes(path)) not working with relative path

查看:305
本文介绍了Java Paths.get .... readAllBytes(path))不使用相对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java新手并尝试构建FX应用程序。我的一个功能旨在用其他字符串替换某些字符串。只要我定义目标文件的绝对路径,脚本就可以正常工作,但是当我使用相对路径时,脚本会中断。

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.

问题在于readAllBytes方法,它只适用于完整路径。但我需要相对路径,因为文件夹位置会有所不同。

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.

目标文件位于项目文件夹中。有没有其他方法可以用来读取文件内容,这不需要绝对路径?

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


推荐答案

异常根本原因 java.nio.file.NoSuchFileException:Target.fxml 确实意味着该文件没有存在于给定位置。

The exception root cause java.nio.file.NoSuchFileException: Target.fxml does mean that the file does not exist at the given location.

如果你正在做 Paths.get(Target.fxml)你正在查找当前工作目录中的文件 Target.fxml 。但由于该文件位于 src / javafxapplication / Target.fxml 中,程序从另一个目录运行 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.

您可以使用以下方式检查申请表的工作目录:

You can check the working directory of your application using e.g.:

System.out.println(System.getProperty("user.dir")));

这可能是目录。如果你想要,例如从指向 src 文件夹,您可以使用以下路径:

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:

Paths.get("../src/javafxapplication/Target.fxml")

这是不好的做法,因为 src 文件夹通常不属于您的分发包。你应该可能将 Target.fxml 复制到另一个位置或使用像Apache Maven这样的构建工具来创建一个包含 Target.fxml 并使用 ClassLoader.getResource()从jar文件中读取内容。

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().

这篇关于Java Paths.get .... readAllBytes(path))不使用相对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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