2012-12-20 59 views
1

對於我的應用程序,我想創建一個類,我可以將數據記錄到文件中。在我的應用程序中,如果遇到問題,可以通過電子郵件向我發送電子郵件,此文件中的數據可能會幫助我解決問題。首先,我是否以錯誤的方式去做這件事?有沒有更好的方法來記錄發生的異常?FileOutputStream日誌

的問題是,如果我嘗試使用日誌方法使用另一個類:

Logger.log(0,"","",""); 

它無法找到該文件或事實上,如果尚未創建它創建該文件。代碼附在下面。

package com.example.test; 

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

import android.app.Activity; 
import android.content.Context; 
import android.os.Build; 
import android.os.Bundle; 
import android.text.format.Time; 
import android.util.Log; 

public class Logger extends Activity { 

final static String FileName = "Log"; 
static FileOutputStream fos; 
static FileInputStream fis; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    Log.d("logger", "oncreate"); 
    try { 
     File f = getFileStreamPath(FileName); 
     if (!f.exists()) { 
      Log.d("logger", "file doesn't exist"); 
      fos = openFileOutput(FileName, Context.MODE_APPEND); 
      fos.write(("Created on " + Build.TIME + "\nDevice name: " 
        + Build.MODEL + " \nAndroid Version" + Build.VERSION.SDK_INT) 
        .getBytes()); 
      fos.close(); 
     } 
     fos = openFileOutput(FileName, Context.MODE_APPEND); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

} 

public static void log(int type, String TAG, String msg) { 
    Log.d("logger", "file being written"); 
    Log.println(type, TAG, msg); 
    Time now = new Time(); 
    now.setToNow(); 
    try { 
     fos = new FileOutputStream(FileName); 
     fos.write(("\n" + now.toString() + " " + type + " " + TAG + " " + msg).getBytes()); 
     fos.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

} 

回答

0

文件名不足以寫入文件。與使用openFileOutput時不同,使用FileOutputStream時需要提供目錄。因此,如果你改變這一行:

final static String FileName = "Log"; 

final static String FileName = "data/data/com.example.test/files/Log"; 

這將解決這個問題。最後,在訪問類中的方法時不會調用oncreate。你必須將兩者結合如下:

final static String FileName = "data/data/com.example.test/files/Log"; 
static FileOutputStream fos; 

public static void log(int priority, String tag, String msg) { 
    Log.println(priority, tag, msg); 
    try { 
     Time now = new Time(); 
     now.setToNow(); 
     File f = new File(FileName); 
     if (!f.exists()) { 
      Log.i("Logger", "Creating Log file"); 
      fos = new FileOutputStream(f, true); 
      fos.write(("Created on " + now.toString().subSequence(0, 15) 
        + "\nDevice name: " + Build.MODEL 
        + " \nAndroid Version " + Build.VERSION.SDK_INT) 
        .getBytes()); 
      fos.close(); 
     } 
     fos = new FileOutputStream(f, true); 
     Log.d("file", now.toString()); 
     fos.write(("\n" + now.toString().substring(4, 15) + " " + priority 
       + " " + tag + " " + msg).getBytes()); 
     fos.close(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 
1

這是我自己的應用程序的代碼片段:

public static void writeLogToFile(String totalFilePath, String myError) { 
    FileWriter fWriter; 
try { 

    fWriter = new FileWriter(totalFilePath, true); 
    fWriter.append("\n"); 
    fWriter.append("{" 
      + android.text.format.DateFormat.format(
        "yyyy-MM-dd kk:mm:ss", new java.util.Date()) + "} "); 
    fWriter.append(myError); 
    fWriter.flush(); 
    fWriter.close(); 

} catch (IOException e) { 
    CreateLog.addToLog(e.toString()); 
} 




public static void checkExists(String totalPath, String fileName) { 
    File path = new File(totalPath); 
    if (!(path.exists())) { 
     // Folder does not exists 
      createFolder(totalPath); 

    } 

    File myFile = new File(totalPath + fileName); 
    if (!(myFile.exists())) { 
     // File does not exists 
     writeToFile(totalPath, fileName, "%TEMP%"); 
    }  
} 
2

是否有一個原因,你不能使用標準的日誌庫,像log4j的或SLF4J?我認爲這些可以滿足您的需求,而無需親自編寫Logger類。看起來Android上的slf4j仍處於測試階段,但傳聞表明人們使用它取得成功。