Let’s say we need to read the content from the file that is stored in the resources
folder
Read file as List of Strings
try (InputStream inputStream = MyClass.class.getResourceAsStream("/" + fileName);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
lines = reader.lines().collect(Collectors.toList());
} catch (IOException e) {
e.printStackTrace();
// Add something for IOException or throw an exception
}
Read file as String
try (InputStream inputStream = MyClass.class.getResourceAsStream("/" + fileName);
ByteArrayOutputStream result = new ByteArrayOutputStream()) {
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) != -1) {
result.write(buffer, 0, length);
}
string = result.toString();
} catch (IOException e) {
e.printStackTrace();
// Add something for IOException or throw an exception
}
The way how we get InputStream
allows us to read files even if they are in a jar.
This is an “improved” version of the code above.
Resource file reader
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class ResFile {
private final String fileName;
public ResFile(String fileName) {
this.fileName = fileName;
}
public List<String> getLines() {
List<String> lines = new ArrayList<>();
try (InputStream inputStream = ResFile.class.getResourceAsStream("/" + fileName);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
lines = reader.lines().collect(Collectors.toList());
} catch (IOException e) {
e.printStackTrace();
// Add something for IOException or throw an exception
}
return lines;
}
public String getString() {
String string = "";
try (InputStream inputStream = ResFile.class.getResourceAsStream("/" + fileName);
ByteArrayOutputStream result = new ByteArrayOutputStream()) {
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) != -1) {
result.write(buffer, 0, length);
}
return result.toString();
} catch (IOException e) {
e.printStackTrace();
// Add something for IOException or throw an exception
}
return string;
}
}
This is how that can be used
ResFile bookFile = new ResFile("my-book.json");
String json = bookFile.getString();
Book book = new Gson().fromJson(json, Book.class);
ResFile linesFile = new ResFile("lines.txt");
List<String> lines = linesFile.getLines();
Easy Files library
The similar approach is used in the open source library Easy Files.
So instead of implementing the code that reads your resource files you can just add the Maven dependency
<dependency>
<groupId>io.lenar</groupId>
<artifactId>files</artifactId>
<version>${currentVersion}</version>
</dependency>
and read files just like this
ResourceFile file = new ResourceFile("my-file.json");
String content = file.content();
List<String> lines = file.lines();
Book book = file.fromJson(Book.class);
List<Book> books = file.fromJsonAsList(Book[].class);
The current version of Easy Files - .
Give Easy Files a star on GitHub if you like it.
You may also find these posts interesting: