简体   繁体   English

使用getResourceAsStream()在项目文件夹之外的文件的绝对路径

[英]Absolute path to file outside of project folder with getResourceAsStream()

I want to create jar which will read txt fom jar and then save txt file to user.home folder. 我想创建将读取txt fom jar的jar,然后将txt文件保存到user.home文件夹。 When it is runned again, it will read file from user.home. 再次运行时,它将从user.home中读取文件。

I read file like this: 我这样读取文件:

if(getClass().getResourceAsStream("/"+System.getProperty("user.home")+"/"+file_name) == null){
            configStream = getClass().getResourceAsStream(file_name);
        } else {
            configStream = getClass().getResourceAsStream(System.getProperty("user.home")+ "/"+file_name);
        }
        BufferedReader br = new BufferedReader(new InputStreamReader(configStream));

And then I write to file like this: 然后我这样写到文件:

try {
        BufferedWriter out = new BufferedWriter(new FileWriter(System.getProperty("user.home")+ "/" + file_name));
        for (int j = 0; j < y; j++) {
            for (int i = 0; i < x; i++) {
                if (((Block) (listArray.get(i).get(j))).getState() == blockState.blank) {
                    out.write("0");
                } else if (((Block) (listArray.get(i).get(j))).getState() == blockState.solid) {
                    out.write("1");
                } else if (((Block) (listArray.get(i).get(j))).getState() == blockState.player) {
                    out.write("I");
                } else if (((Block) (listArray.get(i).get(j))).getState() == blockState.spikes) {
                    out.write("^");
                } else if (((Block) (listArray.get(i).get(j))).getState() == blockState.water) {
                    out.write("~");
                } else if (((Block) (listArray.get(i).get(j))).getState() == blockState.transparent) {
                    out.write("T");
                }
            }
            out.write("\n");
        }
        out.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

I don't know why, but program never read file from user.home, it always reads one from project folder. 我不知道为什么,但是程序从不从user.home读取文件,它总是从项目文件夹中读取文件。 Where am I making a mistake? 我在哪里出错? Thanks 谢谢

You cannot use getResourceAsStream on locations that are not within the JVM classpath. 您不能在JVM类路径之外的位置上使用getResourceAsStream The following snippet works because somewhere within your JVM classpath the file_name exists. 以下代码段有效,因为JVM类路径中的file_name存在。

getClass().getResourceAsStream(file_name);

What you are attempting to do here in the next snippet is to use the current class's classloader to load a file that may or may not be part of the JVM classpath. 在下一个代码段中,您要尝试使用的是当前类的类加载器来加载文件,该文件可能是也可能不是JVM类路径的一部分。

configStream = getClass().getResourceAsStream(System.getProperty("user.home")+ "/"+file_name);

The relative root node from which this path will be traversed can also not be controller. 将要遍历此路径的相对根节点也不能是控制器。 Use a FileInputStream and load the file from an absolute path like /usr/local/file_name instead. 使用FileInputStream并从绝对路径(例如/usr/local/file_name加载文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM