提取文档中的指定内容


例子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();
		
	}

}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值