Android Getting Started file system operation

Source: Internet
Author: User

Getting Started with Android file system operations (ii) file operations related directives (RPM) (i) Get the total root
    1. File[] Filelist=file.listroots ();
    2. Returns filelist.length to 1
    3. Filelist.getabsolutepath () is "/"
    4. 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

    1. File file=new file ("/");
    2. File[] Filelist=file.listfiles ();
    3. Get the directory In addition to "/sdcard" and "/system" there are "/data", "/cache", "/dev" and so on
    4. The root directory of Android is not divided into C-disk, D-disk, e-disk, etc. as Symbian system.
    5. 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

    1. File file=environment.getrootdirectory ();  //file file=new File ("/system");
    2. File[] Filelist=file.listfiles ();
    3. The system here refers only to "/system."
    4. 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

    1. File file=environment.getexternalstoragedirectory ();  //file file=new File ("/sdcard");
    2. File[] Filelist=file.listfiles ();
    3. To obtain an SD card, first verify that the SD card is loaded
    4. Boolean is=environment.getexternalstoragestate (). Equals (environment.media_mounted);
    5. If true, it is loaded
    6. 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

    1. File file=environment.getdatadirectory ();  //file file=new File ("/data");
    2. File[] Filelist=file.listfiles ();
    3. 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

    1. Context context= this; First, get context in the activity.
    2. File File=context.getfilesdir ();
    3. String Path=file.getabsolutepath ();
    4. The path returned here is/data/data/packet/files, where the package is the package where we built the main activity
    5. We can see that this path is also in the Data folder
    6. The program itself can operate on its own private files
    7. 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

    1. File file= ...
    2. String Relativepath=file.getpath (); //relative path
    3. String Absolutepath=file.getabsolutepath (); //absolute path
    4. String Filename=file.getname (); //file (folder) name
    5. 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

    1. File file= ...
    2. File[] Filelist=file.listfiles ();
File file= ... File[] Filelist=file.listfiles ();

(ix) Determine whether it is a file or a folder

    1. File file= ...
    2. 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

    1. File file= ...
    2. Boolean is=file.exists (); //true-Yes, false-no
File file= ... boolean is=file.exists ();//true-Yes, false-no

(11) New file (clip)

  1. File file= ...
  2. Oolean is=file.isdirectory (); //Decide whether to be a folder
  3. /* Method 1*/
  4. if (IS) {
  5. String Path=file.getabsolutepath ();
  6. String name="ABC"; You want to create a new folder name or file name
  7. String Pathx=path+name;
  8. File filex=new file (PATHX);
  9. boolean is=filex.exists (); //Determine if the file (clip) exists
  10. if (!is) {
  11. Filex.mkdir (); //Create a folder
  12. //filex.createnewfile ();//Create File
  13. }
  14. /* Method 2*/
  15. if (IS) {
  16. String Path=file.getabsolutepath ();
  17. String name="test.txt"; You want to create a new folder name or file name
  18. File filex=new file (path,name); The difference between Method 1 and Method 2 is that this
  19. boolean is=filex.exists (); //Determine if the file (clip) exists
  20. if (!is) {
  21. Filex.mkdir (); //Create a folder
  22. //filex.createnewfile ();//Create File
  23. }
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)

    1. File file= ...
    2. String parentpath=file.getparent ();
    3. String newname="name"; Renamed file or folder name
    4. File filex=new file (parentpath,newname); File Filex=new file (parentpaht+newname)
    5. 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)

    1. File file= ...
    2. File.delete (); //Delete now
    3. File.deleteonexit ()///program exit after deletion, only normal exit will be deleted

Android Getting Started file system operation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.