import java.io.*;public class Keyword{ public void test(String keyword)throws IOException{ String fileName = "MagazineList.txt"; FileReader fr = new FileReader(fileName); BufferedReader br = new BufferedReader(fr); String nextLine; int line=1; while((nextLine=br.readLine())!=null){ if(nextLine.indexOf(keyword)>-1){ System.out.println(line+"行 "+nextLine); } line++; } } public static void main(String[] args)throws IOException{ new Keyword().test("java"); }}一个完整的例子,请参考
java 编程,搜索txt中关键字
本文来自网络,不代表本站立场,转载请注明出处:https://www.3nbk.com/at79165.html