开始的想法是通过Runtime.getRuntime().exec()方法执行adb 的pull命令直接保存在本地,但是代码执行下面的代码
String command = "cmd.exe /c adb pull /sdcard/QQ20150226172954.csv C:\test";
并没有成功的pull到本地,也没有错误信息输出。不知道神马情况,就想着在shell下通过cat命令查看该文件内容,并将该内容输出
Process pro = Runtime.getRuntime().exec(command);
StringBuffer out = new StringBuffer();
byte[] b = new byte[1024];
for(int n; (n=pro.getInputStream().read(b))!=-1; ){
out.append(new String(b,0,n,"GBK"));
}
createFile(filePath, out.toString(),false);
写入到本地,我得文件是csv格式的,貌似用utf-8编码输出乱码。下面是本地创建file的方法
/**
*
* @param absolutePath
* @param defaultContent
* @param append,
* true append the defaultContents to the end of file
* false overwrite the contents
* @return
*/
public static boolean createFile(String absolutePath, String defaultContent, boolean append ) {
if (absolutePath == null || "".equals(absolutePath.trim())) {
throw new RuntimeException("The absolute path is empty");
}
File file = new File(absolutePath);
BufferedWriter writer = null;
if (!file.exists()) {
try {
writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(file,append), "GBK"));
writer.write(defaultContent);
writer.flush();
if (writer != null) {
writer.close();
}
} catch (Exception e) {
e.printStackTrace();
}<pre name="code" class="java">
<span style="white-space:pre"> </span>下面是自己写的读取csv格式文件的方法,一并贴上来吧。做个备份,希望也能对其他人有所帮助。
<pre name="code" class="html">public static void readCsv(String csvFilePath){
try {
CsvReader reader = new CsvReader(csvFilePath,',',
Charset.forName("GBK"));//the csv file
//read header of the form
reader.readHeaders();
String[] headers = reader.getHeaders();
//read and save the contents of the form
List<Object[]> list = new ArrayList<Object[]>();
while (reader.readRecord()) {
list.add(reader.getValues());
}
findIndicatorPara(list, "PASS_RATE");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void findIndicatorPara(List<Object[]> list, String indicator ){
int flag = 0;
List<String> strList = new ArrayList<String>();
for(int i=0;i<list.size();i++){
Object[] data = list.get(i);
if(data.length>10){
for (int j = 0; j < data.length; j++) {
Object cell = data[j];
if(cell.toString().trim().contains(indicator)){
flag = j;
break;
}
}
strList.add(data[flag].toString());
}else{
continue;
}
}
Collections.sort(strList);
for(String s:strList){
System.out.println(s);
}
}