Java遍历文件夹&读取指定格式的文件zUI金

最近想自己写个Android音乐播放器练手,首先遇到的问题就是如何将手机里的歌曲文件扫描出来。百般思虑决定先用PC练练手。

其实网上有过用递归或者不用递归的方式。后来我有发现了大部分都是使用的递归,且是深度遍历的方式。下面是我整理的一种写法,使用递归扫描指定文件夹的文件并输出路径。

<span style="font-size:18px;">/**
	 * 读取某个文件下的所有文件
	 */
	public boolean readfile(String filepath) throws FileNotFoundException, IOException {
		try {
			File file = new File(filepath);
			<span style="background-color: rgb(51, 102, 255);"><span style="color:#ffcc66;">if (!file.isDirectory()) {
				System.out.println("name=" + file.getName());
				System.out.println("文件"+file.getName());
				System.out.println("path=" + file.getPath());
				System.out.println("absolutepath=" + file.getAbsolutePath());
				if (file.getName().endsWith(".mp3")) {
					System.out.println("-->"+file.getName());
//					mDocument = new Document(file.getPath(), file.getAbsolutePath(), file.getName());
//					mList.add(mDocument);
				}</span></span>
			}else if(file.isDirectory()){
				System.out.println("文件夹"+file.getName());
				String[] filelist=file.list();
				for(int i=0;i<filelist.length;i++){
					File secondfile=new File(filepath+"\\"+filelist[i]);
					if(!secondfile.isDirectory()){
						System.out.println("name=" + secondfile.getName());
						System.out.println("path=" + secondfile.getPath());
						System.out.println("absolutepath=" + secondfile.getAbsolutePath());
						if (secondfile.getName().endsWith(".mp3")) {
							System.out.println("-->"+secondfile.getName());
//							mDocument = new Document(file.getPath(), file.getAbsolutePath(), file.getName());
//							mList.add(mDocument);
						}
					}else if(secondfile.isDirectory()){
						readfile(filepath+"\\"+filelist[i]);
					}
				}
			}
		} catch (FileNotFoundException e) {
			System.out.println("readfile()   Exception:" + e.getMessage());
		}
		return true;
	}</span>
很多人可能会想那段用背景色标出的是否有必要。因为操作跟下面的是类似的。其实这一段是有必要的,因为你不知道filepath传过来是一个文件夹的路径还是一个文件的路径,如果没有这个判断,就会默认当成是文件夹,进行转换列表操作,那必然会报错,大家可以试试。

另外,我注释的那段是用来判断音乐文件的,如果是音乐文件,就将它存入list,当然这里作了一下封装,将文件对象进行了封装。封装代码如下:

/**
	 * 文件对象
	 */
	public class Document {
		String path;
		String absolutepath;
		String name;

		public Document() {
			super();
		}

		public Document(String path, String absolutepath, String name) {
			super();
			this.path = path;
			this.absolutepath = absolutepath;
			this.name = name;
		}

		public String getPath() {
			return path;
		}

		public void setPath(String path) {
			this.path = path;
		}

		public String getAbsolutepath() {
			return absolutepath;
		}

		public void setAbsolutepath(String absolutepath) {
			this.absolutepath = absolutepath;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

	}
这样的封装其实为了方便使用,以前不怎么写代码,现在写得多了经验才一点点的从解决问题中总结出来。所以,还是得多练练手,出现问题解决了才是真正学会了。看Demo永远停留在欣赏的角度。

对于那段for循环可以用下面的操作代替:

private static void readfile(File file) {
		File[] fillarry=file.listFiles();
		for(File f:fillarry){
			if(f.isDirectory()){
				System.out.println("文件夹"+f.getName());
				readfile(f);
			}else if(!f.isDirectory()){
				System.out.println("文件"+f.getName());
				if(f.getName().endsWith(".mp3")){
					System.out.println("-->"+f.getName());
				}
			}
		}
	}
唯一的区别就是for循环,具体名称说不出来,算是一种特别的方式吧!如果有大神知道,还望告知。
最后贴上之前搜的几个链接。

http://www.cnblogs.com/azhqiang/p/4596793.html





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值