Java InputStream Read Line readLines(InputStream in, int maxArraySize)

Here you can find the source of readLines(InputStream in, int maxArraySize)

Description

read Lines

License

Open Source License

Parameter

Parameter Description
in the input stream to read.
maxArraySize the maximum number of lines this method will return. If this is negative then it is ignored and all lines will always be read.

Exception

Parameter Description
IOException if an IO problem occurs.

Return

all the lines of text in a text file.

Declaration

public static String[] readLines(InputStream in, int maxArraySize) throws IOException 

Method Source Code

//package com.java2s;
/**//from w  ww  .  j a  va2  s.c  o m
 * This software is released as part of the Pumpernickel project.
 * 
 * All com.pump resources in the Pumpernickel project are distributed under the
 * MIT License:
 * https://raw.githubusercontent.com/mickleness/pumpernickel/master/License.txt
 * 
 * More information about the Pumpernickel project is available here:
 * https://mickleness.github.io/pumpernickel/
 */

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

import java.io.FileInputStream;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
     * @return all the lines of text in a text file.
     * 
     * @see #writeLines(File, String[], boolean)
     * @param file
     *            the file to read
     * @param maxArraySize
     *            the maximum number of lines this method will return. If this
     *            is negative then it is ignored and all lines will always be
     *            read.
     * @throws IOException
     *             if an IO problem occurs.
     */
    public static String[] readLines(File file, int maxArraySize) throws IOException {
        try (FileInputStream in = new FileInputStream(file)) {
            return readLines(in, maxArraySize);
        }
    }

    /**
     * @return all the lines of text in a text file.
     * 
     * @see #writeLines(File, String[], boolean)
     * @param in
     *            the input stream to read.
     * @param maxArraySize
     *            the maximum number of lines this method will return. If this
     *            is negative then it is ignored and all lines will always be
     *            read.
     * @throws IOException
     *             if an IO problem occurs.
     */
    public static String[] readLines(InputStream in, int maxArraySize) throws IOException {
        try (BufferedReader br = new BufferedReader(new InputStreamReader(in))) {
            List<String> strings = new ArrayList<String>();
            String s = br.readLine();
            while (s != null && (maxArraySize < 0 || strings.size() < maxArraySize)) {
                strings.add(s);
                s = br.readLine();
            }
            return strings.toArray(new String[strings.size()]);
        }
    }
}

Related

  1. readLines(InputStream in)
  2. readLines(InputStream in)
  3. readLines(InputStream in)
  4. readLines(InputStream in)
  5. readLines(InputStream in)
  6. readLines(InputStream in, String charset)
  7. readLines(InputStream in, String encoding)
  8. readLines(InputStream input)
  9. readLines(InputStream input)