Mega Code Archive

 
Categories / Android / File
 

Read the created file and display to the screen

package app.test; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class Test extends Activity {     private static final String FILENAME = "data.txt";          @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         TextView tv = new TextView(this);         setContentView(tv);                  try {             FileOutputStream mOutput = openFileOutput(FILENAME, Activity.MODE_PRIVATE);             String data = "THIS DATA WRITTEN TO A FILE";             mOutput.write(data.getBytes());             mOutput.close();         } catch (FileNotFoundException e) {             e.printStackTrace();         } catch (IOException e) {             e.printStackTrace();         }                  try {             FileInputStream mInput = openFileInput(FILENAME);             byte[] data = new byte[128];             mInput.read(data);             mInput.close();                          String display = new String(data);             tv.setText(display.trim());         } catch (FileNotFoundException e) {             e.printStackTrace();         } catch (IOException e) {             e.printStackTrace();         }                  deleteFile(FILENAME);         } }