Here you can find the source of slurpLines(final Reader reader)
Parameter | Description |
---|---|
reader | source to read lines from |
Parameter | Description |
---|---|
IOException | on any read error |
IllegalArgumentException | if source is null |
public static String[] slurpLines(final Reader reader) throws IOException
import org.apache.log4j.Logger; import java.io.*; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main{ private static final Logger log = Logger.getLogger(FileUtil.class); /**//from w w w. j a v a2s. c o m * Read all lines from a file. * * @param reader source to read lines from * @return all lines from file * @throws IOException on any read error * @throws IllegalArgumentException if source is null */ public static String[] slurpLines(final Reader reader) throws IOException { checkSourceNotNull(reader); final BufferedReader bufferedReader = new BufferedReader(reader); final List<String> lines = new ArrayList<String>(); String line; while ((line = bufferedReader.readLine()) != null) { lines.add(line); } closeCleanly(bufferedReader, "reader for slurpLines"); return lines.toArray(new String[lines.size()]); } private static void checkSourceNotNull(final Object o) { if (o == null) { throw new IllegalArgumentException( "Cannot read from source that is null"); } } /** * Close the given item without throwing an exception, instead logging it at * the ERROR level. * * @param stream thing to close * @param identifier identifier to put into the logging message */ public static void closeCleanly(final Closeable stream, final String identifier) { if (stream == null) return; try { stream.close(); } catch (final IOException e) { log.error(identifier + ": Failed to close properly: " + e.getMessage()); } } }