Using the Activity class's Openfileinput () and Openfileoutput methods to manipulate files on the device, the file created by default exists in the "/data/data/<pakage name>/files" directory, as in the package named " Com.company.business "To create a" data.txt "file in the program, the storage path will be"/data/data/com.company.business/file/date.txt ". In the default state, Files cannot be shared between different programs, both of which support only reading files in the application directory, and if reading files that are not in their own directory will throw FileNotFoundException exceptions.
The key implementation code is as follows, and it is recommended that you call these two methods into two menu options to see the program effect.
(1) Read the file code
public void ReadFile (String sfilename) { try { FileInputStream fis = openfileinput (sfilename); Convert byte streams to character stream inputstreamreader inreader = new InputStreamReader (FIS); Convert to BufferedReader bufferedreaader BufferedReader = new BufferedReader (inreader) with cache; String s; while ((s = bufferedreader.readline ())!=null) { toast.maketext (This,s,toast.length_long). Show (); } Fis.close ();//Close input stream }catch (Exception ex) ex.printstacktrace ();}}
(2) write the file code
public void WriteFile (string sfilename) { string s = "Hello"; s = s+ "/n" + "nice to meet you"; try{ //defines a file byte output stream with the name sFileName fileoutputstream fos = openfileoutput (sfilename,0); The file bytes are input into the file character output stream outputstreamwriter outwriter = new OutputStreamWriter (FOS); Then convert the file character output stream to the cache character output flow bufferedwriter BufferedWriter = new BufferedWriter (outwriter); Use the Write method to write information to the file Bufferedwriter.writer (s); Bufferedwriter.flush (); Fos.close ();} catch (Exception ex) { ex.printstacktrace ();}}
Android ROM Files Store data