Skip to content Skip to sidebar Skip to footer

How To Include A Static Log File Into An Android App That Won't Be Removed On Closing The App?

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 storin

Solution 1:

Instead of including the log file with the app you should create it on first launch.

There's more information How to create a file in Android? on creating a file.

Solution 2:

Well i don't see the problem, your user will open the app and it will be runtime and you can write what ever you want to what ever file. So this tip doesn't apply to you.

Solution 3:

Here is the solution:

privatevoidwriteLog(String s)
{
    StringFILENAME = "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){}
    }
}
privatevoidreadLog(EditText logs)
{
    StringFILENAME="log.txt";
    FileInputStreamin=null;
    try
    {
        in = openFileInput(FILENAME);
    }
    catch(IOException e1){}
    try
    {
        byte[] buffer = newbyte[4096]; // Read 4K characters at a time.int len;
        logs.setText("");
        while((len = in.read(buffer)) != -1)
        {
            Strings=newString(buffer, 0, len);
            logs.append(s);
        }
    }
    catch(IOException e){}
    finally
    {
        try
        {
            if(in != null) in.close();
        }
        catch(IOException e){}
    }
}

Post a Comment for "How To Include A Static Log File Into An Android App That Won't Be Removed On Closing The App?"