Android file access

Source: Internet
Author: User

There are several APIs available in Android for file operations in addition to Java's Basic library rollup of Io, file, etc.

1, the context class provides the Openfileoutput () and Openfileinput () two methods to obtain the IO stream, and then can be related to read and write operations.
The parameters of the operation mode of a file in a write operation are several constants provided by the context: mode_private, overwriting file, default mode;
Mode_append, append content, does not exist on the new.
Read file
PublicString Load (Context context,string fileName) {FileInputStream is=NULL; BufferedReader Reader=NULL; StringBuilder result=NewStringBuilder (); Try{ is=Context.openfileinput (fileName); Reader=NewBufferedReader (NewInputStreamReader (IS)); String L=""; while((L=reader.readline ())! =NULL) {result.append (L); } } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } finally { if(reader!=NULL) { Try{reader.close (); } Catch(IOException e) {e.printstacktrace (); } } } returnresult.tostring (); }

Write file
Public BooleanWrite (Context context,string filename,string content) {FileOutputStream OS=NULL; BufferedWriter writer=NULL; Try{OS=Context.openfileoutput (Filename,context. Mode_append); Writer=NewBufferedWriter (Newoutputstreamwriter (OS)); Writer.write (content); return true; } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } finally { if(writer!=NULL) { Try{writer.close (); } Catch(IOException e) {e.printstacktrace (); } } } return false; }

2, the following use of the four environment method, get the current running environment of four different directories, return a file object, we can according to this file

Object for new, write, read, and other operations. For different manufacturers, the Access API for SD card phone memory may be modified, these need actual situation, practical analysis.

        LOG.I ("info", "getdatadirectory ()" +environment.getdatadirectory (). GetAbsolutePath ());        LOG.I ("info", "getrootdirectory ()" +environment.getrootdirectory (). GetAbsolutePath ());        LOG.I ("info", "getexternalstoragedirectory ()" +environment.getexternalstoragedirectory (). GetAbsolutePath ());        LOG.I ("info", "getexternalstoragepublicdirectory ()" +environment.getexternalstoragepublicdirectory (" BasePath "). GetAbsolutePath ());

Android file access

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.