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