Java Reader Read Line readLines(final Reader input)

Here you can find the source of readLines(final Reader input)

Description

Get the contents of a Reader as a list of Strings, one entry per line.

License

Apache License

Parameter

Parameter Description
input the Reader to read from, not null

Exception

Parameter Description
IOException if an I/O error occurs

Return

the array list of Strings. It never be null, but could be empty.

Declaration

public static ArrayList<String> readLines(final Reader input) throws IOException 

Method Source Code

//package com.java2s;
/*/*  w ww. j  a v a  2s  .c  o  m*/
 * Copyright (c) 2014  Haixing Hu
 *
 * 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.
 *
 */

import java.io.BufferedReader;

import java.io.IOException;

import java.io.Reader;

import java.util.ArrayList;

public class Main {
    /**
     * Get the contents of a {@code Reader} as a list of Strings, one entry
     * per line.
     *
     * This method buffers the input internally, so there is no need to use a
     * {@code BufferedReader}.
     *
     * Note that the caller MUST close the reader by itself.
     *
     * @param input
     *          the {@code Reader} to read from, not null
     * @return the array list of Strings. It never be null, but could be empty.
     * @throws IOException
     *           if an I/O error occurs
     */
    public static ArrayList<String> readLines(final Reader input) throws IOException {
        final BufferedReader reader = new BufferedReader(input);
        final ArrayList<String> result = new ArrayList<String>();
        String line = reader.readLine();
        while (line != null) {
            result.add(line);
            line = reader.readLine();
        }
        return result;
    }
}

Related

  1. readLines(BufferedReader in, StringBuilder buffer, int numLines, String lineEnd)
  2. readLines(BufferedReader reader)
  3. readLines(final BufferedReader aReader)
  4. readLines(Reader in)
  5. readLines(Reader in, boolean trim)
  6. readLines(Reader input)
  7. readLines(Reader input)