Getting Started with Android file system operations (ii) file operations related directives (RPM) (i) Get the total root
- File[] Filelist=file.listroots ();
- Returns filelist.length to 1
- Filelist.getabsolutepath () is "/"
- This is the total root of the system.
File[] Filelist=file.listroots (); return filelist.length to 1//filelist.getabsolutepath () to "/"///This is the total root of the system
(ii) Open the total root directory
- File file=new file ("/");
- File[] Filelist=file.listfiles ();
- Get the directory In addition to "/sdcard" and "/system" there are "/data", "/cache", "/dev" and so on
- The root directory of Android is not divided into C-disk, D-disk, e-disk, etc. as Symbian system.
- Android is Linux-based, only directories, no matter the drive letter
File File=new file ("/"); File[] Filelist=file.listfiles (); In addition to the "/sdcard" and "/system", the root directory of//android such as "/data", "/cache", "/dev" is not divided into C-drive, D-disk, e-disk, etc., as Symbian systems Android is Linux-based, only directories, no matter the drive letter
(iii) Obtaining the system storage root directory
- File file=environment.getrootdirectory (); //file file=new File ("/system");
- File[] Filelist=file.listfiles ();
- The system here refers only to "/system."
- The range of phones that do not include external storage is much larger than the so-called system storage
File File=environment.getrootdirectory ();//file file=new file ("/system"); File[] Filelist=file.listfiles (); The system referred to here only refers to "/system"//not including external storage of the mobile phone storage range far greater than the so-called system storage
(iv) Obtaining the SD card storage root directory
- File file=environment.getexternalstoragedirectory (); //file file=new File ("/sdcard");
- File[] Filelist=file.listfiles ();
- To obtain an SD card, first verify that the SD card is loaded
- Boolean is=environment.getexternalstoragestate (). Equals (environment.media_mounted);
- If true, it is loaded
- If False, it is not loaded
File File=environment.getexternalstoragedirectory ();//file file=new file ("/sdcard"); File[] Filelist=file.listfiles (); To obtain an SD card, first make sure that the SD card is loaded with a Boolean is=environment.getexternalstoragestate (). Equals (environment.media_mounted); If true, the mount//If False is not mounted
(v) Get the data root directory
- File file=environment.getdatadirectory (); //file file=new File ("/data");
- File[] Filelist=file.listfiles ();
- Because the Data folder is a very important folder in Android, general permissions are not available to the file, that is, Filelist.length returns 0
File File=environment.getdatadirectory ();//file file=new file ("/data"); File[] Filelist=file.listfiles (); Because the Data folder is a very important folder in Android, general permissions are not available to the file, that is, Filelist.length returns 0
(vi) Get the private file path
- Context context= this; First, get context in the activity.
- File File=context.getfilesdir ();
- String Path=file.getabsolutepath ();
- The path returned here is/data/data/packet/files, where the package is the package where we built the main activity
- We can see that this path is also in the Data folder
- The program itself can operate on its own private files
- Many of the private data in the program is written to the private file path, which is one of the reasons why Android protects data
Context context=this;//First, get context File file=context.getfilesdir () in activity; String Path=file.getabsolutepath (); The path returned here is/data/data/packet/files, where the package is the package that we built the main activity in//we can see that this is also in the Data folder//The program itself is able to operate on its own private files// Many of the private data in the program is written to the private file path, which is one of the reasons why Android protects data
(vii) Get the file (clip) absolute path, relative strength, file (folder) name, parent directory
- File file= ...
- String Relativepath=file.getpath (); //relative path
- String Absolutepath=file.getabsolutepath (); //absolute path
- String Filename=file.getname (); //file (folder) name
- String parentpath=file.getparent (); //Parent directory
File file= ... String Relativepath=file.getpath ();//relative path string Absolutepath=file.getabsolutepath ();//absolute path string filename= File.getname ();//file (folder) name String parentpath=file.getparent ();//Parent Directory
(eight) List all files and folders under the folder
- File file= ...
- File[] Filelist=file.listfiles ();
File file= ... File[] Filelist=file.listfiles ();
(ix) Determine whether it is a file or a folder
- File file= ...
- Boolean is=file.isdirectory (); //true-Yes, false-no
File file= ... boolean is=file.isdirectory ();//true-Yes, false-no
(10) Determine if the file (clip) exists
- File file= ...
- Boolean is=file.exists (); //true-Yes, false-no
File file= ... boolean is=file.exists ();//true-Yes, false-no
(11) New file (clip)
- File file= ...
- Oolean is=file.isdirectory (); //Decide whether to be a folder
- /* Method 1*/
- if (IS) {
- String Path=file.getabsolutepath ();
- String name="ABC"; You want to create a new folder name or file name
- String Pathx=path+name;
- File filex=new file (PATHX);
- boolean is=filex.exists (); //Determine if the file (clip) exists
- if (!is) {
- Filex.mkdir (); //Create a folder
- //filex.createnewfile ();//Create File
- }
- /* Method 2*/
- if (IS) {
- String Path=file.getabsolutepath ();
- String name="test.txt"; You want to create a new folder name or file name
- File filex=new file (path,name); The difference between Method 1 and Method 2 is that this
- boolean is=filex.exists (); //Determine if the file (clip) exists
- if (!is) {
- Filex.mkdir (); //Create a folder
- //filex.createnewfile ();//Create File
- }
File file= ... oolean is=file.isdirectory ();//Determines whether the folder/* method 1*/if (IS) {String path=file.getabsolutepath (); String Name= "ABC";//You want to create a new folder name or file name String pathx=path+name; File Filex=new file (PATHX); Boolean is=filex.exists ();//Determine if the file (clip) exists if (!is) {filex.mkdir ();//Create Folder//filex.createnewfile ();//Create File}/* Method 2*/if (is ) {String path=file.getabsolutepath (); String name= "test.txt";//You want to create a new folder name or file name file Filex=new file (path,name);//The difference between Method 1 and Method 2 is this boolean is=filex.exists ();/ Determine if the file (clip) exists if (!is) {filex.mkdir ();//Create Folder//filex.createnewfile ();//Create File}
(12) Renaming files (clips)
- File file= ...
- String parentpath=file.getparent ();
- String newname="name"; Renamed file or folder name
- File filex=new file (parentpath,newname); File Filex=new file (parentpaht+newname)
- File.renameto (Filex);
File file= ... String parentpath=file.getparent (); String newname= "name";//the file or folder name renamed Filex=new files (parentpath,newname);//file filex=new file (parentpaht+ NewName) File.renameto (Filex);
(13) Delete file (clip)
- File file= ...
- File.delete (); //Delete now
- File.deleteonexit ()///program exit after deletion, only normal exit will be deleted
Android Getting Started file system operation