Java Utililty Methods Text File Read by Charset

List of utility methods to do Text File Read by Charset

Description

The list of methods to do Text File Read by Charset are organized into topic(s).

Method

StringreadFile(final InputStream is, final Charset encoding)
Reads a file.
if (is == null) {
    throw new IllegalArgumentException("The InputStream is null");
if (encoding == null) {
    throw new IllegalArgumentException("The Charset is null");
final Reader reader = new BufferedReader(new InputStreamReader(is, encoding));
final StringBuilder builder = new StringBuilder();
...
StringreadFile(String path, Charset charset)
read File
StringBuilder content = new StringBuilder();
List<String> allLines = Files.readAllLines(new File(path).toPath(), charset);
for (String someLine : allLines) {
    content.append(someLine);
    content.append(STR_NEWLINE);
return content.toString().replaceFirst(UTF8_BOM, STR_EMPTY);
StringreadFile(String path, Charset encoding)
Read a file from path and convert it into string.
return new Scanner(new File(path), "UTF-8").useDelimiter("\\A").next();
StringreadFileAsString(String path, String charsetName)
This method reads the file as String and assumes that file contains information in specified encoding.
ByteBuffer content = internalReadFileAsByteArray(path);
CharBuffer result = Charset.forName(charsetName).decode(content);
char[] charContent = null;
if (result.hasArray()) {
    charContent = result.array();
} else {
    charContent = new char[result.limit()];
    result.get(charContent);
...
StringreadFileContents(File file, Charset charset)
read File Contents
BufferedReader br = null;
StringBuilder sb = null;
String line;
if (file != null && charset != null) {
    if (file.exists()) {
        try {
            sb = new StringBuilder();
            br = Files.newBufferedReader(Paths.get(file.getPath()), charset);
...
ListreadFileToLines(String inFile, String inCharset)
read File To Lines
return inFile == null ? new ArrayList<>(0) : readFileToLines(new File(inFile), inCharset);
ListreadFileToList(String filePath, String charsetName)
read file to string list, a element of list is a line
File file = new File(filePath);
List<String> fileContent = new ArrayList<String>();
if (file == null || !file.isFile()) {
    return null;
BufferedReader reader = null;
try {
    InputStreamReader is = new InputStreamReader(new FileInputStream(file), charsetName);
...
StringreadFileToString(String filename, Charset encoding)
read File To String
return readStreamToString(new FileInputStream(filename), encoding);
StringreadFileToString(String path, Charset charset)
read File To String
List<String> lines = Files.readAllLines(Paths.get(path), charset);
return join("", lines);
StringreadFromInputStream(InputStream stream, Charset charset)
read From Input Stream
List<Byte> bytes = new LinkedList<>();
byte[] buffer = new byte[128];
int read;
while ((read = stream.read(buffer)) != -1) {
    for (int i = 0; i < read; i++) {
        bytes.add(buffer[i]);
int total = bytes.size();
buffer = new byte[total];
for (int i = 0; i < total; i++) {
    buffer[i] = bytes.remove(0);
if (charset == null) {
    return new String(buffer);
} else {
    return new String(buffer, charset);