File This is the base class of files, which represent a file entity abstractly, and it has four different construction methods:
File (file dir, String name)
File (String Path)
File (String Dirpath, string name)
File (URI Uri)
The essence of these constructs is the same, that is, to construct a file instance, you must provide a complete path to the files.
Constructs a file instance, does not say that creates a new files or the folder, the file instance, is only one representative of the actual system inside the document entity.
Important: Public Boolean exists () determines whether a file or folder exists
public boolean isfile () determines whether the file
Public Boolean isdirectory determines whether the folder
public Boolean createnewfile () creates a new, empty file based on the path and file name you have established
public boolean mkdir () creates a folder based on the path established (first-level operation)
public Boolean mkdirs () creates folders based on established paths (multilevel operations)
public boolean Delete () deletes a file or folder
public boolean Renameto (file dest) renaming files or folders
public long length () Gets the length of the file in bytes (folder is not available)
Public String GetName () Gets the name of the file or folder
Public String GetAbsolutePath () Gets the absolute path to the file or folder
Pubic String GetPath () to get the path to a file or folder
Public file[] Listfiles () returns all file objects within a folder
Public string[] List () returns the name of all files or folders within a folder
File storage mechanisms within Android apps:
Activity provides two ways to obtain byte-based input and output streams, respectively:
Public FileOutputStream openfileoutput (String name, int mode) to get the output stream
Public FileInputStream openfileinput (String name) gets the input stream
Both of these methods create or read files in data/data/<applicatin-package>/files/, which means that this is similar to sharedpreference, specifically for a file storage mechanism that is provided within an application. (not shared between different apps by default)
Mode has the following four modes:
Use 0 or mode_private (the default operation): uses 0 to indicate the default value, only the application that can create the file accesses the file, each time the file is written as an overlay.
Mode_append: Each time a file is written as an append, similar to the APPEND () method in StringBuffer.
Mode_world_readable: only Read permission.
Mode_world_writeable: Only write permission.
If you want to have read and write permission at the same time, you can create it in mode by: Mode_world_readable+ mode_world_writeable
File name Filter General usage
1. Implement FilenameFilter interface;
2. Implement the Boolean accept (File dir,string name); Dir indicates the current directory of the file, and name indicates the filename;
Class Myfilter implements filenamefilter{
Private String type; Type is a condition that needs to be filtered, such as if type= ". jpg", only files with the suffix JPG can be returned
Public myfilter (String type) {
This.type = type;
}
Public boolean accept (file Dir,string name) {//returns true for files qualified
}
}
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
Java and Android file operations