このチュートリアルでは、Javaおよび単体テスト環境の両方でリソースフォルダからファイルを読み込む方法を説明します。単純に、ファイルをresourcesフォルダに置き、次のコードスニペットでファイルを読み込みます。
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("file/test.xml").getFile());
1.プロジェクトディレクトリ
Mavenプロジェクトの構造を見直してください。
2.クラシックな例
例は、 `resources`フォルダから” test.txt “というファイルを読み込みます。
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();
}
}
出力
This is line 1 This is line 2 This is line 3 This is line 4 This is line 5
3. IOUtilsの例
この例では `IOUtils`を使ってファイルを解析しています。
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;
}
}
出力
This is line 1 This is line 2 This is line 3 This is line 4 This is line 5
ユニットテストの例
jUnitの例
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;
}
}
出力
<test>
<case id=1>
<param>100</param>
<expected>mkyong</expected>
</case>
<case id=2>
<param>99</param>
<expected>mkyong</expected>
</case>
</test>
参考文献
-
リンク://java/java-getresourceas-static-method-stream/[Java:
静的メソッドのGetResourceAsStream]。
http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html
[Class –
JavaDoc]
リンク://タグ/java/[java]