首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FileOutputStream日志

FileOutputStream日志
EN

Stack Overflow用户
提问于 2012-12-21 04:06:00
回答 3查看 1.9K关注 0票数 1

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

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

代码语言:javascript
复制
Logger.log(0,"","","");

它无法找到该文件,或者如果该文件尚未创建,则无法创建该文件。代码附在下面。

代码语言:javascript
复制
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();
    }
}

}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-12-21 08:38:36

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

代码语言:javascript
复制
final static String FileName = "Log";

代码语言:javascript
复制
final static String FileName = "data/data/com.example.test/files/Log";

这会解决这个问题。最后,在访问类中的方法时,不会调用oncreate。您必须将这两者结合起来,如下所示:

代码语言:javascript
复制
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();
    }
}
票数 0
EN

Stack Overflow用户

发布于 2012-12-21 04:12:09

有没有什么原因不能使用像log4j或slf4j这样的标准日志库?我认为这些可以满足您的需求,而不需要自己编写Logger类。安卓系统上的slf4j看起来还处于测试阶段,但一些轶事表明,人们已经成功地使用了它。

票数 2
EN

Stack Overflow用户

发布于 2012-12-21 04:11:40

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

代码语言:javascript
复制
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%");
    }       
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13979645

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档