Java Text File Read Line readLines(File file)

Here you can find the source of readLines(File file)

Description

read Lines

License

Open Source License

Declaration

public static List<String> readLines(File file) 

Method Source Code

//package com.java2s;
/*/*from w w  w  . j  av  a2s . c  om*/
 * $Id$
 *
 * Copyright (C) 2003-2015 JNode.org
 *
 * This library 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 library 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 library; If not, write to the Free Software Foundation, Inc., 
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

import java.io.BufferedReader;
import java.io.Closeable;
import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;
import java.io.Flushable;

import java.io.IOException;

import java.io.Reader;
import java.util.List;
import java.util.LinkedList;

public class Main {
    private static final int BUFFER_SIZE = 4096;
    private static final String ex_null_param = "A required paramater is null";

    public static List<String> readLines(File file) {
        BufferedReader reader = openBufferedReader(file);
        if (reader == null) {
            return null;
        }
        try {
            return readLines(reader);
        } finally {
            close(reader);
        }
    }

    public static List<String> readLines(Reader reader) {
        return readLines(new BufferedReader(reader, BUFFER_SIZE), -1);
    }

    public static List<String> readLines(BufferedReader reader) {
        return readLines(reader, -1);
    }

    /**
     * Read all of the lines from a Reader.
     *
     * Calling this is the same as readLines(new BufferedReader(reader), max)
     *
     * @param reader the source to read from
     * @param max the max number of lines to read, if this is -1 Integer.MAX_VALUE is used
     * @return a list of strings, possibly empty, or null if there was an exception.
     * @throws NullPointerException if reader is null
     */
    public static List<String> readLines(Reader reader, int max) {
        return readLines(new BufferedReader(reader, BUFFER_SIZE), max);
    }

    /**
     * Read all of the lines from a Reader.
     *
     * If there are no lines to read, an empty List will be returned.
     *
     * If there was an exception while reading, null will be returned.
     *
     * @param reader the source to read from
     * @param max the max number of lines to read, if this is -1 Integer.MAX_VALUE is used
     * @return a list of strings, possibly empty, or null if there was an exception.
     * @throws NullPointerException if reader is null
     */
    public static List<String> readLines(BufferedReader reader, int max) {
        checkNull(reader);
        List<String> ret = new LinkedList<String>();
        String line;
        int count = 0;

        try {
            if (max > 0) {
                while ((count++ < max) && (line = reader.readLine()) != null) {
                    ret.add(line);
                }
            } else {
                while ((line = reader.readLine()) != null) {
                    ret.add(line);
                }
            }
        } catch (IOException e) {
            return null;
        }

        return ret;
    }

    /**
     * Opens a BufferedReader on a file.
     *
     * This method will not throw a FileNotFoundException like the FileReader
     * constructor would.
     *
     * @param file the file to open the reader on
     * @return the reader, or null if the file could not be opened
     * @throws NullPointerException if file is null
     */
    public static BufferedReader openBufferedReader(File file) {
        return openBufferedReader(file, BUFFER_SIZE);
    }

    /**
     * Opens a BufferedReader on a file.
     * 
     * This method will not throw a FileNotFoundException like the FileReader
     * constructor would.
     *
     * @param file the file to open the reader on
     * @param bufferSize the buffer size to use for this reader
     * @return the reader, or null if the file could not be opened
     * @throws NullPointerException if file is null
     */
    public static BufferedReader openBufferedReader(File file, int bufferSize) {
        Reader reader = openReader(file);
        if (reader != null) {
            return new BufferedReader(reader, bufferSize);
        }
        return null;
    }

    /**
     * Wraps a {@code Reader} with a {@code BufferedReader}.
     *
     * @param reader the reader to wrap
     * @return a {@code BufferedReader}
     * @throws NullPointerException if reader is null
     */
    public static BufferedReader openBufferedReader(Reader reader) {
        return openBufferedReader(reader, BUFFER_SIZE);
    }

    /**
     * Wraps a {@code Reader} with a {@code BufferedReader}.
     *
     * @param reader the reader to wrap
     * @param bufferSize the size of buffer to use
     * @return a {@code BufferedReader}
     * @throws NullPointerException if reader is null
     */
    public static BufferedReader openBufferedReader(Reader reader, int bufferSize) {
        checkNull(reader);
        return new BufferedReader(reader, bufferSize);
    }

    /**
     * Call the close method of a list of Closeable objects.
     *
     * This will not throw a NullPointerException if any of the objects are null.
     * 
     * This is a convenience method that traps the exception from various
     * stream close() methods.
     *
     * @param objs one or more Closeable objects.
     */
    public static void close(Closeable... objs) {
        close(false, objs);
    }

    /**
     * Call the close method of a list of Closeable objects.
     *
     * If the flush parameter is set to true, and an object implements
     * the Flushable interface, then the flush method will be called before
     * the close method is called.
     *
     * This will not throw a NullPointerException if any of the objects are null.
     *
     * This will not throw an IOException if either the close or flush methods throw
     * an IOException.
     *
     * If calling flush causes an IOException, close will still be called.
     *
     * @param flush if true, check if the object is Flushable
     * @param objs one or more Closeable objects
     */
    public static void close(boolean flush, Closeable... objs) {
        for (Closeable obj : objs) {
            if (obj != null) {
                if (flush && (obj instanceof Flushable)) {
                    try {
                        ((Flushable) obj).flush();
                    } catch (IOException _) {
                        // ignore
                    }
                }
                try {
                    obj.close();
                } catch (IOException _) {
                    // ignore
                }
            }
        }
    }

    /**
     * Check for null objects in a list of objects.
     */
    private static void checkNull(Object... objs) {
        for (Object obj : objs) {
            if (obj == null)
                throw new NullPointerException(ex_null_param);
        }
    }

    /**
     * Opens a Reader on a file.
     * 
     * This method will not throw a FileNotFoundException like the FileReader
     * constructor would.
     *
     * @param file the file to open the reader on
     * @return the reader, or null if the file could not be opened
     * @throws NullPointerException if file is null
     */
    public static Reader openReader(File file) {
        checkNull(file);

        try {
            return new FileReader(file);
        } catch (FileNotFoundException e) {
            // fall through
        }

        return null;
    }

    /**
     * Call the flush method of a list of Flushable objects.
     *
     * This method will not throw a NullPointerException if any of the objects are null.
     *
     * This method will trap the IOException from flush.
     *
     * @param objs one or more Flushable objects
     */
    public static void flush(Flushable... objs) {
        for (Flushable obj : objs) {
            if (obj != null) {
                try {
                    obj.flush();
                } catch (IOException e) {
                    // ignore
                }
            }
        }
    }
}

Related

  1. readLines(File file)
  2. readLines(File file)
  3. readLines(File file)
  4. readLines(File file)
  5. readLines(File file)
  6. readLines(File file)
  7. readLines(File file)
  8. readLines(File file)
  9. readLines(File file)