Java Reader Read Line readLines(Reader input)

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

Description

Gets the contents of the Reader provided as a List of Strings for each line.

License

Open Source License

Parameter

Parameter Description
input the Reader to be read

Exception

Parameter Description
IOException If an I/O error occurs.
NullPointerException If input is null.

Return

a List of Strings representing each line

Declaration

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

Method Source Code


//package com.java2s;
/* Copyright (C) 2011 Alasdair Mercer, http://neocotic.com/
 * //  w  w w.j a  v  a2 s .  co m
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

public class Main {
    /**
     * Gets the contents of the {@code Reader} provided as a {@code List} of
     * {@code Strings} for each line.
     * 
     * @param input
     *            the {@code Reader} to be read
     * @return a {@code List} of {@code Strings} representing each line
     * @throws IOException
     *             If an I/O error occurs.
     * @throws NullPointerException
     *             If {@code input} is {@code null}.
     */
    public static List<String> readLines(Reader input) throws IOException {
        List<String> list = new ArrayList<String>();
        BufferedReader reader = new BufferedReader(input);

        String line = reader.readLine();

        while (line != null) {
            list.add(line);
            line = reader.readLine();
        }

        return list;
    }
}

Related

  1. readLines(Reader input)
  2. readLines(Reader input)
  3. readLines(Reader input)
  4. readLines(Reader input)
  5. readLines(Reader input)
  6. readLines(Reader input)
  7. readLines(Reader input)
  8. readLines(Reader input)
  9. readLines(Reader input)