PC通过ADB命令获取Android制定文件内容并保存在本地

开始的想法是通过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);
		}
	}


 















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值