When operating an android file, you must grant the following permissions:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
The following code writes text to a file:
Private Final Static string Path = "/sdcard/Lin"; private final static string filename = "/test.txt"; @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); writefile ();} private void writefile () {// determine whether the device has sdcardif (environment. media_mounted.equals (environment. getexternalstoragestate () {try {log. D ("file operation", "sta Rt! "); File Path = new file (PATH); file = new file (path + filename); // if this path does not exist, create this path if (! Path. exists () {path. mkdir () ;}// if this file does not exist, create this file if (! File. exists () {file. createnewfile () ;}// set "text message! "Information is written into the corresponding file outputstreamwriter OSW = new outputstreamwriter (New fileoutputstream (File); OSW. Write (" text message! "); OSW. Close (); log. D (" file operation "," success! ") ;}Catch (exception e) {log. D (" file operation "," file create error ");}}}