import java.io.*; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Demo1030a { public static boolean Containstr(String s1, String s2) { if (s1.indexOf(s2) >= 0) { return true; } else { return false; } } public static void main(String[] args) { File f = new File("e:/text.txt"); try { FileReader fr = new FileReader(f); BufferedReader br = new BufferedReader(fr); List<String> list = new ArrayList<String>(); String str = null; int num = 1; while ((str = br.readLine()) != null) { list.add(str); } System.out.println("请输入查找的单词"); Scanner sc = new Scanner(System.in); String strword = sc.nextLine(); for (String s : list) { boolean b = Containstr(s, strword); if (b) { System.out.println(num + ":" + s); } num++; } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
查找单词在文件中出现的位置及对应的行数
最新推荐文章于 2022-12-13 23:10:15 发布