Java Scanner Read Line readLineByLine(String fileName)

Here you can find the source of readLineByLine(String fileName)

Description

read Line By Line

License

Open Source License

Declaration

public static List<String> readLineByLine(String fileName) 

Method Source Code


//package com.java2s;
/*//from  ww  w.  j  a  v a2 s  .  co m
 * JBoss, Home of Professional Open Source.
 * Copyright 2013, Red Hat Middleware LLC, and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

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

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

public class Main {
    private final static String DEFAULT_FILE_ENCODING = "UTF-8";

    public static List<String> readLineByLine(File file) {
        if (file == null)
            throw new IllegalArgumentException("Provided was file object was 'null'");
        if (!file.exists())
            throw new IllegalArgumentException("File " + file.getAbsolutePath() + " does not exist");
        if (!file.canRead())
            throw new IllegalArgumentException("File " + file.getAbsolutePath() + " can't be read");

        return readLineByLine(file.getAbsoluteFile().toString());
    }

    public static List<String> readLineByLine(String fileName) {
        String fileEncoding = getFileEncoding();
        List<String> lines = new ArrayList<String>();
        Scanner scanner = null;
        try {
            scanner = new Scanner(new FileInputStream(fileName), fileEncoding);
            while (scanner.hasNextLine()) {
                lines.add(scanner.nextLine());
            }
        } catch (FileNotFoundException e) {
            throw new IllegalStateException(e);
        } finally {
            if (scanner != null)
                scanner.close();
        }
        return lines;
    }

    public static boolean exists(String filename) {
        return new File(filename).exists();
    }

    public static String getFileEncoding() {
        String fileEncoding = System.getProperty("file.encoding");
        if (fileEncoding == null || "".equals(fileEncoding))
            fileEncoding = DEFAULT_FILE_ENCODING; // Fallback to UTF-8 default encoding
        return fileEncoding;
    }
}

Related

  1. readLine(String path)