I'm Trying to Write Logs to Custom Log.txt File on Android File using this code of Mine but then this method creates file but contains nothing. Basically I want to read previous contents of the file and then append my data with the existing content.
The Code is as follows :
public static void write(String str)
{
InputStream fileInputStream = null;
FileOutputStream fileOutpurStream = null;
try
{
fileInputStream = new FileInputStream(file);
fileOutpurStream = new FileOutputStream(file);
if(file.exists())
{
int ch = 0;
int current = 0;
StringBuffer buffer = new StringBuffer();
while((ch = fileInputStream.read()) != -1)
{
buffer.append((char) ch);
current++;
}
byte data[]=new byte[(int)file.length()];
fileInputStream.read(data);
fileOutpurStream.write(data);
fileOutpurStream.write(str.getBytes(),0,str.getBytes().length);
fileOutpurStream.flush();
}
else
{
file.createNewFile();
fileOutpurStream.write(str.getBytes(),0,str.getBytes().length);
fileOutpurStream.flush();
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
fileInputStream.close();
fileOutpurStream.flush();
fileOutpurStream.close();
fileOutpurStream = null;
fileInputStream = null;
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I solved this problem with following piece of code in command line way:
Where "time" option adds metadata field details for date, invocation time, priority/tag, and PID of the process issuing the message.
Then in your code just do something similar to this (using android.util.Log):
microlog4android works for me but the documentation is pretty poor. All they need to add is a this is a quick start tutorial.
Here is a quick tutorial I found.
Add the following static variable in your main Activity:
Add the following to your
onCreate()
method:Create a file named
microlog.properties
and store it inassets
directoryEdit the
microlog.properties
file as follows:Add logging statements like this:
For each class you create a logger object as specified in 1)
6.You may be add the following permission:
Here is the source for tutorial
I have created a simple, lightweight class (about 260 LoC) that extends the standard android.util.Log implementation with file based logging:
Every log message is logged via android.util.Log and also written to a text file on the device.
You can find it on github:
https://github.com/volkerv/FileLog
You should take a look at microlog4android. They have a solution ready to log to a file.
http://code.google.com/p/microlog4android/
Hope this can help...
In general, you must have a file handle before opening the stream. You have a fileOutputStream handle before createNewFile() in the else block. The stream does not create the file if it doesn't exist.
Not really android specific, but that's a lot IO for this purpose. What if you do many "write" operations one after another? You will be reading the entire contents and writing the entire contents, taking time, and more importantly, battery life.
I suggest using java.io.RandomAccessFile, seek()'ing to the end, then writeChars() to append. It will be much cleaner code and likely much faster.