Languages
[Edit]
EN

Java - read all text from file with custom FileUtils

11 points
Created by:
Root-ssh
7645

In Java it is possible to read all text from file in following way.

1. Custom file reading class example

Program.java file:

package com.dirask.examples;

import java.io.*;
import java.nio.file.Paths;

public class Program {
    public static void main(String[] args) throws IOException {

        String text1 = FileUtils.readAllText("C:\\Project\\input.txt");
        System.out.println(text1);

        String text2 = FileUtils.readAllText(new File("C:\\Project\\input.txt"));
        System.out.println(text2);

        String text3 = FileUtils.readAllText(Paths.get("C:\\Project\\input.txt"));
        System.out.println(text3);

        try(FileReader reader = new FileReader("C:\\Project\\input.txt")) {
            String text4 = FileUtils.readAllText(reader);

            System.out.println(text4);
        }

        try(InputStream stream = new FileInputStream("C:\\Project\\input.txt")) {
            String text5 = FileUtils.readAllText(stream);

            System.out.println(text5);
        }
    }
}

output:

This is 1st line...
This is 2nd line...
This is 3rd line...

This is 1st line...
This is 2nd line...
This is 3rd line...

This is 1st line...
This is 2nd line...
This is 3rd line...

This is 1st line...
This is 2nd line...
This is 3rd line...

This is 1st line...
This is 2nd line...
This is 3rd line...

 

input.txt file (full path is C:\Project\input.txt):

This is 1st line...
This is 2nd line...
This is 3rd line...

FileUtils.java file:

package com.dirask.examples;

import java.io.*;
import java.nio.file.Path;

public class FileUtils {

    public static String readAllText(Reader reader) throws IOException {
        StringBuilder builder = new StringBuilder();

        char[] buffer = new char[1024];

        while (true) {
            int count = reader.read(buffer, 0, buffer.length);

            if (count == -1)
                break;

            builder.append(buffer, 0, count);
        }

        return builder.toString();
    }

    public static String readAllText(InputStream stream) throws IOException {
        Reader reader = new InputStreamReader(stream, "UTF-8");

        return readAllText(reader);
    }

    public static String readAllText(File file) throws IOException {
        InputStream stream = new FileInputStream(file);

        return readAllText(stream);
    }

    public static String readAllText(Path path) throws IOException {
        File file = path.toFile();

        return readAllText(file);
    }

    public static String readAllText(String path) throws IOException {
        FileInputStream stream = new FileInputStream(path);

        return readAllText(stream);
    }
}

Merged questions

  1. Java - how to read all lines as one string from file object?
  2. Java - how to read all lines as one string from input stream object?
  3. Java - how to read all lines as one string from reader object?
  4. Java - how to read all lines as one string using file path string?
  5. Java - how to read all lines as one string using path object?
Native Advertising
50 000 ad impressions - 449$
🚀
Get your tech brand or product in front of software developers.
For more information contact us:
Red dot
Dirask - friendly IT community for everyone.

❤️💻 🙂

Join