tags:

views:

2520

answers:

6

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();
  }
 }
+1  A: 

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.

Nate
Okay Nate I'll try that today and inform you the out come.
y ramesh rao
+1  A: 

Warning: I may be totally misunderstanding you, but if all you want is a log file, why sweat?

Put this in a bat file (change the path to your tools directory, and yourappname is of course your app's name):

cd "C:\devAndroid\Software\android-sdk-windows-1.6_r1\android-sdk-windows-1.6_r1\tools"
adb logcat -v time   ActivityManager:W  yourappname:D  *:W >"C:\devAndroid\log\yourappname.log"

Then in your code just do something similar to this:

Log.d("yourappname", "Your message");

To create the log, connect the USB cable and run your bat file.

Regards

BeMeCollective
Don't forget importing android.util.Log.
Spidey
+3  A: 

You should take a look at microlog4android. They have a solution ready to log to a file.

http://code.google.com/p/microlog4android/

darius
A: 

Hi.. I m successfully write the logs in the text file and also read it...... but i want to send that text file through email, so that i want the path of the file...... so how we can get the path.....please reply

Kaushik
A: 

Hi in reply to the solution given by "BeMeCollective" , when I implemented I am getting error as "The Sys. can't find the path specified" C:\android-sdk_r3-windows\android-sdk-windows\tools>adb logcat -v time Activit yManager:W D:\mApp2\mApp2Android_nomaps\src\com\cognizant\activity :D *:W >"C:\ devAndroid\log\mApp2Android_nomaps.log" The system cannot find the path specified. Any solu. for this?

Thanks in Advance.

Android_programmer_camera
Invalid filter expression 'D:\mApp2\mApp2Android_nomaps\src\com\cognizant\activity'Usage: logcat [options] [filterspecs]options include: -s Set default filter to silent. Like specifying filterspec '*:s' -f <filename> Log to file. Default to stdout -r [<kbytes>] Rotate log every kbytes. (16 if unspecified). Requires -f -n <count> Sets max number of rotated logs to <count>, default 4 -v <format> Sets the log print format, where <format> is one of: brief process tag thread raw time threadtime long
Android_programmer_camera