(i) Access to the total root
[Java]View PlainCopy
- File[] Filelist=file.listroots ();
- Returns filelist.length to 1
- Filelist.getabsolutepath () is "/"
- This is the total root of the system.
(ii) Open the total root directory
[Java]View PlainCopy
- 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
(iii) Obtaining the system storage root directory
[Java]View PlainCopy
- 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
(iv) Obtaining the SD card storage root directory
[Java]View PlainCopy
- 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
(v) Get the data root directory
[Java]View PlainCopy
- 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
[Java]View PlainCopy
- 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
(vii) Get the file (clip) absolute path, relative strength, file (folder) name, parent directory
[Java]View PlainCopy
- 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
[Java]View PlainCopy
- File file= ...
- File[] Filelist=file.listfiles ();
(ix) Determine whether it is a file or a folder
[Java]View PlainCopy
- File file= ...
- Boolean is=file.isdirectory (); //true-Yes, false-no
(10) Determine if the file (clip) exists
[Java]View PlainCopy
- File file= ...
- Boolean is=file.exists (); //true-Yes, false-no
(11) New file (clip)
[Java]View PlainCopy
- 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
- }
(12) Renaming files (clips)
[Java]View PlainCopy
- 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);
(13) Delete file (clip)
[Java]View PlainCopy
- File file= ...
- File.delete (); //Delete now
- File.deleteonexit ()///program exit after deletion, only normal exit will be deleted
Android Getting Started file system operations (ii) file operation related instructions