Android openFileInput openFileOutput
Zookeeper
OpenFileInput: Open the input stream corresponding to the name file in the data folder of the application.
OpenFileOutput: Open the output stream corresponding to the name file in the data folder of the application.
The following is an example of read/write operations.
Public class MainActivity extends Activity {
Private EditText et1;
Private EditText et2;
Private Button bt1;
Private Button bt2;
Final String FILE_NAME = "crazyit. bin ";
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
Et1 = (EditText) findViewById (R. id. editText1 );
Et2 = (EditText) findViewById (R. id. editText2 );
Bt1 = (Button) findViewById (R. id. button1 );
Bt2 = (Button) findViewById (R. id. button2 );
Bt1.setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View arg0 ){
Write (et1.getText (). toString ());
Et1.setText ("");
}
});
Bt2.setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
Et2.setText (read ());
}
});
}
Private String read (){
Try {
FileInputStream FCM = openFileInput (FILE_NAME );
Byte [] buff = new byte [1024];
Int hasread = 0;
StringBuilder sb = new StringBuilder ();
While (hasread = FS. read (buff)> 0 ){
Sb. append (new String (buff, 0, hasread ));
}
FCM. close ();
Return sb. toString ();
} Catch (FileNotFoundException e ){
E. printStackTrace ();
} Catch (IOException e ){
E. printStackTrace ();
}
Return null;
}
Private void write (String content ){
Try {
FileOutputStream fos = openFileOutput (FILE_NAME, MODE_APPEND );
PrintStream ps = new PrintStream (fos );
Ps. println (content );
Ps. close ();
} Catch (FileNotFoundException e ){
E. printStackTrace ();
}
}
}