編寫通用的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();
}
}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章