本文主要使用Java對文件操作功能,來實現測試中log內容的輸出
1、前提條件
在被測試的應用中添加對SD卡的讀寫權限,文件爲AndroidManifest.xml,添加以下標示
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" ></uses-permission>
2、使用StringBuffer來自定義方法StringBufferDemo(String url, String str)
- public void StringBufferDemo(String url, String str) throws IOException{
- File file=new File(url);
- if(!file.exists())
- file.createNewFile();
- FileOutputStream out=new FileOutputStream(file,true);
- StringBuffer sb=new StringBuffer();
- sb.append(str); //直接在文件中追加文字
- out.write(sb.toString().getBytes("utf-8"));
- out.close();
- }
url:指定生成的log文件, 例如:url = "/sdcard/sms.log"
str:需要輸出的log信息, 例如:str= "test"
直接在testcase中調用該方法即可
String log_url = "/sdcard/sms.log" ;
String log_text = "test" ;
StringBufferDemo(log_url,log_text) ;