Write new file or append text Java Android example

BufferedWriter, FileWriter, write, close MODE_APPEND Java and Android example

Write to file Android example

try {
		String MYFILE = "my_file";
		String strText = "My text";
 
                // MODE_APPEND, MODE_WORLD_READABLE, MODE_WORLD_WRITEABLE
		// create new file or rewrite existing
                FileOutputStream fos = openFileOutput(MYFILE, getApplicationContext().MODE_PRIVATE);
                // append to file
                FileOutputStream fos = openFileOutput(MYFILE, getApplicationContext().MODE_APPEND);
 
		fos.write(strText.getBytes());
		fos.close();					
	} catch (IOException e) {
		e.toString();
    }

Write to file Java example

try {
// new file    
BufferedWriter out = new BufferedWriter(
    		new FileWriter("outfilename"));
    // append text 
   BufferedWriter out = new BufferedWriter(new FileWriter("outfilename", true));
    out.write("some text");
    out.close();
 } catch (IOException e) {
   e.toString();
}
This entry was posted in Android Examples Code and tagged , , , . Bookmark the permalink.


Comments are closed.



23,372 views