Java Text File Read Line readLines(File file, int maxArraySize)

Here you can find the source of readLines(File file, int maxArraySize)

Description

read Lines

License

Open Source License

Parameter

Parameter Description
file the file 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(File file, int maxArraySize) throws IOException 

Method Source Code

//package com.java2s;
/**//from w  ww. java 2s.  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(File file)
  2. readLines(File file)
  3. readLines(File file)
  4. readLines(File file)
  5. readLines(File file, boolean ignoreComments)
  6. readLines(File file, String charset)
  7. readLines(File file, String charsetName)
  8. readLines(File file, String encoding)
  9. readLines(File file, String encoding)