java 读取文件中指定内容

public static void readWantedText(String url, String wanted) {
try {
FileReader fr = new FileReader(url);
BufferedReader br = new BufferedReader(fr);
String temp = "";// 用于临时保存每次读取的内容
while (temp != null) {
temp = br.readLine();
if (temp != null && temp.contains(wanted)) {
System.out.println(temp);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
readWantedText("D:\\新建文件夹\\1.txt", "#");
}

posted on 2021-01-14 18:11  小泥爪子  阅读(652)  评论(0)    收藏  举报

导航