Read a file or folder instance under the Android root directory _android

Source: Internet
Author: User
Tags parent directory

The last few days is really a variety of significance of the busy, busy exams, but also busy with curriculum design, hand and outsourced projects, the other side of the school project to do, their own things still in the document stage, it is really let people want to die Ah!!

Nearly half a month, C # This aspect of coding more, Android and Java here completely shelved, so take time to write a small example.

This is a very small example of a folder or file that reads in the Android root directory, as a hot body.

 @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinst 
  Ancestate); 
  Setcontentview (R.layout.main); 
  EditText Keywordtext = (edittext) This.findviewbyid (R.id.keyword); 
  Button button = (button) This.findviewbyid (R.id.button); 
  TextView result = (TextView) This.findviewbyid (R.id.result); Button.setonclicklistener (New Button.onclicklistener () {public void OnClick (View v) {String keyword = keywordte 
     Xt.gettext (). toString (); if (Keyword.equals ("")) {Result.settext ("Do not enter blank keyword!!");} 
     else {result.settext (searchfile (keyword)); 
} 
   } 
 }); 
  private string Searchfile (string keyword) {string result = ' "; 
  file[] files = new File ("/"). Listfiles (); 
   for (File file:files) {if (Fike.getname (). INDEXOF (keyword) >= 0) {result = File.getpath () + \ n;
 } if (Result.equals (")) {result =" file!! not found ";} 
return result; }

First, we need to get the names of all the files or folders under the folder through the File.listfiles () method, where the path to the folder is "/", which is the root directory. Then we get their relative paths by GetPath () one by one.

Examples of operating results such as:

Of course we need to add permissions to the root file:

 
 

Because the example is too small, the entire piece of paper is not long, so deliberately listed in the post all the file operations for your reference:

I. Operation of the SDK

1, to determine whether the SD card inserted

  Environment.getexternalstoragestate (). Equals (Android.os.Environment.MEDIA_MOUNTED); 

This is very useful in what I have done before, because it involves reading the SDK and naturally it is necessary to determine if the SDK is plugged in.

2, access to the SD card root directory:

 
 

Two. File operation in private directory:

1. Get the private root directory:

 
 

2. folder or folder operation:

(1) Get folder and file path

A. Obtain the absolute and relative paths of a file or folder. Difference

String path = File.getpath ();//relative 
String path = File.getabsoultepath ();/absolute 

B. Getting the parent directory of a file or folder

 
 

C. Obtain the name of the file or folder:

 
 

(2) Create a file or folder

File.mkdir (); Create a folder 
File.createnewfile ()//Create a file 

(3) Judgment is a file or folder

File.isdirectory () 
file.isdirectory ()

(4) List all file and folder names under the folder

 
 

(5) Modify folder and filename

 
 

(6) Delete a folder or file

 
 

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.