`
hanhan8020
  • 浏览: 45669 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类
最新评论

getResourceAsStream() vs FileInputStream

阅读更多

I was trying to load a file in a webapp, and I was getting a FileNotFound exception when I used FileInputStream. However, using the same path, I was able to load the file when I did getResourceAsStream(). What is the difference between the two methods, and why does one work while the other doesn't?


solution:

The java.io.File and consorts acts on the local disk file system. The root cause of your problem is that relative  paths in java.io are dependent on the current working directory. I.e. the directory from which the JVM (in your case: the webserver) is started. This may for example be C:/Tomcat/bin  or something entirely different, but thus not C:/Tomcat/webapps/contextname  or whatever you'd expect it to be. The working directory is in no way programmatically controllable. You should never use relative paths with them. Always use absolute paths. E.g. C:/full/path/to/file.ext.

You don't want to hardcode or guess the absolute path in Java (web)applications. That's only portability trouble. The normal practice is to place those kind of resoures in the classpath (or to add its full path to the classpath) and use ClassLoader#getResource() or ClassLoader#getResourceAsStream() instead. It is able to locate files relative to the "root" of the classpath, as you by coincidence figured out. In webapplications (or any other application which uses multiple classloaders) it's recommend to use the ClassLoader as returned by Thread.currentThread().getContextClassLoader() for this.

Another alternative in webapps is the ServletContext#getResource() and its counterpart ServletContext#getResourceAsStream(). It is able to access files located in the public web folder of the webapp project, including the /WEB-INF folder. The ServletContext is available in servlets by the inherited getServletContext() method, you can call it as-is. It however requires that the WAR is been exploded by the servletcontainer.

Sample in attach
分享到:
评论

相关推荐

    使用J2SE API读取Properties文件的六种方法

    这是最基本的方法,通过`FileInputStream`打开文件,然后使用`Properties`类的`load()`方法加载内容。例如: ```java String name = "config.properties"; InputStream in = new BufferedInputStream(new ...

    Java加载properties文件的六种方法

    这是最直接的方法,通过 `FileInputStream` 创建输入流,然后用 `Properties` 类的 `load()` 方法加载内容。示例代码如下: ```java InputStream in = new BufferedInputStream(new FileInputStream(name)); ...

    java读取proterties的六种方法

    这是最直接的方法,通过 `FileInputStream` 打开文件,然后创建 `Properties` 对象并调用 `load()` 方法加载文件内容。示例代码如下: ```java InputStream in = new BufferedInputStream(new FileInputStream(...

    Java读取Properties文件的六种方法.txt

    try (FileInputStream in = new FileInputStream("config.properties")) { props.load(in); } catch (IOException e) { e.printStackTrace(); } ``` #### 2. 使用`ClassLoader` 这种方式非常适合于读取类路径下...

    properties文件的读取

    FileInputStream s = new FileInputStream(file); properties.load(s); } catch (Exception e) { e.printStackTrace(); } return properties; } public void saveConfig(Properties properties, Context ...

    读取Properties文件的六种方法

    try (FileInputStream in = new FileInputStream(fileName)) { props.load(in); System.out.println("Read properties: " + props); } catch (IOException e) { e.printStackTrace(); } } } ``` #### 2. ...

    java读取properties属性文件

    FileInputStream fis = new FileInputStream("config.properties"); try { props.load(fis); } catch (IOException e) { e.printStackTrace(); } finally { if (fis != null) { try { fis.close(); } ...

    Java读取Properties文件的六种方法

    try (FileInputStream fis = new FileInputStream("path/to/config.properties")) { prop.load(fis); } catch (IOException ex) { ex.printStackTrace(); } ``` 5. 使用`org.apache.commons.io.IOUtils` Apache ...

    使用J2SEAPI读取Properties文件的六种方法

    FileInputStream fis = new FileInputStream("config.properties"); try { prop.load(fis); } finally { fis.close(); } ``` 这是最基本的读取方式,通过`FileInputStream`打开文件,然后使用`Properties`类...

    java读写properties文件,解决系统找不到指定路径,解决写入后读取正常,但文件数据未更新问题

    FileInputStream fis = new FileInputStream("C:\\config\\config.properties"); Properties prop = new Properties(); prop.load(fis); ``` 确保路径正确无误,并考虑到不同操作系统可能的路径分隔符差异。 接下来...

    java 读properties 文件六种方法

    import java.io.FileInputStream; import java.io.InputStream; import java.util.Properties; public class PropertiesReader { public static void main(String[] args) { try { InputStream in = new ...

    Java 读取资源文件

    - 如果资源文件位于文件系统的某个位置,你可以使用`FileInputStream`直接读取,但这不适用于类路径中的资源。 5. Maven或Gradle构建工具 - 对于Maven和Gradle这样的构建工具,它们会自动把`src/main/resources`...

    Java读取.properties配置文件方法示例

    在Java中,读取Properties文件主要有两种方法,一种是使用getResourceAsStream方法,另一种是使用FileInputStream方法。 一、Properties文件 Properties 文件是Java中主要的配置文件,文件类型为.properties,格式...

    jwwe学习资料,jwwe学习资料

    InputStream in = new BufferedInputStream(new FileInputStream(name)); Properties p = new Properties(); p.load(in); ``` 这种方法简单明了,适用于大多数情况,但需要处理文件流的打开与关闭,确保资源得到...

    Java读取.properties文件

    InputStream is = TestProperties.class.getResourceAsStream("/config.properties"); prop.load(is); ``` 此外,`.properties`文件中的特殊字符,如换行符、制表符等,会被自动处理。如果需要在值中包含这些特殊...

    JAVA操作properties文件.pdf

    FileInputStream in = new FileInputStream("config.properties"); prop.load(in); in.close(); ``` - 使用`getResourceAsStream`方法通过类加载器加载资源文件 ```java InputStream in = getClass()....

    java读取配置文件源代码

    在这个示例中,我们首先创建一个`Properties`对象,然后通过`getResourceAsStream()`或`FileInputStream`加载配置文件。`getProperty()`方法用于获取配置文件中的特定属性。请注意,`getResourceAsStream()`通常用于...

    java读取properties文件

    InputStream is = PropertiesReader.class.getResourceAsStream("/config.properties"); props.load(is); is.close(); ``` 这会从类路径的根目录下查找`config.properties`。 5. 使用`Properties`类的其他功能: `...

    在web和j2se中两个不同环境下 读取配置文件 备忘

    FileInputStream fis = new FileInputStream(filePath); props.load(fis); // 访问配置文件属性,如props.getProperty("key") fis.close(); } catch (IOException e) { throw new RuntimeException("无法加载...

Global site tag (gtag.js) - Google Analytics