如何将一个静态的日志文件到一个Android应用程序将不会关闭应用程序被删除? [英] How to include a static log file into an Android app that won't be removed on closing the app?

查看:160
本文介绍了如何将一个静态的日志文件到一个Android应用程序将不会关闭应用程序被删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想包括我的应用程序中的一个静态的日志文件。每当用户启动应用程序,额外信息的时间将被添加到该文件。刚开始,我还以为存储文件到资产的文件夹或文件夹的原料将是解决方案,但后来我看着的 Android的单证它规定:

I want to include a static log file within my app. Whenever the user starts the app, a time with extra information will be appended to that file. At the beginning, I thought storing the file into assets folder or raw folder would be the solution, but then I looked into android documentations where it states:

提示:如果您想保存静态文件在您在编译应用程序
  时间,将文件保存在您的项目RES /生/目录下。你可以打开
  它openRawResource(),传递R.raw。资源ID。
  此方法返回您可以用来读取文件的InputStream
  (,但你不能写入到原来的文件)。

Tip: If you want to save a static file in your application at compile time, save the file in your project res/raw/ directory. You can open it with openRawResource(), passing the R.raw. resource ID. This method returns an InputStream that you can use to read the file (but you cannot write to the original file).

我该如何解决这个问题呢?

How can I solve this problem?

编辑:

我要的不是要在关闭应用程序中删除的日志文件。

I want the log file not to be removed on closing the app.

推荐答案

下面是解决方案:

private void writeLog(String s)
{
    String FILENAME = "log.txt";
    FileOutputStream fos = null;
    try
    {
        fos = openFileOutput(FILENAME, Context.MODE_APPEND);
        fos.write(s.getBytes());
    }
    catch(FileNotFoundException e){}
    catch(IOException e){}
    finally
    {
        try
        {
            fos.close();
        }
        catch(IOException e){}
    }
}

private void readLog(EditText logs)
{
    String FILENAME = "log.txt";
    FileInputStream in = null;
    try
    {
        in = openFileInput(FILENAME);
    }
    catch(IOException e1){}
    try
    {
        byte[] buffer = new byte[4096]; // Read 4K characters at a time.
        int len;
        logs.setText("");
        while((len = in.read(buffer)) != -1)
        {
            String s = new String(buffer, 0, len);
            logs.append(s);
        }
    }
    catch(IOException e){}
    finally
    {
        try
        {
            if(in != null) in.close();
        }
        catch(IOException e){}
    }
}

这篇关于如何将一个静态的日志文件到一个Android应用程序将不会关闭应用程序被删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆