Android FileOutputStream日志

Android FileOutputStream日志,android,fileoutputstream,Android,Fileoutputstream,对于我的应用程序,我想创建一个类,在这个类中我可以将数据记录到一个文件中。在我的应用程序中,如果他们遇到问题,可以向我发送电子邮件,此文件中的数据可能有助于我分析问题。首先,我是不是走错了路?是否有更好的方法记录发生的异常 问题是,如果我尝试使用另一个类的log方法,使用: Logger.log(0,"","",""); 它无法找到该文件,或者如果尚未创建该文件,则无法创建该文件。代码附在下面 package com.example.test; import java.io.File; im

对于我的应用程序,我想创建一个类,在这个类中我可以将数据记录到一个文件中。在我的应用程序中,如果他们遇到问题,可以向我发送电子邮件,此文件中的数据可能有助于我分析问题。首先,我是不是走错了路?是否有更好的方法记录发生的异常

问题是,如果我尝试使用另一个类的log方法,使用:

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();
    }
}

}

这是我自己的应用程序中的代码片段:

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%");
    }       
}

有什么原因不能使用log4j或slf4j之类的标准日志库吗?我认为这些可以满足您的需要,而无需自己编写Logger类。Android上的slf4j看起来仍处于测试阶段,但有传闻称人们已经成功地使用了它。

文件名不足以编写文件。与使用openFileOutput时不同,使用FileOutputStream时需要提供一个目录。因此,如果您更改此行:

final static String FileName = "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();
    }
}
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();
    }
}