Android log file output

Source: Internet
Author: User
<span id="Label3"></p><p><p><span style="font-size:24px; color:#CC0000">Android log file output</span></p></p><p><p>Usage:<br></p></p><p><p>try {<br><br>} catch (Exception E) {<br><span style="font-size:18px; color:#FF0000">logutil.logexception (e);</span><br>}<br></p></p><p><p>Package com.hexun.util;</p></p><br>Import java.io.BufferedReader;<br>Import java.io.File;<br>Import java.io.FileInputStream;<br>Import java.io.InputStreamReader;<br>Import java.io.PrintWriter;<br>Import java.io.RandomAccessFile;<br>Import java.io.StringWriter;<br><br>Import android.os.Environment;<br>Import android.util.Log;<br><br>/**<br>* Log Printing<br>*/<br>public class Logutil {<br><br><span style="color:#CC0000"><span style="color:#CC0000">private static final String file_name = "/logs.txt";<br>private static Boolean Mylog_switch = true; Log file Total switch<br></span></span><br>public static void I (string tag, string Str) {<br>LOG.I (tag, str.tostring ());<br>}<br><br>public static void Logexception (Exception E) {<br>If (mylog_switch) {<br>StringWriter SW = new StringWriter ();<br>PrintWriter pw = new PrintWriter (sw);<br>E.printstacktrace (pw);<br>String str = sw.tostring ();<br>WriteException (str);<br>} else {<br>E.printstacktrace ();<br>}<br>}<br><br>private static void WriteException (String Content) {<br>try {<br>If (environment.getexternalstoragestate (). equals (<br>ENVIRONMENT.MEDIA_MOUNTED)) {<br>Get the SD card directory<br>File Sdcarddir = environment.getexternalstoragedirectory ();<br>File TargetFile = new file (sdcarddir.getcanonicalpath ()<br>+ file_name);<br>Create a Randomaccessfile object with the specified file<br>Randomaccessfile RAF = new Randomaccessfile (targetfile, "rw");<br>Move the file record pointer to the last<br>Raf.seek (targetfile.length ());<br>Output file contents<br>Raf.write (content.getbytes ());<br>Raf.close ();<br>}<br>} catch (Exception E) {<br>E.printstacktrace ();<br>}<br>}<br><br>private static String readexception () {<br>try {<br>If (environment.getexternalstoragestate (). equals (<br>ENVIRONMENT.MEDIA_MOUNTED)) {<br>Get the storage directory for the SD card<br>File Sdcarddir = environment.getexternalstoragedirectory ();<br>Gets the input stream corresponding to the specified file<br>FileInputStream fis = new FileInputStream (<br>Sdcarddir.getcanonicalpath () + file_name);<br>Wraps the specified input stream into Bufferreader<br>BufferedReader br = new BufferedReader (new InputStreamReader (<br>fis));<br>StringBuilder sb = new StringBuilder ("");<br>String line = null;<br>Looping through the contents of a file<br>While (line = Br.readline ()) = null) {<br>Sb.append (line);<br>}<br>Br.close ();<br>return sb.tostring ();<br>}<br>} catch (Exception E) {<br>E.printstacktrace ();<br>}<br>Return null;<br>}<br><br>}<p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>Android log file output</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.