Java – Read a file from resources folder - LearnHowToCode SarkariResult.com Interview Questions and Answers LearnHowToCodeOnline

Java – Read a file from resources folder

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

2. Classic Example

Example to read a file “test.txt” from a resources folder.
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.mycareerrepublic;

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 uses IOUtils to 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.mycareerrepublic;

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>mycareerrepublic</expected>
</case>
<case id=2>
<param>99</param>
<expected>mycareerrepublic</expected>
</case>
</test>
TestHello.java
package com.mycareerrepublic;

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>mycareerrepublic</expected>
</case>
<case id=2>
<param>99</param>
<expected>mycareerrepublic</expected>
</case>
</test>

About Mariano

I'm Ethan Mariano a software engineer by profession and reader/writter by passion.I have good understanding and knowledge of AngularJS, Database, javascript, web development, digital marketing and exploring other technologies related to Software development.

0 comments:

Featured post

Political Full Forms List

Acronym Full Form MLA Member of Legislative Assembly RSS Really Simple Syndication, Rashtriya Swayamsevak Sangh UNESCO United Nations E...

Powered by Blogger.