相关疑难解决方法(0)

getResourceAsStream()vs FileInputStream

我试图在webapp中加载一个文件,FileNotFound当我使用时我得到了一个例外FileInputStream.但是,使用相同的路径,我能够在我这样做时加载文件getResourceAsStream().这两种方法之间有什么区别,为什么一种方法有效而另一方方法无效?

java file-io web-applications fileinputstream

168
推荐指数
5
解决办法
19万
查看次数

JAVA:FileInputStream和FileOutputStream

我对输入和输出流有这个奇怪的事情,我只是无法理解.我使用inputstream从这样的资源中读取属性文件:

Properties prop = new Properties();
InputStream in = getClass().getResourceAsStream( "/resources/SQL.properties" );
rop.load(in);
return prop;
Run Code Online (Sandbox Code Playgroud)

它找到我的文件并成功更新它.我尝试写这样的修改设置:

prop.store(new FileOutputStream( "/resources/SQL.properties" ), null);
Run Code Online (Sandbox Code Playgroud)

我收到奇怪的错误:

java.io.FileNotFoundException: \resources\SQL.properties (The system cannot find the path specified)
Run Code Online (Sandbox Code Playgroud)

那么为什么改变物业的道路呢?如何解决这个问题?我在Windows上使用Netbeans

java fileinputstream fileoutputstream

6
推荐指数
2
解决办法
1万
查看次数

写入属性文件不起作用

我想写入*.properties文件.多数民众赞成我的代码如何做到这一点:

    properties = loadProperties("user.properties");//loads the properties file
    properties.setProperty(username, password);
        try {
                properties.store(new FileOutputStream("user.properties"), null);
                System.out.println("Wrote to propteries file!" + username + " " + password);
Run Code Online (Sandbox Code Playgroud)

我没有得到异常,但我也没有将输出写入文件.

这也是我的文件结构:

在此输入图像描述

我感谢你的回答!!!

UPDATE

我加载我的属性文件:

    InputStream in = ClassLoader.getSystemResourceAsStream(filename);
Run Code Online (Sandbox Code Playgroud)

我的问题是,如何从特定路径加载它?

这是我的"新"文件结构:

在此输入图像描述

java eclipse properties file

5
推荐指数
1
解决办法
3348
查看次数