实用正则表达式扫描android SDcard的文件
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
package
match;
import
java.io.File;
import
java.util.regex.Matcher;
import
java.util.regex.Pattern;
public
class
Utils {
/**
*
遍历指定文件夹下的资源文件
*
@param folder 文件
*/
public
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);
//
如果是文件夹则继续递归当前方法
if
(file.isDirectory()) {
simpleScanning(file);
}
//
如果是文件则对文件进行相关操作
else
{
Matcher
matcher = mPattern.matcher(name);
if
(matcher.matches()) {
//
文件名称
String
fileName = matcher.group( 1 );
//
文件后缀
String
fileExtension = matcher.group( 2 );
//
文件路径
String
filePath = file.getAbsolutePath();
if
(Utils.isMusic(fileExtension)) {
//
初始化音乐文件......................
System.out.println( "This
file is Music File,fileName=" +fileName+ "."
+fileExtension+ ",filePath=" +filePath);
}
if
(Utils.isPhoto(fileExtension)) {
//
初始化图片文件......................
System.out.println( "This
file is Photo File,fileName=" +fileName+ "."
+fileExtension+ ",filePath=" +filePath);
}
if
(Utils.isVideo(fileExtension)) {
//
初始化视频文件......................
System.out.println( "This
file is Video File,fileName=" +fileName+ "."
+fileExtension+ ",filePath=" +filePath);
}
}
}
}
}
}
/**
*
判断是否是音乐文件
*
@param extension 后缀名
*
@return
*/
public
static
boolean
isMusic(String extension) {
if
(extension == null )
return
false ;
final
String ext = extension.toLowerCase();
if
(ext.equals( "mp3" )
|| ext.equals( "m4a" )
|| ext.equals( "wav" )
|| ext.equals( "amr" )
|| ext.equals( "awb" )
||
ext.equals( "aac" )
|| ext.equals( "flac" )
|| ext.equals( "mid" )
|| ext.equals( "midi" )
||
ext.equals( "xmf" )
|| ext.equals( "rtttl" )
|| ext.equals( "rtx" )
|| ext.equals( "ota" )
||
ext.equals( "wma" )
||ext.equals( "ra" )
|| ext.equals( "mka" )
|| ext.equals( "m3u" )
|| ext.equals( "pls" ))
{
return
true ;
}
return
false ;
}
/**
*
判断是否是图像文件
*
@param extension 后缀名
*
@return
*/
public
static
boolean
isPhoto(String extension) {
if
(extension == null )
return
false ;
final
String ext = extension.toLowerCase();
if
(ext.endsWith( "jpg" )
|| ext.endsWith( "jpeg" )
|| ext.endsWith( "gif" )
|| ext.endsWith( "png" )
||
ext.endsWith( "bmp" )
|| ext.endsWith( "wbmp" ))
{
return
true ;
}
return
false ;
}
/**
*
判断是否是视频文件
*
@param extension 后缀名
*
@return
*/
public
static
boolean
isVideo(String extension) {
if
(extension == null )
return
false ;
final
String ext = extension.toLowerCase();
if
(ext.endsWith( "mpeg" )
|| ext.endsWith( "mp4" )
|| ext.endsWith( "mov" )
|| ext.endsWith( "m4v" )
||
ext.endsWith( "3gp" )
|| ext.endsWith( "3gpp" )
|| ext.endsWith( "3g2" )
||
ext.endsWith( "3gpp2" )
|| ext.endsWith( "avi" )
|| ext.endsWith( "divx" )
||
ext.endsWith( "wmv" )
|| ext.endsWith( "asf" )
|| ext.endsWith( "flv" )
||
ext.endsWith( "mkv" )
|| ext.endsWith( "mpg" )
|| ext.endsWith( "rmvb" )
||
ext.endsWith( "rm" )
|| ext.endsWith( "vob" )
|| ext.endsWith( "f4v" ))
{
return
true ;
}
return
false ;
}
}
|
下面使用该工具类进行测试指定的路径:/home/ouyangpeng/justForTest当前路径下放了一些测试文件,如下图所示:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
|
package
match;
import
java.io.File;
public
class
Test{
public
static
void
main(String[] args) {
String
path= "/home/ouyangpeng/justForTest" ;
File
file = new
File(path);
if
(file== null )
{
System.out.println( "file
does not exist" );
} else {
Utils.simpleScanning(file);
}
}
}
|
原文网址:https://blog.csdn.net/ouyang_peng/article/details/17008129
最后更新:2017-04-03 14:54:29