J2ME递归扫描文件夹下所有文件

网上一高人写的,由于项目需要COPY来用,以下为其应用方式。
主要实现给定一个文件路径参数,扫描遍历该路径下所有文件夹中文件,做相应处理.
第一步 创建FileSystem文件遍历类.
public class FileSystem {
 public Hashtable table = new Hashtable();
 public int level = 1;

 public void getTreeList(String path) {
  FileConnection fc = null;
  try {
   fc = (FileConnection) Connector.open(path,
     Connector.READ_WRITE);
   Enumeration enums = fc.list();
   Vector folderVector = new Vector();
   while (enums.hasMoreElements()) {
    String name = enums.nextElement().toString();
           //扫描文件路径(path+name)放入Hashtable表中
    table.put(path+name, new Integer(level));
    if (name.indexOf("/") > 0) {
     folderVector.addElement(name);
    }
   }
   int size = folderVector.size();
   if (size > 0) {
    level++;
    for (int i = 0; i < size; i++) {
     String tmpPath = path
       + folderVector.elementAt(i).toString();
     getTreeList(tmpPath);
    }
   } else {
    return;
   }
  } catch (IOException ex) {
   ex.printStackTrace();
  }finally {
   try {
    if (fc != null) {
     fc.close();
    }
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }
}
第二步: 在一线程中调用FileSystem 类对象,遍历Hashtable得到指定文件夹下文件路径,做指定操作.
public void run() {
                String sdCardFileUrl = "file///sdcarddire/";
  FileSystem fs = new FileSystem();

  fs.getTreeList(sdCardFileUrl);
  Enumeration enums = fs.table.keys();
  while (enums.hasMoreElements()) {
   String fileUrl = enums.nextElement().toString();
                        //addplayList(fileUrl)方法对文件遍历得到文件路径处理具体操作
   addPlayList(fileUrl);
  }
}
第三步: 我项目中addPlayList(fileUrl)方法为 扫描所有MP3文件,获取其歌曲信息保存到RMS中.同时记录MP3文件

扫描个数.
private void addPlayList(String fileUrl) {
  String url = fileUrl;

  String format = fileUrl.substring(fileUrl.length() - 3);
  if (format.equals("mp3")) {
   fileCount++;
   musicAll = amrms.getRecords(Parameter.searchlistRMS);
   SongInfo info = getSongInfo(url);

   amrms.addRecord(Parameter.searchlistRMS,
     info.getSongName() == null ? "" : info.getSongName(), info
       .getArtist() == null ? "" : info.getArtist(),

info
       .getAlbum() == null ? "" : info.getAlbum(),
     url == null ? "" : url, format, Long.parseLong(info
       .getComment()), other, 0);

   Parameter.fileNum = fileCount;
  }
 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值