Android creates and writes files in the specified directory

Source: Internet
Author: User

Goal:

Create files in the file directory and write text messages. Used to share data with the app.

After searching for keywords, we found the following methods are recommended:

which

Name
Used to specify the file name, cannot contain the path delimiter "/", and if the file does not exist, Android will automatically create it. Like Info.txt.

The created file is saved in the /data/data/<package name>/files/ directory.


Mode value:
Mode_append Private (only the program that created this file can be used, other applications cannot access it), adding data based on the original content
Mode_private Private, overwriting the original content every time the file is opened
Mode_world_readable can be read by other applications (this is no longer used, it will be an error)
Mode_world_writeable can be written by other applications

However, the/data/data/ directory is not visible through the file management system that comes with the phone and the computer file management system.

Need to be opened in Android Studio with Device Monitor, the specific path

Tools-android-android Device Monitor-file Explorer

Here are the solutions I use:

                LOG.E (TAG, copycontent.tostring ());                try {                    file fs = new File (environment.getexternalstoragedirectory () + "/msc/" + fileName);                    FileOutputStream outputstream =new FileOutputStream (FS);                    Outputstream.write (Copycontent.getbytes ());                    Outputstream.flush ();                    Outputstream.close ();                    Toast.maketext (Getbasecontext (), "File created successfully", Toast.length_long). Show ();                    LOG.E (TAG, "successful");                } catch (FileNotFoundException e) {                    e.printstacktrace ();                } catch (IOException e) {                    e.printstacktrace (); c13/>}

That is, without the openfileoutput method, using the FileOutputStream method, the pass parameter of the method is a file object that can be set to a path.

Create the file file in the specified directory, and then use the FileOutputStream class to create a outputstream for it, then you can write the data normally.

Android creates and writes files in the specified directory

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.