Android ROM Files Store data

Source: Internet
Author: User

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

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.