How to write data into a file in android


/**
 * @param filePath path of file
 * @param fileData data which you want to insert into the above file
 * @return <b>true</b> if data was written to the file<br/>
 * <b>false</b> if data was not written to the file<br/><br/>
 *
 * <b>Note:</b> This requires permission <b>"android.permission.WRITE_EXTERNAL_STORAGE"</b>
 * This will override the data which exists in the file
 */
 public static boolean writeDataToFile(String filePath, String fileData)
 {
 boolean success = false;

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

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

 if(srcFile.exists() && srcFile.isFile())
 {
 try
 {
 FileOutputStream stream = new FileOutputStream(filePath);
 OutputStreamWriter out = new OutputStreamWriter(stream);
 out.write(fileData);
 out.close();
 stream.flush();
 stream.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: