编写通用的java代码执行任意adb命令

package com.yougel.ExeAdb;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;

import java.util.Date;

public class ExeAdb {
public FileWriter fileWriter;
public BufferedReader reader;
public Process process;
public File file=new File(“log.txt”);//因为多个方法要使用该对象,所以提取出来
//执行adb命令的方法
public void exeAdb(String adb){
try {
if(!file.exists()){
file.createNewFile();
}
String line=null;
fileWriter=new FileWriter(file,true);
process=Runtime.getRuntime().exec(adb);
//用UTF-8会中文乱码?
reader=new BufferedReader(new InputStreamReader(process.getInputStream(),”GBK”));
while((line=reader.readLine())!=null){
fileWriter.write(line+”\r\n”);
System.out.println(line);
}
}
catch (IOException e) {
e.printStackTrace();
}
finally{
try {
reader.close();
fileWriter.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
//判断文件是否大于200K
public boolean isOutTwoHandred(){
if((file.length()/1024)>=200)
return true;
else
return false;
}
//归档文件的方法
public void pigeonhole(){
try {
FileInputStream fis=new FileInputStream(file);
reader=new BufferedReader(new InputStreamReader(fis, “GBK”));
SimpleDateFormat timeFormat=new SimpleDateFormat(“yyyy-MM-dd HH:mm”);
//归档文件名的格式采用:当前时间_log.txt
SimpleDateFormat timeFormat1=new SimpleDateFormat(“yyyyMMddHHmm”);
String string1=timeFormat1.format(new Date());
String string=timeFormat.format(new Date());
File newFile=new File(string1+”_log.txt”);
if(!newFile.exists()){
newFile.createNewFile();
}
FileWriter fw=new FileWriter(newFile);
fw.write(“*”+string+”**\r\n”);
String line=null;
while((line=reader.readLine())!=null){
fw.write(line+”\r\n”);
// System.out.println(line);
}
fis.close();
reader.close();
fw.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值