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); } } } }