Android File Operation detailed

Source: Internet
Author: User
Tags parent directory save file

Android file operations should have permission:

Determine if the SD card is plugged in

Environment.getexternalstoragestate (). Equals (

Android.os.Environment.MEDIA_MOUNTED);

Get SD Card root directory

File skroot = Environment.getexternalstoragedirectory ();

Get the private root directory

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

Identify or get 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 ()

To obtain the name of a file or folder:

String Name = File.getname ();

Create a file or folder

File.createnewfile ()//Create a file

Judgment is a file or folder

File.isdirectory ()

List all file and folder names under the folder

file[] files = file.listfiles ();

modifying folders and file names

File.renameto (dest);

Delete a folder or file

File.delete ();

File read and write operation mode

Context.mode_private: New content covering the original content

Context.mode_append: After the 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.

/*** Save File

* @param fileName

* @param filecontent

* @throws exception*/

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

The parent of the activity's parent class is that Context,context is the same as the context in other frameworks for us to provide some core operational 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 ();

}

}

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.