Android openFileInput openFileOutput

Source: Internet
Author: User

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


}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.