Java Text File Read Line readLines(File inputFile, String encoding)

Here you can find the source of readLines(File inputFile, String encoding)

Description

Read a file into a list of lines

License

Apache License

Parameter

Parameter Description
inputFile the file to read
encoding the encoding to use, e.g. "utf-8"

Return

list of lines

Declaration

public static List<String> readLines(File inputFile, String encoding) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010, 2012 Institute for Dutch Lexicology
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.//from   w ww  . ja va 2 s  .  com
 *******************************************************************************/

import java.io.BufferedReader;

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

import java.io.InputStreamReader;

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
     * The default encoding for opening files.
     */
    private static String defaultEncoding = "utf-8";

    /**
     * Read a file into a list of lines
     *
     * @param inputFile
     *            the file to read
     * @return list of lines
     */
    public static List<String> readLines(File inputFile) {
        return readLines(inputFile, defaultEncoding);
    }

    /**
     * Read a file into a list of lines
     *
     * @param inputFile
     *            the file to read
     * @param encoding
     *            the encoding to use, e.g. "utf-8"
     * @return list of lines
     */
    public static List<String> readLines(File inputFile, String encoding) {
        try {
            List<String> result = new ArrayList<>();
            BufferedReader in = openForReading(inputFile, encoding);
            try {
                String line;
                while ((line = in.readLine()) != null) {
                    result.add(line.trim());
                }
            } finally {
                in.close();
            }
            return result;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Opens a file for reading, with the default encoding.
     *
     * Wraps the Reader in a BufferedReader for efficient and convenient access.
     *
     * @param file
     *            the file to open
     * @return read interface into the file
     */
    public static BufferedReader openForReading(File file) {
        return openForReading(file, defaultEncoding);
    }

    /**
     * Opens a file for reading, with the default encoding.
     *
     * Wraps the Reader in a BufferedReader for efficient and convenient access.
     *
     * @param file
     *            the file to open
     * @param encoding
     *            the encoding to use, e.g. "utf-8"
     * @return read interface into the file
     */
    public static BufferedReader openForReading(File file, String encoding) {
        try {
            return new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. readLines(File file, String charsetName)
  2. readLines(File file, String encoding)
  3. readLines(File file, String encoding)
  4. readLines(File file, String outputLineEnding)
  5. readLines(File inputFile)
  6. readLines(final File file)
  7. readlines(final File file)
  8. readLines(int problemNumber)
  9. readLines(String body)