File: This is a File base class that abstracts a File object. It has four different constructor methods:
File (File dir, String name)
File (String path)
File (String dirPath, String name)
File (URI uri)
The essence of these constructor methods is to construct a File instance and provide a complete File path.
A File instance is constructed, which does not mean that a new File or folder is created. This File instance is only a representative of the File entity in the system.
Important Method: public boolean exists () to determine whether a file or folder exists
Public boolean isFile () determines whether it is a file
Public boolean isDirectory determines whether it is a folder
Public boolean createNewFile () creates a new, empty file based on the specified path and file name.
Public boolean mkdir () create a folder Based on the specified path (level 1 Operation)
Public boolean mkdirs () create a folder Based on the specified path (multi-level operation)
Public boolean delete () delete a file or folder
Public boolean renameTo (File dest) rename a File or folder
Public long length () gets the object length, in bytes (Folder unavailable)
Public String getName () Get the name of the file or folder
Public String getAbsolutePath () to obtain the absolute path of the file or folder
Pubic String getPath () to obtain the path of the file or folder
Public File [] listFiles () returns all File objects in the folder.
Public String [] list () returns the names of all files or folders in the folder.
File storage mechanism in Android applications:
Activity provides two methods to obtain the byte-based input and output streams:
Public FileOutputStream openFileOutput (String name, int mode) to obtain the output stream
Public FileInputStream openFileInput (String name) to obtain the input stream
Both methods are used to create or read files in data/<applicatin-package>/files/. That is to say, this method is similar to SharedPreference and is dedicated to the file storage mechanism provided by an application. (By default, different applications cannot be shared)
Mode has the following four modes:
Use 0 or MODE_PRIVATE (the default operation): Use 0 to indicate the default value. Only the application that creates the file can access the file. Each file write overwrites the file.
MODE_APPEND: append mode for each file write, similar to the append () method in StringBuffer.
MODE_WORLD_READABLE: only the read permission.
MODE_WORLD_WRITEABLE: only write permission.
To obtain both read and write permissions, create MODE_WORLD_READABLE + MODE_WORLD_WRITEABLE in mode as follows:
Common File Name filter usage
1. Implement the FilenameFilter interface;
2. Implement boolean accept (File dir, String name); // dir indicates the current directory of the File, and name indicates the File name;
Class MyFilter implements FilenameFilter {
Private String type; // type is the filter condition. For example, if type = ". jpg", only jpg files with the suffix can be returned.
Public MyFilter (String type ){
This. type = type;
}
Public boolean accept (File dir, String name) {// The File that returns true is qualified.
}
}
For android files, you must have the following permissions:
Determine whether the SD card is inserted
Environment. getExternalStorageState (). equals (
Android. OS. Environment. MEDIA_MOUNTED );
Obtain the SD card root directory
File skRoot = Environment. getExternalStorageDirectory ();
Obtain the private root directory
File fileRoot = Context. getFilesDir () + "";
Determine or obtain the folder and file path
String path = File. getPath (); // relative
String path = File. getAbsoultePath (); // absolute
Obtain 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 (); // create a File
Identify as a file or folder
File. isDirectory ()
Lists all files and folder names in a folder.
File [] files = File. listFiles ();
Modify folders and file names
File. renameTo (dest );
Delete folders or files
File. delete ();
File read/write mode
Context. MODE_PRIVATE: the new content overwrites the original content.
Context. MODE_APPEND: after the new content is appended to the original content
Context. MODE_WORLD_READABLE: allows other applications to read data.
Context. MODE_WORLD_WRITEABLE: allows other applications to write data and overwrites the original data.