Android Reader Read slurpValidLines(final Reader reader)

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

Description

Read lines from a reader but skip blank lines and those beginning with a '#'

Parameter

Parameter Description
reader reader for lines

Exception

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

Return

valid lines from file, trimmed

Declaration

public static String[] slurpValidLines(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 av  a  2  s  .  c  o  m
     * Read lines from a reader but skip blank lines and those beginning with a '#'
     *
     * @param reader reader for lines
     * @return valid lines from file, trimmed
     * @throws IOException on any read error
     * @throws IllegalArgumentException if reader is null
     */
    public static String[] slurpValidLines(final Reader reader)
            throws IOException {
        final List<String> validLines = new ArrayList<String>();
        for (String line : slurpLines(reader)) {
            line = line.trim();
            if (line.length() == 0 || line.charAt(0) == '#') {
                continue;
            }
            validLines.add(line);
        }
        return validLines.toArray(new String[validLines.size()]);
    }
    /**
     * 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. readContents(Reader in)
  2. readFile(String filename)
  3. readFileReader(String filePath)
  4. readTextFile(String fileName)
  5. slurpLines(final Reader reader)
  6. toList(Reader reader)
  7. nextString(Reader r)
  8. nextString(Reader r, char c)
  9. nextPairedString(Reader r, char b, char c)