Android reads files or folders under the root directory

Source: Internet
Author: User

1 @Override2  Public voidonCreate (Bundle savedinstancestate) {3    Super. OnCreate (savedinstancestate);4 Setcontentview (R.layout.main); 5EditText Keywordtext = (EditText) This. Findviewbyid (;6Button button = (Button) This. Findviewbyid (;7TextView result = (TextView) This. Findviewbyid (;8Button.setonclicklistener (NewButton.onclicklistener () {9        Public voidOnClick (View v) {TenString keyword =Keywordtext.gettext (). toString (); One          if(Keyword.equals ("")) {  AResult.settext ("Do not enter a blank keyword!!"));  -}Else {  - result.settext (searchfile (keyword)); the          }  -      }  -   });  - } +  - Privatestring searchfile (string keyword) { +String result = "";  Afile[] Files =NewFile ("/"). Listfiles ();  at     for(File file:files) { -      if(Fike.getname (). INDEXOF (keyword) >= 0) {  -Result + = File.getpath () + "\ n";  -      }  -    }  -   if(Result.equals ("")){ inresult = "File not found!!";  -   } to  returnresult; +}

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

Examples of running results such as:

Android reads files or folders under the root directory

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: 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.