去掉代码中的注释和空白行

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.function.Predicate;
import java.util.stream.Stream;

/**
 * 从文本中读取脚本代码,去掉注释("#") 和空白行
 * 
 * @author Felix
 *
 */
public class StreamFilterDemo1 {

    public static void main(String[] args) {
    Predicate<String> notCommentOrEmptyLine = (line) -> line.trim().length() > 0 && !line.trim().startsWith("#");
    try (FileReader fr = new FileReader("src/example.txt"); BufferedReader br = new BufferedReader(fr)) {
        Stream<String> lines = br.lines();
        lines.filter(notCommentOrEmptyLine).forEach(System.out::println);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    }

}

example.txt文件中的内容:

# Set path so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"

fi
原文地址:https://www.cnblogs.com/IcanFixIt/p/4325970.html