Android Append Text File

Android append text file

I figured it out....I had to change the line

FileOutputStream fOut = openFileOutput("savedData.txt", MODE_WORLD_READABLE);

to

FileOutputStream fOut = openFileOutput("savedData.txt",  MODE_APPEND);

After that I was able to append the text file without overwriting the data that was already inside the text file. Thanks for your assistance guys. I guess going to the 4th page on google IS useful sometimes.

How to append data in a file in android

FileOutputStream fileinput = new FileOutputStream(file, true);

It was a simple mistake, because of not setting the append flag to true. Just set it to true.

The default behavior is to overwrite the file content :)

Android save to file.txt appending

Change this,

OutputStreamWriter out = new OutputStreamWriter(openFileOutput("save.txt", MODE_PRIVATE));

to,

OutputStreamWriter out = new OutputStreamWriter(openFileOutput("save.txt", Context.MODE_APPEND));

This will append your new contents to the already existing file.

I Hope it helps!

Appending data to a text file in Android

The FileOutputStream constructor allows to specify whether it should append to an already existing file or not:

new FileOutputStream(file, true);

will create a stream that appends to the given file.

unable to write and append the text file android

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();
}

}


Related Topics



Leave a reply



Submit