661
技術社區[雲棲]
實用正則表達式掃描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