Java – Read a file from resources folder
2014-12-01 17:47
295 查看
Java – Read a file from resources folder
Posted on July 30, 2014
By
mkyong
In this tutorial, we will show you how to read a file from a resources folder, in both Java and Unit Test environment. In simple, put files in a resources folder, and read the file with following code snippets :
ClassLoader classLoader = getClass().getClassLoader(); File file = new File(classLoader.getResource("file/test.xml").getFile());
1. Project Directory
Review a Maven project Structure.2. Classic Example
Example to read a file “test.txt” from aresourcesfolder.
main/resources/file/test.txt
This is line 1 This is line 2 This is line 3 This is line 4 This is line 5
Hello.java
package com.mkyong; import java.io.File; import java.io.IOException; import java.util.Scanner; public class Hello { public static void main(String[] args) { Hello obj = new Hello(); System.out.println(obj.getFile("file/test.txt")); } private String getFile(String fileName) { StringBuilder result = new StringBuilder(""); //Get file from resources folder ClassLoader classLoader = getClass().getClassLoader(); File file = new File(classLoader.getResource(fileName).getFile()); try (Scanner scanner = new Scanner(file)) { while (scanner.hasNextLine()) { String line = scanner.nextLine(); result.append(line).append("\n"); } scanner.close(); } catch (IOException e) { e.printStackTrace(); } return result.toString(); } }
Output
This is line 1 This is line 2 This is line 3 This is line 4 This is line 5
3. IOUtils Example
This example usesIOUtilsto parse a file.
pom.xml
<dependency> <groupId>org.apache.directory.studio</groupId> <artifactId>org.apache.commons.io</artifactId> <version>2.4</version> </dependency>
Hello.java
package com.mkyong; import java.io.IOException; import org.apache.commons.io.IOUtils; public class Hello { public static void main(String[] args) { Hello obj = new Hello(); System.out.println(obj.getFileWithUtil("file/test.txt")); } private String getFileWithUtil(String fileName) { String result = ""; ClassLoader classLoader = getClass().getClassLoader(); try { result = IOUtils.toString(classLoader.getResourceAsStream(fileName)); } catch (IOException e) { e.printStackTrace(); } return result; } }
Output
This is line 1 This is line 2 This is line 3 This is line 4 This is line 5
4. Unit Test Example
A jUnit example.test/resources/xml/test.xml
<test> <case id=1> <param>100</param> <expected>mkyong</expected> </case> <case id=2> <param>99</param> <expected>mkyong</expected> </case> </test>
TestHello.java
package com.mkyong; import java.io.IOException; import org.apache.commons.io.IOUtils; import org.junit.Test; public class TestHello { @Test public void testHello() { String result = getFile("xml/test.xml"); System.out.println(result); } private String getFile(String fileName){ String result = ""; ClassLoader classLoader = getClass().getClassLoader(); try { result = IOUtils.toString(classLoader.getResourceAsStream(fileName)); } catch (IOException e) { e.printStackTrace(); } return result; } }
Output
<test> <case id=1> <param>100</param> <expected>mkyong</expected> </case> <case id=2> <param>99</param> <expected>mkyong</expected> </case> </test>
References
Java : GetResourceAsStream In Static MethodClass – JavaDoc
相关文章推荐
- get file's name from a folder using Java
- j2me工具类:ReadFromFile.java
- java 读取配置(read config from file)
- java.lang.RuntimeException: Could not read input channel file descriptors from parcel.
- How to read file in Java – BufferedInputStream Example
- Unable to read TLD "META-INF/c.tld" from JAR file standard.rar
- How To Read a File From Your Application Bundle
- No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/D:/package/java/hibernat
- [Tips]:Read value from Resource file in C#
- Read pixel data from bitmap file
- java.sql.SQLException: Io 异常: Got minus one from a read call
- java file read and write code(io包的使用)
- Unable to read TLD "META-INF/c.tld" from JAR file "file: 解决办法
- Unable to build: the file dx.jar was not loaded from the SDK folder!
- file read and write use java
- Read a file from xml
- Unable to read TLD "META-INF/tlds/struts-bean.tld" from JAR file "file:/xxxWEB-INF/lib/struts.jar": org.apache.jasper.Ja
- How to open/read/write a local file from an applet
- Java read a text file
- A simple way to read an XML file in Java