Java Text File Read by Charset readFileToList(String filePath, String charsetName)

Here you can find the source of readFileToList(String filePath, String charsetName)

Description

read file to string list, a element of list is a line

License

Open Source License

Parameter

Parameter Description
filePath a parameter
charsetName The name of a supported java.nio.charset.Charset </code>charset<code>

Exception

Parameter Description
RuntimeException if an error occurs while operator BufferedReader

Return

if file not exist, return null, else return content of file

Declaration

public static List<String> readFileToList(String filePath, String charsetName) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 *    BDD-Security, application security testing framework
 * /* ww  w.  java  2 s .  c o m*/
 * Copyright (C) `2014 Stephen de Vries`
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see `<http://www.gnu.org/licenses/>`.
 ******************************************************************************/

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

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.ArrayList;
import java.util.List;

public class Main {
    /**
     * read file to string list, a element of list is a line
     * 
     * @param filePath
     * @param charsetName The name of a supported {@link java.nio.charset.Charset </code>charset<code>}
     * @return if file not exist, return null, else return content of file
     * @throws RuntimeException if an error occurs while operator BufferedReader
     */
    public static List<String> readFileToList(String filePath, String charsetName) {
        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);
            reader = new BufferedReader(is);
            String line = null;
            while ((line = reader.readLine()) != null) {
                fileContent.add(line);
            }
            reader.close();
            return fileContent;
        } catch (IOException e) {
            throw new RuntimeException("IOException occurred. ", e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    throw new RuntimeException("IOException occurred. ", e);
                }
            }
        }
    }
}

Related

  1. readFile(String path, Charset charset)
  2. readFile(String path, Charset encoding)
  3. readFileAsString(String path, String charsetName)
  4. readFileContents(File file, Charset charset)
  5. readFileToLines(String inFile, String inCharset)
  6. readFileToString(String filename, Charset encoding)
  7. readFileToString(String path, Charset charset)
  8. readFromInputStream(InputStream stream, Charset charset)
  9. readInputStream(InputStream stream, Charset cs)