题:查找某个文件中出现指定字符ERROR的记录并打印出来 本人写的如下,如有好的算法,请多多指教。 package com.teedry.interview; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; public class Find { //以行读取 public static void find(String fileName){ BufferedReader bufferedReader = null; //InputStreamReader reader = null; try { FileInputStream in = new FileInputStream(fileName); InputStreamReader reader = new InputStreamReader(in,"GBK"); bufferedReader = new BufferedReader(reader); String tempString = null; int line = 0; while ((tempString = bufferedReader.readLine()) !=null) { if(tempString.indexOf("ERROR")!= -1){ // 显示行号 // tempString = new String(tempString.getBytes(),"UTF-8"); System.out.println("line " + line + ": " + tempString); } line++; } } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args){ String fileName = "C:/server.log"; Find.find(fileName); } }