JavaFile筛选读取文件内容

来源:互联网 发布:js判断是否有class 编辑:程序博客网 时间:2024/06/06 12:24

代码如下;【思路注释在代码里】

public class LoginfoUtils {public static String readWantedText(String FileName, String wantedOne, String wantedTwo) {String Str = "";try {  //获取对应文件  String LogAddress = configuration.getString("log.address");  File f = new File(LogAddress+"//"+FileName+".log");  //防止读取文件内容时乱码  InputStreamReader read = new InputStreamReader(new FileInputStream(f),"UTF-8");  BufferedReader br = new BufferedReader(read);  //用于临时保存每次读取的内容  String temp = "";  while (temp != null) {  temp = br.readLine();  //筛选条件  if (temp != null && temp.contains(wantedOne) && temp.contains(wantedTwo)) {  Str+=temp+";";   }  }    } catch (FileNotFoundException e) {    e.printStackTrace();    } catch (IOException e) {    e.printStackTrace();    }    return Str;  }}

原创粉丝点击