ged*_*edO 6 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
问题是getResourceAsStream()正在解析相对于类路径的路径,而new FileOutputStream()直接在文件系统中创建文件.他们有不同的起点.
通常,您无法回写加载资源的源位置,因为它可能根本不存在于文件系统中.例如,它可能位于jar文件中,JVM不会更新jar文件.
也许它有效
try
{
java.net.URL url = this.getClass().getResource("/resources/SQL.properties");
java.io.FileInputStream pin = new java.io.FileInputStream(url.getFile());
java.util.Properties props = new java.util.Properties();
props.load(pin);
}
catch(Exception ex)
{
ex.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
并检查以下网址
getResourceAsStream() 与 FileInputStream
| 归档时间: |
|
| 查看次数: |
10101 次 |
| 最近记录: |