1. 程式人生 > >Android 列印輸出資訊

Android 列印輸出資訊

為了以後便於查詢

/**列印資訊類*/
public class FileWrite {

    /**根目錄*/
    private  static final String SDCARD_PATH = Environment.getExternalStorageDirectory().getPath();         
    private  static final String LOG_FILE_PATH = SDCARD_PATH + File.separator+"lou"+File.separator+"log.log";   
    private  static final
String parentpath = SDCARD_PATH + File.separator+"lou"; private static final String mCharset = "utf-8"; private static FileWrite fw = null; private static FileOutputStream mFos; private static File mLogFile; private static File parentFile; private FileWrite(){ //判斷檔案是否存在
createIfNotExist(LOG_FILE_PATH); try { //此處判斷檔案是否存在,若存在則會追加 否則建立檔案 mFos = new FileOutputStream(mLogFile, true); // mFos = new FileOutputStream(LOG_FILE_PATH, true); } catch (FileNotFoundException e) { Log.e("lou", "FileWrite FileNotFoundException"
); e.printStackTrace(); } } /**建立檔案 判斷檔案是否存在 * @param path 檔案路徑*/ private void createIfNotExist(String path) { parentFile = new File(parentpath); if(!parentFile.exists()){ parentFile.mkdir(); } mLogFile = new File(path); if(!mLogFile.exists()){ try { mLogFile.createNewFile(); } catch (IOException e) { Log.e("lou", "FileWrite.createIfNotExist createNewFile failed"); e.printStackTrace(); } } } public static FileWrite getInstance(){ if(fw == null){ fw = new FileWrite(); } return fw; } /** * 此處是呼叫的介面 * @param content 列印的內容*/ public void WriteString(String content){ Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); content = sdf.format(date)+":"+content; byte[] data; try { data = content.getBytes(mCharset); writeByte(data); } catch (UnsupportedEncodingException e) { Log.e("lou", ""+e.getMessage()); e.printStackTrace(); } } /**寫檔案 * @param data 資料*/ private boolean writeByte(byte[] data) { try { mFos.write(data); mFos.write("\r\n".getBytes()); mFos.flush(); return true; } catch (IOException e) { Log.e("lou", "writeByte failed"); e.printStackTrace(); } return false; } }