Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Utils to read resource text file to String (Java) [closed]

Is there a way to read a text file in the resource into a String?

I suppose this is a popular requirement, but I couldn't find any utility after Googling.

like image 212
Loc Phan Avatar asked May 20 '11 06:05

Loc Phan


People also ask

What is a pretty sure way you can read in a whole file text into a string using a scanner?

readString() method, which can read all characters from a file into a string. It takes a path to the file and overloaded to accept the charset used for decoding from bytes to characters.

What is the best way to read an entire file into a single string?

The FileUtils. readFileToString() is an excellent way to read a whole file into a String in a single statement.


4 Answers

Yes, Guava provides this in the Resources class. For example:

URL url = Resources.getResource("foo.txt");
String text = Resources.toString(url, StandardCharsets.UTF_8);
like image 200
Jon Skeet Avatar answered Oct 17 '22 01:10

Jon Skeet


You can use the old Stupid Scanner trick oneliner to do that without any additional dependency like guava:

String text = new Scanner(AppropriateClass.class.getResourceAsStream("foo.txt"), "UTF-8").useDelimiter("\\A").next();

Guys, don't use 3rd party stuff unless you really need that. There is a lot of functionality in the JDK already.

like image 40
akosicki Avatar answered Oct 16 '22 23:10

akosicki


For java 7:

new String(Files.readAllBytes(Paths.get(getClass().getResource("foo.txt").toURI())));

For Java 11:

Files.readString(Paths.get(getClass().getResource("foo.txt").toURI()));
like image 110
Kovalsky Dmitryi Avatar answered Oct 16 '22 23:10

Kovalsky Dmitryi


Pure and simple, jar-friendly, Java 8+ solution

This simple method below will do just fine if you're using Java 8 or greater:

/**
 * Reads given resource file as a string.
 *
 * @param fileName path to the resource file
 * @return the file's contents
 * @throws IOException if read fails for any reason
 */
static String getResourceFileAsString(String fileName) throws IOException {
    ClassLoader classLoader = ClassLoader.getSystemClassLoader();
    try (InputStream is = classLoader.getResourceAsStream(fileName)) {
        if (is == null) return null;
        try (InputStreamReader isr = new InputStreamReader(is);
             BufferedReader reader = new BufferedReader(isr)) {
            return reader.lines().collect(Collectors.joining(System.lineSeparator()));
        }
    }
}

And it also works with resources in jar files.

About text encoding: InputStreamReader will use the default system charset in case you don't specify one. You may want to specify it yourself to avoid decoding problems, like this:

new InputStreamReader(isr, StandardCharsets.UTF_8);

Avoid unnecessary dependencies

Always prefer not depending on big, fat libraries. Unless you are already using Guava or Apache Commons IO for other tasks, adding those libraries to your project just to be able to read from a file seems a bit too much.

like image 108
Lucio Paiva Avatar answered Oct 17 '22 00:10

Lucio Paiva