Java Reader Read Line readLines(Reader reader)

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

Description

Reads a reader putting all its lines in an array.

License

Apache License

Parameter

Parameter Description
reader Reader to read

Return

an array of String containing reader's lines

Declaration

public static final String[] readLines(Reader reader) 

Method Source Code

//package com.java2s;
/**//from  w  w w . j  a  va  2 s  .  c  om
 *  Copyright 2011 Marco Berri - marcoberri@gmail.com
 *
 *  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.Reader;

import java.util.ArrayList;

public class Main {
    /**
     * Reads a reader putting all its lines in an array.
     * 
     * @param reader
     *            Reader to read
     * @return an array of String containing reader's lines
     */
    public static final String[] readLines(Reader reader) {
        return readLines(reader, 0, 0);
    }

    /**
     * Reads a reader putting nLines lines in an array.
     * 
     * @param reader
     *            Reader to read
     * @param nLines
     *            number of lines to read, if nLines <= 0 then read until end of
     *            reader
     * @return an array of String containing reader's lines
     */
    public static final String[] readLines(Reader reader, int nLines) {
        return readLines(reader, 0, nLines);
    }

    /**
     * Reads a reader starting from startLine and putting nLines lines in an
     * array.
     * 
     * @param reader
     *            Reader to read
     * @param startLine
     *            line to start, if startLine < 0 then startLine = 0
     * @param nLines
     *            number of lines to read, if nLines <= 0 then read until end of
     *            reader
     * @return an array of String containing reader's lines
     */
    public static final String[] readLines(Reader reader, int startLine, int nLines) {
        if (reader == null) {
            return null;
        }
        if (startLine < 0) {
            startLine = 0;
        }
        try {
            BufferedReader buf = new BufferedReader(reader);
            ArrayList lines = new ArrayList();
            int i = 0;
            for (String s = null; (nLines <= 0 || i < nLines) && (s = buf.readLine()) != null; i++) {
                if (i < startLine) {
                    continue;
                }
                lines.add(s);
            }
            return (String[]) lines.toArray(new String[i]);
        } catch (Exception x) {
            return null;
        }
    }
}

Related

  1. readLines(Reader input)
  2. readLines(Reader input)
  3. readLines(Reader input)
  4. readLines(Reader r, List lines)
  5. readLines(Reader reader)
  6. readLines(Reader reader)
  7. readLines(Reader reader)
  8. readLines(Reader reader)
  9. readLinesFromReader(final Reader reader)