01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
@H_403_208@
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
package
match;
import
java.io.File;
java.util.regex.Matcher;
java.util.regex.Pattern;
public
class
Utils {
/**
* @param folder 文件
*/
static
void
simpleScanning(File folder) {
//指定正则表达式
Pattern mPattern = Pattern.compile(
"([^\\.]*)\\.([^\\.]*)"
);
// 当前目录下的所有文件
final
String[] filenames = folder.list();
// 当前目录的名称
//final String folderName = folder.getName();
// 当前目录的绝对路径
//final String folderPath = folder.getAbsolutePath();
if
(filenames !=
null
) {
// 遍历当前目录下的所有文件
for
(String name : filenames) {
File file =
new
File(folder,name);
(file.isDirectory()) {
simpleScanning(file);
}
else
{
Matcher matcher = mPattern.matcher(name);
(matcher.matches()) {
String fileName = matcher.group(
1
);
// 文件后缀
String fileExtension = matcher.group(
2
);
// 文件路径
String filePath = file.getAbsolutePath();
(Utils.isMusic(fileExtension)) {
// 初始化音乐文件......................
System.out.println(
"This file is Music File,fileName="
+fileName+
"."
+fileExtension+
",filePath="
+filePath);
}
(Utils.isPhoto(fileExtension)) {
"This file is Photo File,255)!important">"."
+filePath);
}
(Utils.isVideo(fileExtension)) {
// 初始化视频文件......................
"This file is Video File,255)!important">"."
+filePath);
}
}
}
}
}
}
/**
* 判断是否是音乐文件
* @param extension 后缀名
* @return
*/
boolean
isMusic(String extension) {
(extension ==
)
return
false
;
String ext = extension.toLowerCase();
(ext.equals(
"mp3"
) || ext.equals(
"m4a"
"wav"
"amr"
"awb"
) ||
ext.equals(
"aac"
"flac"
"mid"
"midi"
) ||
"xmf"
"rtttl"
"rtx"
"ota"
) ||
"wma"
) ||ext.equals(
"ra"
"mka"
"m3u"
"pls"
)) {
true
;
}
;
}
/**
* 判断是否是图像文件
* @param extension 后缀名
* @return
*/
isPhoto(String extension) {
)
;
String ext = extension.toLowerCase();
(ext.endsWith(
"jpg"
) || ext.endsWith(
"jpeg"
"gif"
"png"
) ||
ext.endsWith(
"bmp"
"wbmp"
)) {
;
}
;
}
/**
* 判断是否是视频文件
* @param extension 后缀名
* @return
*/
isVideo(String extension) {
)
;
String ext = extension.toLowerCase();
"mpeg"
"mp4"
"mov"
"m4v"
) ||
"3gp"
"3gpp"
"3g2"
) ||
"3gpp2"
"avi"
"divx"
) ||
"wmv"
"asf"
"flv"
) ||
"mkv"
"mpg"
"rmvb"
) ||
"rm"
"vob"
"f4v"
)) {
;
}
;
}
}
|
下面使用该工具类进行测试指定的路径:/home/ouyangpeng/justForTest 当前路径下放了一些测试文件,如下图所示:
15
Test{
main(String[] args) {
String path=
"/home/ouyangpeng/justForTest"
;
File(path);
(file==
) {
"file does not exist"
);
}
{
Utils.simpleScanning(file);
}
}
原文网址:http://blog.csdn.net/ouyang_peng/article/details/17008129