欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JAVA 判断某个文件中是否包含指定字符串

程序员文章站 2024-01-15 12:16:04
...
public class FindStringInTxt {
	String encoding = "UTF-8";
	public static void main(String[] args) throws IOException {
		findStringInFile("C://1.TXT");
	}
	
	public static void findStringInFile(String path) throws IOException{
		File file = new File(path);
        InputStreamReader read = new InputStreamReader(new FileInputStream(file),"UTF-8");//考虑到编码格式
        BufferedReader bufferedReader = new BufferedReader(read);
		String line = null;
		while ((line = bufferedReader.readLine()) != null) {
			if(line.startsWith("#")){
				continue;
			}
			//指定字符串判断处
			if (line.contains("xxyy")) {
				System.out.println(line);
			}
		}
	}
}

相关标签: java