一、首先我们要获取Logcat中的日志
如何获取呢?
首先我们要先定义一个String[]
数组,里面的代码是
//第一个是Logcat ,也就是我们想要获取的log日志//第二个是 -s 也就是表示过滤的意思//第三个就是 我们要过滤的类型 W表示warm ,我们也可以换成 D :debug, I:info,E:error等等String[] running = new String[]{"logcat","-s","adb logcat *: W"};复制代码
当我们设置好之后,我们还需要一个process
类,作用通俗来讲就是用Java
代码来进行adb
命令行操作代码是:
Process exec = Runtime.getRuntime().exec(running);复制代码
通过以上的方法我们就可以获得和过滤Logcat中的方法。
二、接下来开始使用IO流进行字符操作,把数据保存在Android SDCard
中
首先:我们定义一个InputStream
,
final InputStream is = exec.getInputStream复制代码
接下来开启一个线程,线程中的方法就是通过IO流先读取Logcat
中的数据,然后再把数据通过OutPutStream
方法写入到SDCard
中。
new Thread() {@Overridepublic void run() {FileOutputStream os = null;try {//新建一个路径信息os = new FileOutputStream("/sdcard/Log/Log.txt");int len = 0;byte[] buf = new byte[1024];while (-1 != (len = is.read(buf))) {os.write(buf, 0, len);os.flush();}} catch (Exception e) {Log.d("writelog","read logcat process failed. message: "+ e.getMessage());} finally {if (null != os) {try {os.close();os = null;} catch (IOException e) {// Do nothing}}}}}.start();} catch (Exception e) {Log.d("writelog","open logcat process failed. message: " + e.getMessage());}}复制代码
当我们这个类写完之后,我们再把权限添加进去就可以了。
<!-- 读取Log权限 --><uses-permission android:name="android.permission.READ_LOGS" /><!-- 在SDCard中创建与删除文件权限 --><uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /><!-- 往SDCard写入数据权限 --><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><!-- 从SDCard读出数据权限 --><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />复制代码
添加完权限,我们运行试试。
然后我们再打开我们的SDCard
中的文件目录:
这样我们就已经获取到了Logcat
中的日志(可以和控制台的对比一下):
由于我开启了两次所以打印出了两次的log
.
三、之后我们先创建页面,然后在按行读取Txt文本中的内容
首先我们开始编写XMl
视图文件:
<LinearLayout xmlns:android="/apk/res/android"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".MainActivity" ><LinearLayout android:layout_width="match_parent"android:layout_weight="7"android:orientation="vertical"><ListView android:id="@+id/ListLog"android:layout_width="match_parent"android:layout_height="match_parent"></ListView></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_weight="1"android:gravity="center"android:orientation="horizontal" ><Button android:layout_gravity="center"android:gravity="center"android:id="@+id/BtnLog"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="清空日志"/></LinearLayout></LinearLayout>复制代码
编写完成后,我们开始在MainActivity
里面初始化我们的类
private ListView listView;private Button btn;listView = (ListView) findViewById(R.id.ListLog);btn = (Button) findViewById(R.id.BtnLog);复制代码
之后,我们开始编写我们的读取TXT
文件的方法
/** * 根据行读取内容 * @return */ public List<String> Txt() { //将读出来的一行行数据使用List存储 String filePath = "/sdcard/Log.txt"; List newList=new ArrayList<String>(); try { File file = new File(filePath); int count = 0;//初始化 key值 if (file.isFile() && file.exists()) {//文件存在 InputStreamReader isr = new InputStreamReader(new FileInputStream(file)); BufferedReader br = new BufferedReader(isr); String lineTxt = null; while ((lineTxt = br.readLine()) != null) { if (!"".equals(lineTxt)) { String reds = lineTxt.split("\\+")[0]; //java 正则表达式 newList.add(count, reds); count++; } } isr.close(); br.close(); }else { Log.e("tag", "can not find file");} } catch (Exception e) { e.printStackTrace(); } return newList; } 复制代码
我们看d的代码,其实也就是IO读写操作
if (file.isFile() && file.exists()) //这一行是判断是否有文件存在复制代码
然后我们用InputStreamReader
读取我们SDCard
中的文件;
使用BufferedReader
方法读取我们获取的字符流;
最后我们用While
循环和正则表达式来把每一行都给放入List中;
最后我们返回List
;
InputStreamReader isr = new InputStreamReader(new FileInputStream(file)); BufferedReader br = new BufferedReader(isr); String lineTxt = null; while ((lineTxt = br.readLine()) != null) { if (!"".equals(lineTxt)) { String reds = lineTxt.split("\\+")[0]; //java 正则表达式 newList.add(count, reds); count++; } } 复制代码
还有一个XML
视图文件,名称log_list_item.xml
<TextView xmlns:android="/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent" android:textColor="#000000"android:gravity="left"android:paddingLeft="20dp"android:textSize="20sp"android:singleLine="true"/>复制代码
接下来就是把List
放入ListView
中:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.log_list_item,Txt());listView.setAdapter(adapter);复制代码
好让我们运行一下看看效果:
好了,我们的显示日志也已经成功了。接下来就是要可以清空日志;
最后、清空日志
如何清空日志呢?
其实非常简单
/*** 删除Log文件* @param fileName 文件路径和名称*/public static void delFile(String fileName){ File file = new File(fileName); if(file.isFile()){ file.delete(); } file.exists(); } 复制代码
我们只需要把路径传过去,进行判断,如果有就直接删除。
然后我们对ListView
进行刷新就可以了。