How to append data to a file in android


/**
 * @param filePath path of file
 * @param fileDataToAppend data which you want to append into the above file
 * @return <b>true</b> if data was appended to the file<br/>
 * <b>false</b> if data was not appended to the file<br/><br/>
 * <b>Note:</b> This requires permission <b>"android.permission.WRITE_EXTERNAL_STORAGE"</b>
 */
 public static boolean appendFile(String filePath, String fileDataToAppend)
 {
 boolean success = false;

 if(filePath!=null && fileDataToAppend!=null)
 {
 filePath = filePath.trim();
 fileDataToAppend = fileDataToAppend.trim();

 if(!filePath.equals("") && !fileDataToAppend.equals(""))
 {
 File srcFile = new File(filePath);

 if(srcFile.exists() && srcFile.isFile())
 {
 try
 {
 FileWriter out = new FileWriter(filePath,true);
 out.write(fileDataToAppend);
 out.close();
 success = true;
 }
 catch (Exception e)
 {
 e.printStackTrace();
 }
 }
 }
 }
 return success;
 }

Advertisements

Tags: ,

About krishjlk

http://www.facebook.com/krishna.kanth.121

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: