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
分享到:
相关推荐
这是最基本的方法,通过`FileInputStream`打开文件,然后使用`Properties`类的`load()`方法加载内容。例如: ```java String name = "config.properties"; InputStream in = new BufferedInputStream(new ...
这是最直接的方法,通过 `FileInputStream` 创建输入流,然后用 `Properties` 类的 `load()` 方法加载内容。示例代码如下: ```java InputStream in = new BufferedInputStream(new FileInputStream(name)); ...
这是最直接的方法,通过 `FileInputStream` 打开文件,然后创建 `Properties` 对象并调用 `load()` 方法加载文件内容。示例代码如下: ```java InputStream in = new BufferedInputStream(new FileInputStream(...
try (FileInputStream in = new FileInputStream("config.properties")) { props.load(in); } catch (IOException e) { e.printStackTrace(); } ``` #### 2. 使用`ClassLoader` 这种方式非常适合于读取类路径下...
FileInputStream s = new FileInputStream(file); properties.load(s); } catch (Exception e) { e.printStackTrace(); } return properties; } public void saveConfig(Properties properties, Context ...
try (FileInputStream in = new FileInputStream(fileName)) { props.load(in); System.out.println("Read properties: " + props); } catch (IOException e) { e.printStackTrace(); } } } ``` #### 2. ...
FileInputStream fis = new FileInputStream("config.properties"); try { props.load(fis); } catch (IOException e) { e.printStackTrace(); } finally { if (fis != null) { try { fis.close(); } ...
try (FileInputStream fis = new FileInputStream("path/to/config.properties")) { prop.load(fis); } catch (IOException ex) { ex.printStackTrace(); } ``` 5. 使用`org.apache.commons.io.IOUtils` Apache ...
FileInputStream fis = new FileInputStream("config.properties"); try { prop.load(fis); } finally { fis.close(); } ``` 这是最基本的读取方式,通过`FileInputStream`打开文件,然后使用`Properties`类...
FileInputStream fis = new FileInputStream("C:\\config\\config.properties"); Properties prop = new Properties(); prop.load(fis); ``` 确保路径正确无误,并考虑到不同操作系统可能的路径分隔符差异。 接下来...
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 ...
- 如果资源文件位于文件系统的某个位置,你可以使用`FileInputStream`直接读取,但这不适用于类路径中的资源。 5. Maven或Gradle构建工具 - 对于Maven和Gradle这样的构建工具,它们会自动把`src/main/resources`...
在Java中,读取Properties文件主要有两种方法,一种是使用getResourceAsStream方法,另一种是使用FileInputStream方法。 一、Properties文件 Properties 文件是Java中主要的配置文件,文件类型为.properties,格式...
InputStream in = new BufferedInputStream(new FileInputStream(name)); Properties p = new Properties(); p.load(in); ``` 这种方法简单明了,适用于大多数情况,但需要处理文件流的打开与关闭,确保资源得到...
InputStream is = TestProperties.class.getResourceAsStream("/config.properties"); prop.load(is); ``` 此外,`.properties`文件中的特殊字符,如换行符、制表符等,会被自动处理。如果需要在值中包含这些特殊...
FileInputStream in = new FileInputStream("config.properties"); prop.load(in); in.close(); ``` - 使用`getResourceAsStream`方法通过类加载器加载资源文件 ```java InputStream in = getClass()....
在这个示例中,我们首先创建一个`Properties`对象,然后通过`getResourceAsStream()`或`FileInputStream`加载配置文件。`getProperty()`方法用于获取配置文件中的特定属性。请注意,`getResourceAsStream()`通常用于...
InputStream is = PropertiesReader.class.getResourceAsStream("/config.properties"); props.load(is); is.close(); ``` 这会从类路径的根目录下查找`config.properties`。 5. 使用`Properties`类的其他功能: `...
FileInputStream fis = new FileInputStream(filePath); props.load(fis); // 访问配置文件属性,如props.getProperty("key") fis.close(); } catch (IOException e) { throw new RuntimeException("无法加载...