Introduction to Android File system operations (ii) file operation related instructions

Source: Internet
Author: User

(i) Access to the total root

[Java]View PlainCopy
    1. File[] Filelist=file.listroots ();
    2. Returns filelist.length to 1
    3. Filelist.getabsolutepath () is "/"
    4. This is the total root of the system.

(ii) Open the total root directory

[Java]View PlainCopy
    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

(iii) Obtaining the system storage root directory

[Java]View PlainCopy
    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

(iv) Obtaining the SD card storage root directory

[Java]View PlainCopy
    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

(v) Get the data root directory

[Java]View PlainCopy
    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

(vi) Get the private file path

[Java]View PlainCopy
    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

(vii) Get the file (clip) absolute path, relative strength, file (folder) name, parent directory

[Java]View PlainCopy
    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

(eight) List all files and folders under the folder

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

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

[Java]View PlainCopy
    1. File file= ...
    2. Boolean is=file.isdirectory (); //true-Yes, false-no

(10) Determine if the file (clip) exists

[Java]View PlainCopy
    1. File file= ...
    2. Boolean is=file.exists (); //true-Yes, false-no

(11) New file (clip)

[Java]View PlainCopy
  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. }

(12) Renaming files (clips)

[Java]View PlainCopy
    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);

(13) Delete file (clip)

[Java]View PlainCopy
      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 operations (ii) file operation related instructions

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.