例子1:
while ((line = reader.readLine()) != null) {
System.out.println(line);
saveToFile(line, "reportlog.log", false);
if (line.startsWith("INSTRUMENTATION_STATUS_CODE:")) {
status ++;
if (status == 2) {
System.out.println(getCode(line));
if (getCode(line).equalsIgnoreCase("-1")) {
runresult = "运行错误";
} else if (getCode(line).equalsIgnoreCase("-2")) {
runresult = "断言错误";
} else {
runresult = "运行成功";
}
}
}
if (line.startsWith("INSTRUMENTATION_STATUS: stack=")) {//截取异常
runinfo = line.substring(30, line.length());
}
if (line.startsWith(" at student.Special.")) {//截取出错行
errorlineinspecial = line.substring(line.indexOf(".")+1, line.length());
}
if (line.startsWith(" at student.Case.")) {//截取出错行
errorlineincase = line.substring(line.indexOf(".")+1, line.length());
}
if (line.startsWith(" at student.Library.")) {//截取出错行
errorlineinliabrary = line.substring(line.indexOf(".")+1, line.length());
}
}
例子2:
package example_1;
import java.lang.*;
import java.awt.List;
import java.io.*;
import java.io.IOException;
import java.lang.Exception;
import java.util.regex.Pattern;
public class Xample_6 {
public void Reader()throws IOException,Exception{
try{
Pattern pattern = Pattern.compile("Error in \\w+:"); //正则匹配
String p1 = "com.android.uiautomator.core.UiObjectNotFoundException"; //开头匹配
String p2 = "Tests run: \\d+, Failures: \\d+, Errors: \\d+"; //正则匹配
String p3 ="Tests run:"; //开头匹配
File file = new File("C:\\Users\\Administrator\\Desktop\\monkey\\log1.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String list = null;
while((list = (bufferedReader.readLine()))!= null){
if(list.matches(pattern.toString())){ //正则匹配
System.out.println(list);
}
if(list.startsWith(p1)){ //匹配字符串开始位置
System.out.println(list.substring(1, list.length())); //提取整段字符串
}
if(list.startsWith(p3)){
System.out.println(list.substring(1,list.length()));
}
}
bufferedReader.close();
fileReader.close();
}catch(IOException e1){
e1.printStackTrace();
}catch(Exception e2){
e2.printStackTrace();
}
}
public static void main(String[] args)throws Exception,IOException{
Xample_6 xample= new Xample_6();
xample.Reader();
}
}
本文来自博客园,作者:观心静 ,转载请注明原文链接:https://www.cnblogs.com/guanxinjing/p/9708677.html
本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。
分类:
Java 学习
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具