Android Reader Read slurpLines(final Reader reader)

Here you can find the source of slurpLines(final Reader reader)

Description

Read all lines from a file.

Parameter

Parameter Description
reader source to read lines from

Exception

Parameter Description
IOException on any read error
IllegalArgumentException if source is null

Return

all lines from file

Declaration

public static String[] slurpLines(final Reader reader)
        throws IOException 

Method Source Code

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());
        }
    }
}

Related

  1. copy(Reader input, Writer output)
  2. readContents(Reader in)
  3. readFile(String filename)
  4. readFileReader(String filePath)
  5. readTextFile(String fileName)
  6. slurpValidLines(final Reader reader)
  7. toList(Reader reader)
  8. nextString(Reader r)
  9. nextString(Reader r, char c)