Java Text File Read Line readLinesNoComments(String fileName)

Here you can find the source of readLinesNoComments(String fileName)

Description

read Lines No Comments

License

Open Source License

Declaration

public static List<String> readLinesNoComments(String fileName) throws IOException 

Method Source Code


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

import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;

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

public class Main {
    public final static String UTF8 = "UTF-8";

    public static List<String> readLinesNoComments(String fileName) throws IOException {
        return readLinesNoComments(fileName, null);
    }//w  w w  .j av  a2  s  .c o  m

    public static List<String> readLinesNoComments(String fileName, String encoding) throws IOException {
        List<String> lines = new ArrayList<>();
        try (Scanner scanner = openScanner(fileName, encoding)) {
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine().trim();
                if (line.length() > 0) {
                    if (line.startsWith("#") == false) {
                        lines.add(line);
                    }
                }
            }
        }
        return lines;
    }

    public static final Scanner openScanner(File file) throws IOException {
        return openScanner(file, null);
    }

    public static final Scanner openScanner(String fileName) throws IOException {
        return openScanner(new File(fileName), null);
    }

    public static final Scanner openScanner(String fileName, String encoding) throws IOException {
        return openScanner(new File(fileName), encoding);
    }

    public static final Scanner openScanner(File file, String encoding) throws IOException {
        if (encoding == null) {
            encoding = UTF8;
        }
        FileInputStream fis = new FileInputStream(file);
        return new Scanner(fis, encoding);
    }
}

Related

  1. readLines(String text)
  2. readLines2Lowcase(String inputFile, ArrayList lines)
  3. readLinesAsList(String filename)
  4. readLinesFileSimple(final File inFile, final int prefixMode, final String prefix)
  5. readLinesFromCommand(String command[], List buffer)
  6. readLinesRaw(final File inFile)
  7. readLinesTrimmedNoComment(final String fileName, final String commentString)
  8. readLinesWithPattern(File file, Pattern pattern)