Java InputStream Read Line readLines(InputStream is)

Here you can find the source of readLines(InputStream is)

Description

read Lines

License

Open Source License

Declaration

public static List<String> readLines(InputStream is) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedReader;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static List<String> readLines(InputStream is) throws IOException {
        return readLines(is, true, new String[] { "#", "//" });
    }/*from  w  ww .j  ava  2s. c om*/

    /**
     * Reads all lines from an InputStream ignoring lines that start with commenChar if ignoreComments is true
     */
    public static List<String> readLines(InputStream is, boolean ignoreComments, String... commentChars)
            throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        String line = null;
        List<String> result = new ArrayList<String>();
        while ((line = reader.readLine()) != null) {
            //ignore empty lines
            if (line.trim().equals("")) {
                continue;
            }

            //test comments
            boolean ignoreLine = false;
            if (ignoreComments) {
                for (String commentChar : commentChars) {
                    if (line.startsWith(commentChar)) {
                        ignoreLine = true;
                        break;
                    }
                }
            }

            if (!ignoreLine) {
                result.add(line);
            }
        }
        return result;
    }
}

Related

  1. readLines(InputStream inputStream)
  2. readLines(InputStream inputStream)
  3. readLines(InputStream inputStream)
  4. readLines(InputStream is)
  5. readLines(InputStream is)
  6. readLines(InputStream is)
  7. readLines(InputStream is)
  8. readLines(InputStream is)
  9. readLines(InputStream is)