JAVA 音乐文件读取工具

项目要求 

a)读取指定目录的所有mp3文件,包括所有子目录
b)将所读取的文件呈现在控制台


代码如下

import java.io.*;
import java.util.Scanner;

public class fileMP3 {

	private static Scanner input = new Scanner(System.in);

	public static void main(String[] args) {

		System.out.println("Please enter the directory to search: ");
		String songPath = input.next();
		File songFile = new File(songPath);
		System.out.println("\nFind the MP3 files in the specified directory("
				+ songPath + "): ");
		findMP3(songFile, songPath);
		System.out.println("----Find end----");
	}

	private static void findMP3(File songFile, String songPath) {

		File[] songList = songFile.listFiles();

		if (songList != null) {
			for (File s : songList) {

				// 用递归法来查找各个子目录
				if (s.isDirectory() == true) {
					findMP3(s, s.getAbsolutePath());

				} else {

					// String类下的endsWith来判断文件后缀
					if (s.getAbsolutePath().endsWith(".mp3") == true
							|| s.getAbsolutePath().endsWith("MP3")) {
						System.out.println(s.getAbsolutePath());

					}
				}
			}
		}
	}
}


文件截图



运行结果

Please enter the directory to search: 
src

Find the MP3 files in the specified directory(src): 
D:\eclipse\workspace\homework\src\Tynisha Keli - Lullabye.mp3
D:\eclipse\workspace\homework\src\房祖名 - 最好的我.mp3
----Find end----

学习心得

本来想把要查找的路径在代码中敲出,但是好像涉及到字符串中的反斜杠问题…不知道有没有什么好的处理方法?因此改用了自己输入路径的方式。

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值