Read text file with encoding - Java java.nio.file

Java examples for java.nio.file:Files

Description

Read text file with encoding

Demo Code


import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

public class JSFormatter {

    static String readFile(String path, Charset encoding)
            throws IOException {
        byte[] encoded = Files.readAllBytes(Paths.get(path));
        return new String(encoded, encoding);
    }/*from   w w  w.  j  a v  a  2s  .co m*/
}

Related Tutorials