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