Java Text File Read by Charset readFileToLines(String inFile, String inCharset)

Here you can find the source of readFileToLines(String inFile, String inCharset)

Description

read File To Lines

License

Open Source License

Declaration

public static List<String> readFileToLines(String inFile, String inCharset) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedReader;

import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.nio.charset.Charset;

import java.util.ArrayList;

import java.util.List;

public class Main {
    public static List<String> readFileToLines(String inFile, String inCharset) throws IOException {
        return inFile == null ? new ArrayList<>(0) : readFileToLines(new File(inFile), inCharset);
    }//w  ww . j  a v a2 s.c o  m

    public static List<String> readFileToLines(File inFile, String inCharset) throws IOException {
        return inFile == null ? new ArrayList<>(0)
                : readFileToLines(inFile, inCharset == null ? null : Charset.forName(inCharset));
    }

    public static List<String> readFileToLines(String inFile, Charset inCharset) throws IOException {
        return inFile == null ? new ArrayList<>(0) : readFileToLines(new File(inFile), inCharset);
    }

    public static List<String> readFileToLines(File inFile, Charset inCharset) throws IOException {

        if (!isFile(inFile)) {
            return null;
        }

        inCharset = defaultIfNull(inCharset, Charset.defaultCharset());

        final List<String> theLines = new ArrayList<>();

        try (BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(inFile), inCharset))) {

            while (r.ready()) {
                theLines.add(r.readLine());
            }
        }

        return theLines;
    }

    public static boolean isFile(File inFile) {
        return inFile != null && inFile.isFile();
    }

    public static boolean isFile(String inFilePath) {
        return isNotBlank(inFilePath) && isFile(new File(inFilePath));
    }

    public static <T> T defaultIfNull(T inValue, T inDefault) {
        return inValue == null ? inDefault : inValue;
    }

    public static Charset defaultCharset(String inCharset) {
        try {
            return Charset.forName(inCharset);
        } catch (Exception e) {
            return Charset.defaultCharset();
        }
    }

    public static boolean isNotBlank(String inValue) {
        return !isBlank(inValue);
    }

    public static boolean isBlank(String inValue) {
        return inValue == null || inValue.trim().isEmpty();
    }
}

Related

  1. readFile(final InputStream is, final Charset encoding)
  2. readFile(String path, Charset charset)
  3. readFile(String path, Charset encoding)
  4. readFileAsString(String path, String charsetName)
  5. readFileContents(File file, Charset charset)
  6. readFileToList(String filePath, String charsetName)
  7. readFileToString(String filename, Charset encoding)
  8. readFileToString(String path, Charset charset)
  9. readFromInputStream(InputStream stream, Charset charset)