An explanation of the Android file files operation

Source: Internet
Author: User

An explanation of the Android file files operation

The Android file operation should have permission:

Determine if the SD card is plugged in

Environment.getexternalstoragestate (). Equals (

Android.os.Environment.MEDIA_MOUNTED);

Get the SD card root directory

File skroot = Environment.getexternalstoragedirectory ();

Get the private root directory

File fileroot = Context.getfilesdir () + "";

Determining or obtaining folder and file paths

String path = File.getpath ();//Relative

String path = File.getabsoultepath ();//Absolute

Get the parent directory of a file or folder

String Parentpath = File.getparent ()

Get the name of the file or folder:

String Name = File.getname ();

Create a file or folder

File.createnewfile ();//Build file

Judging is a file or folder

File.isdirectory ()

List all file and folder names under a folder

file[] files = file.listfiles ();

Modify folder and file name

File.renameto (dest);

Delete a folder or file

File.delete ();

File read/write operation mode

Context.mode_private: New content overwrites original content

Context.mode_append: After new content is appended to the original content

Context.mode_world_readable: Allow other applications to read

Context.mode_world_writeable: Allows other applications to write, overwriting the original data.

/*** Saving files

* @param fileName

* @param filecontent

* @throws exception*/

public void Save (string fileName, String filecontent) throws Exception {

The parent class of the activity's parent is context,context, which is the same as the context in the other frameworks, providing us with some core manipulation tools.

FileOutputStream FileOutputStream = This.context.openFileOutput (

FileName, context.mode_private);

Fileoutputstream.write (Filecontent.getbytes ());

}

/**

* Read files

*

* @param fileName

* @return

* @throws Exception

*/

public string Read (string fileName) throws Exception {

FileInputStream FileInputStream = This.context.openFileInput (fileName);

Bytearrayoutputstream ByteArray = new Bytearrayoutputstream ();

byte[] buffer = new byte[1024];

int len = 0;

while (len = fileinputstream.read (buffer)) > 0) {

Bytearray.write (buffer, 0, Len);

};

return bytearray.tostring ();

}

}

An explanation of the Android file files operation

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.