Android將需要的日誌文件LOG記錄到本地文件夾下指定的文件

package com.mapbar.android.obd.util;

import android.os.Environment;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by sunxx on 2017/12/14.
 * 日誌文件
 */

public  class Logs {
    public static String TIRE_TAG="1";
    public static String PHY_TAG="2";
    public static String STATE_TAG="3";
    public static boolean isDebug=false;
    public final static String FILE_PATH = "/aa/bb";//包名文件夾
    public static File file;
    public  static FileOutputStream fos;
    public static Logs logs;
    public Logs() {
    }
    public static synchronized Logs getInstance(){
        String path = Environment.getExternalStorageDirectory().getAbsolutePath() + FILE_PATH + "/log/a.txt";
        try {
                file = new File(path);                  
                fos = new FileOutputStream(file, true);
           
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        if (logs==null){
            logs=new Logs();
        }
        return logs;
    }
    //寫入日誌文件
    public  void writeEvent(String tag,String msg){
        if (!isDebug){
            return;
        }
        if (fos==null){
            return;
        }
        try
        {
            fos.write((TimeFormatUtil.formatStrLong(System.currentTimeMillis())+"|"+tag+"|"+msg+"\r").getBytes());
            fos.flush();
            fos.close();
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

發佈了61 篇原創文章 · 獲贊 50 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章