Android File Operations

Source: Internet
Author: User

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

 

Related Article

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.