Jeff Bootsholz
Jeff Bootsholz

Reputation: 3068

unable to write and append the text file android

I am trying to write a text file for logging in my app. When it comes to execution, there are READ-ONLY EXCEPTION and hence cannot write the text file.

only file 1" can be executed

Now using 5.0.1

The below is my code :

public static void writefile(String text  )
{
    File externalStorageDir =  new File (Environment.getExternalStorageDirectory().getAbsolutePath() +  File.separator  + "Download" );
    String fileName=  date() + ".txt" ;
    File dir = new File(externalStorageDir  ,  File.separator + "eyedebug"  );
    boolean statement = dir.exists() && dir.isDirectory();
    if(!statement) {
        // do something here
        dir.mkdirs(); 
        System.out.println("file 1");
    } 


    File myFile = new File(dir.getAbsolutePath() ,  File.separator + fileName  );
    if(!myFile.exists()){
        try {
            myFile.createNewFile();
            System.out.println("file 2");
        }
        catch (IOException e) 
        {
            e.printStackTrace();
        }
    } 

    try
    {
        FileWriter fileWritter = new FileWriter(myFile.getName(),true);
        BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
        bufferWritter.append(text);
        bufferWritter.newLine();
        System.out.println("file 3");
        bufferWritter.close();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }



}

Upvotes: 0

Views: 1304

Answers (4)

Ravi Vaghela
Ravi Vaghela

Reputation: 3420

after long work finally i found your solution, just implement below code it will help you..

 public static void writefile(String text  )
  {
    File externalStorageDir =  new File (Environment.getExternalStorageDirectory().getAbsolutePath()  + "/Download/eyedebug/" );
    String fileName=  System.currentTimeMillis() + ".txt" ;

    boolean statement = externalStorageDir.exists() && externalStorageDir.isDirectory();
    if(!statement) {
        // do something here
        externalStorageDir.mkdirs();
        System.out.println("file 1");
    }


    File myFile = new File(externalStorageDir.getAbsolutePath() ,   fileName  );
    if(!myFile.exists()){
        try {
            myFile.createNewFile();
            System.out.println("file 2");
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    try
    {
        FileWriter fileWritter = new FileWriter(myFile,true);
        BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
        bufferWritter.append(text);
        bufferWritter.newLine();
        System.out.println("file 3");
        bufferWritter.close();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }



}

Upvotes: 4

vijaypalod
vijaypalod

Reputation: 408

There are two ways to print application log into a file.

If you want to get all loged events then you can use following method that used command line to save logs into file.

 public static void printLog(Context context){
    String filename = context.getExternalFilesDir(null).getPath() + File.separator + "my_app.log";
    String command = "logcat -f "+ filename + " -v time -d *:V";

    Log.d("FB Error Log", "command: " + command);

    try{
        Runtime.getRuntime().exec(command);
    }
    catch(IOException e){
        e.printStackTrace();
    }
}

else you can use following method to save indivisual logs into file.

 public static void appendLog(String text) {
    File logFile = new File("sdcard/app_log.txt");
    try {
        if (!logFile.exists()) {
            logFile.createNewFile();
        }

        //BufferedWriter for performance, true to set append to file flag
        Calendar cal = Calendar.getInstance();
        Date date = cal.getTime();
        String format = "[dd/MM/yy HH:mm:ss]";
        SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.getDefault());
        String currentTime = sdf.format(date);
        BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true));
        buf.append(currentTime+" - "+text);
        buf.newLine();
        buf.close();
    }
    catch (Exception e) {
        Log.e("StaticUtils", e.getMessage(), e);
    }
}

Dont forget to add permission

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Upvotes: 0

Naresh Narsing
Naresh Narsing

Reputation: 785

Add following lines in your manifest file

<manifest ...>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    ...
</manifest>

Upvotes: 0

KDeogharkar
KDeogharkar

Reputation: 10959

add write permission WRITE_EXTERNAL_STORAGE in your manifest file.

Upvotes: 0

Related Questions