Java Reader Read Line readLines(Reader in, boolean trim)

Here you can find the source of readLines(Reader in, boolean trim)

Description

Reads all data from the reader and returns it as an array of lines.

License

Open Source License

Parameter

Parameter Description
trim true, if the lines should be trimmed.

Declaration

public static String[] readLines(Reader in, boolean trim) throws IOException 

Method Source Code


//package com.java2s;
/*/*from  ww  w .  j a v  a2s  .  c o  m*/
 * Copyright (C) 2014 Civilian Framework.
 *
 * Licensed under the Civilian License (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.civilian-framework.org/license.txt
 *
 * 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;
import java.util.List;

public class Main {
    /**
     * Reads all data from the reader and returns it as an array of lines.
     * @param trim true, if the lines should be trimmed.
     */
    public static String[] readLines(Reader in, boolean trim) throws IOException {
        ArrayList<String> list = new ArrayList<>();
        readLines(in, trim, list);
        return list.toArray(new String[list.size()]);
    }

    /**
     * Reads all data from the reader and fills it in a list.
     * @param trim true, if the lines should be trimmed.
     */
    public static void readLines(Reader in, boolean trim, List<String> list) throws IOException {
        BufferedReader reader = new BufferedReader(in);
        if (list == null)
            list = new ArrayList<>();
        String line = null;
        while ((line = reader.readLine()) != null) {
            if (trim)
                line = line.trim();
            if (!trim || (line.length() > 0))
                list.add(line);
        }
    }
}

Related

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