Here you can find the source of readLines(File file, int maxArraySize)
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. |
Parameter | Description |
---|---|
IOException | if an IO problem occurs. |
public static String[] readLines(File file, int maxArraySize) throws IOException
//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()]); } } }