1 @Override2 Public voidonCreate (Bundle savedinstancestate) {3 Super. OnCreate (savedinstancestate);4 Setcontentview (R.layout.main); 5EditText Keywordtext = (EditText) This. Findviewbyid (R.id.keyword);6Button button = (Button) This. Findviewbyid (R.id.button);7TextView result = (TextView) This. Findviewbyid (R.id.result);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