使用Java I/O流快速搜索手机文件引擎
文件搜索功能可以快速协助我们找到想要的文件,在手机上制作一个文件搜索的功能其实并不困难,Java I/O的API中提供了java.io.File对象,只要利用File对象的方法,再搭配Android的EditText、TextView等对象,就可以轻松做出一个手机的文件搜索引擎。
下面是程序源码:
package com.example.test01; import java.io.File; import java.util.Iterator; import android.app.Activity; importandroid.content.Context; importandroid.content.res.TypedArray; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; importandroid.view.ViewGroup; importandroid.widget.AdapterView.OnItemClickListener; importandroid.widget.AdapterView; importandroid.widget.BaseAdapter; importandroid.widget.Button; importandroid.widget.EditText; importandroid.widget.Gallery; importandroid.widget.ImageView; importandroid.widget.TextView; import android.widget.Toast; public class MainActivityextends Activity { Button button01; EditText editText01; TextView textView01; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button01 = (Button)findViewById(R.id.button01); editText01 = (EditText)findViewById(R.id.editText01); textView01 = (TextView)findViewById(R.id.textView01); button01.setOnClickListener(new OnClickListener() { public void onClick(View v) { //取得输入的关键字 String string =editText01.getText().toString(); if (string.equals("")) { textView01.setText("请勿输入空白的关键字!"); } else { textView01.setText(SearchFile(string)); } } }); } //搜索文件的方法 public String SearchFile(String string) { String result = ""; File[] file = new File("/").listFiles(); for (File file2 : file) { if (file2.getName().indexOf(string) >= 0) { result += file2.getPath() +"\n"; } } if (result.equals("")) result = "找不到文件!"; return result; } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action barif it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
运行效果如图:
扩展学习:
在本范例中,searchFile(String)方法的功能仅为搜索根目录下符合关键字的文件,并没有进一步再对子目录下的文件进行搜索。如果想实现这个功能,我们可以利用File.isDirectory()这个方法来判断其是否为目录,如果是则继续向下搜索,否则终止向下搜索。所以我们需要运用递归的思想,下面是对searchFile()方法的改进,考虑到手机能否负荷大规模的文件搜索,我们将起始搜索位置定在sdcard中:
//搜索文件的方法,参数分别为关键字,搜索目录,搜索结果 public String SearchFile(String keywords , String filePath ,String result) { String temp = ""; File[] file; if (filePath == null) { file = newFile("/sdcard").listFiles(); } else { file = new File(filePath).listFiles(); } for (File file2 : file) { if (file2.isDirectory()) { //不搜索系统文件夹 if(!file2.getName().contains(".")) { temp += SearchFile(keywords,file2.getAbsolutePath(), result); } } else { if (file2.getName().contains(keywords)) { temp += file2.getPath() +"\n"; } } } if (temp.equals("")) temp = ""; return temp; }
运行效果图:
2013-03-27 17:04:40.608 testView[4685:c07] <UIView: 0x7553320; frame = (0 20; 320 460); autoresize = RM+BM; layer = <CALayer: 0x7553380>>
2013-03-27 17:04:40.609 testView[4685:c07] <UIScrollView: 0x7551890; frame = (0 0; 0 0); clipsToBounds = YES; autoresize = TM+BM; gestureRecognizers = <NSArray: 0x75528a0>; layer = <CALayer: 0x7551ab0>; contentOffset: {0, 0}>
2013-03-27 17:04:40.610 testView[4685:c07] <UIScrollView: 0x7551890; frame = (0 0; 0 0); clipsToBounds = YES; autoresize = TM+BM; gestureRecognizers = <NSArray: 0x75528a0>; layer = <CALayer: 0x7551ab0>; contentOffset: {0, 0}>
2013-03-27 17:04:40.613 testView[4685:c07] view bounds-origin: 0.00 0.00 size:320.00 460.00
2013-03-27 17:04:40.614 testView[4685:c07] bounds-origin: 0.00 0.00 size:0.00 0.00
2013-03-27 17:16:18.755 testView[4745:c07] <UIView: 0x7557620; frame = (0 20; 320 460); autoresize = RM+BM; layer = <CALayer: 0x7557680>>
2013-03-27 17:04:40.618 testView[4685:c07] view bounds-origin: 0.00 0.00 size:320.00 460.00
2013-03-27 17:04:40.620 testView[4685:c07] scrollview bounds-origin: 0.00 0.00 size:0.00 0.00
2013-03-27 17:16:18.773 testView[4745:c07] <UIView: 0x7557620; frame = (0 20; 320 460); autoresize = RM+BM; layer = <CALayer: 0x7557680>>
2013-03-27 17:04:40.637 testView[4685:c07] view bounds-origin: 0.00 0.00 size:320.00 460.00
2013-03-27 17:04:40.639 testView[4685:c07] scrollview bounds-origin: 0.00 0.00 size:320.00 460.00
2013-03-27 17:19:30.464 testView[4774:c07] --- moved.. ---
2013-03-27 17:19:30.465 testView[4774:c07] <UIView: 0x75b3d80; frame = (0 20; 320 460); autoresize = RM+BM; layer = <CALayer: 0x75b3de0>>
2013-03-27 17:19:30.465 testView[4774:c07] <UIScrollView: 0x75b22f0; frame = (0 0; 320 460); clipsToBounds = YES; autoresize = TM+BM; gestureRecognizers = <NSArray: 0x75b3300>; layer = <CALayer: 0x75b2510>; contentOffset: {0, -50}>
2013-03-27 17:19:30.467 testView[4774:c07] view bounds-origin: 0.00 0.00 size:320.00 460.00
public class Test//定义一个类,名字叫Test { public int add(int a, int b)//定义一个方法(add),两个整数相加的方法 { return a + b;//将a + b的值返回回去 } public static void main(String[] args) { Test test = new Test();//生成一个新的对象 int a = 3; int b = 4;//将a,b赋初值为3,4 int c = test.add(a, b);//调用add方法,将运算后结果的值赋给c System.out.println(a); System.out.println(b); System.out.println(c);//输出打印a、b、c的值 } }
1楼maomao9691693439小时前package u;nnpublic class Plus {ntpublic int plus(int a, int b) {nttreturn a + b;nt}nntpublic static void main(String[] args) {nttSystem.out.println(new Plus().plus(1, 2));nt}n}nnnn这样写会不会更简单勒