Android遍历所有文件夹和子目录搜索文件_Android

本文实例为大家分享了android遍历所有文件夹和子目录来搜索文件,供大家参考,具体内容如下

java代码:

import java.io.File;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class ShuosouwenjianActivity extends Activity implements OnClickListener {

  private File file;
  private String path;
  private String info;
  private String key; //关键字
  private TextView result; // 显示结果
  private EditText et; // 编辑view
  private Button search_btn; // button view 

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    result = (TextView)findViewById(R.id.TextView_Result);
    et = (EditText)findViewById(R.id.key);
    search_btn = (Button)findViewById(R.id.button_search);
    // file = new File(Environment.getExternalStorageDirectory().getPath());
    file = new File("/sdcard/");
    info = getString(R.string.info); 

    search_btn.setOnClickListener(this);
  } 

  @Override
  public void onClick(View v) {
    // TODO Auto-generated method stub
    path = "";
    result.setText("");
    key = et.getText().toString();
    BrowserFile(file);
  } 

  public void BrowserFile(File fileold) {
    if (key.equals("")) {
      Toast.makeText(this, getString(R.string.pleaseInput), Toast.LENGTH_LONG).show();
    } else {
     search(fileold);
      if (result.getText().equals("")) {
        Toast.makeText(this, getString(R.string.notFound), Toast.LENGTH_SHORT).show();
      }
    }
  } 

 private void search(File fileold)

  {

   try{

 File[] files=fileold.listFiles();

 if(files.length>0)

 {

   for(int j=0;j<files.length;j++)

   {

  if(!files[j].isDirectory())

  {

  if(files[j].getName().indexOf(key)> -1)

  {

  path += "\n" + files[j].getPath();
    result.setText(info+path);

     //shuju.putString(files[j].getName().toString(),files[j].getPath().toString());

  }

  }

  else{

  this.search(files[j]);

  }

   }

 }

 }

   catch(Exception e)

   {

   }

  }

}

MAIN.XML代码:

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:id="@+id/widget0"
  > 

<Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@+id/button_search"
  android:layout_x="253px"
  android:layout_y="5px"
  android:text="@string/toSearch"
/>
<EditText
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@+id/key"
  android:text="821077962.db" 

/> 

<TextView
  android:layout_width="fill_parent"
  android:layout_height="370px"
  android:id="@+id/TextView_Result"
  android:layout_x="0px"
  android:layout_y="60px"
/>
</AbsoluteLayout>

strings.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string name="hello">Hello World, Activity07!</string>
  <string name="app_name">文件搜索</string> 

  <string name="toSearch">搜索</string>
  <string name="info">系统SDCard目录文件路径:\n</string>
  <string name="pleaseInput">请输入关键字!</string>
  <string name="notFound">SD卡中没有相关文件!!</string>
  <string name="pathError">读取路径出错!!</string>
</resources>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索android遍历文件夹
, java遍历文件夹
android搜索文件
linux遍历目录文件夹、php 遍历文件夹目录、android遍历文件夹、android 遍历子文件夹、android 遍历目录文件,以便于您获取更多的相关知识。

时间: 2024-08-31 21:12:19

Android遍历所有文件夹和子目录搜索文件_Android的相关文章

Android遍历所有文件夹和子目录搜索文件

本文实例为大家分享了android遍历所有文件夹和子目录来搜索文件,供大家参考,具体内容如下 java代码: import java.io.File; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget

asp.net遍历目录文件夹和子目录所有文件_实用技巧

复制代码 代码如下: using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Threading; namespace copefile {     class Program     {         static void Main(string[] args)         {             string testDir = "e:/xun

Windows Explorer(浏览)对话框和遍历文件夹及其子目录函数(FindFisrFile FindNextFile CloseFind)使用

Windows浏览窗口就是你在安装程序的时候经常叫你选择安装在哪个目录里面的对话框,我们一般使用这个对话框来获得用户选择的目录.   (一)主要牵扯到的函数是: PIDLIST_ABSOLUTE SHBrowseForFolder(lpbi)   此函数会调用Windows Explorer(浏览)对话窗口,如果用户点击确定的话,会返回一个PIDLIST_ABSOLUTE(绝对路径标识序列). 1:首先讲解返回值:   PIDLIST_ABSOLUTE是PITEMIDLIST的别称,之所以别称主

C#遍历文件夹及其子目录的完整实现方法_C#教程

本文实例讲述了C#遍历文件夹及其子目录的完整实现方法.分享给大家供大家参考,具体如下: using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Security.AccessControl; using System.Text; namespace ConsoleApplication1 { class Program { static void

使用PHP遍历文件夹与子目录的函数代码_php技巧

我们要使用的函数有 Scandir,它的作用是列出指定路径中的文件和目录,就像 Dir 一样. > 与更强力的 Glob() 函数,作用是以数组的形式返回与指定模式相匹配的文件名或目录. > 友情提醒,千万别像小邪那样在电脑前面呆太长时间,否则就会像小邪一样得见鬼的高血糖. 一. 遍历单层文件夹: > 在扫描单层文件夹的问题是,两个函数的结果虽有不同,不过表现是相差不大的. > Scandir 函数会提供额外两行,分别是 "." 和 ".."

Android递归方式删除某文件夹下的所有文件(.mp3文件等等)_Android

1.由于需要删除文件,因此需要如下权限: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> 2.核心代码 复制代码 代码如下: package com.example.deleteyoumi; import java.io.File; import android.os.Bundle; import android.os.Han

Android递归方式删除某文件夹下的所有文件(.mp3文件等等)

1.由于需要删除文件,因此需要如下权限: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> 2.核心代码 复制代码 代码如下: package com.example.deleteyoumi; import java.io.File; import android.os.Bundle; import android.os.Han

PHP遍历指定文件夹下的所有文件

PHP遍历指定文件夹下的所有文件,如下代码: <?php   session_start();  if($Submit=="提交"){   $shili=$file_dir;  }  ?>  <html>  <head>  <meta http-equiv="Content-Type" content="text/html; charset=gb2312">  <title>遍历指定目录

asp.net遍历文件夹下所有子文件夹并绑定到gridview上的方法_实用技巧

遍历文件夹下所有子文件夹,并且遍历配置文件某一节点中所有key,value并且绑定到GridView上 Helper app_Helper = new Helper(); DataSet ds = new DataSet(); DataTable dt = new DataTable(); protected void Page_Load(object sender, EventArgs e) { gvwBind(); } #region 绑定GridView /// <summary> //