太陽(yáng)雨
痛并快樂(lè)著
BlogJava
首頁(yè)
新隨筆
聯(lián)系
聚合
管理
67 Posts :: 3 Stories :: 33 Comments :: 0 Trackbacks
常用鏈接
我的隨筆
我的評(píng)論
我的參與
最新評(píng)論
留言簿
(4)
給我留言
查看公開留言
查看私人留言
隨筆分類
hibernate相關(guān)(9)
j2se(9)
javaEE(9)
Javascript相關(guān)(10)
spring相關(guān)(6)
struts相關(guān)
ubuntu相關(guān)(2)
windows相關(guān)
個(gè)人經(jīng)驗(yàn)(12)
個(gè)人隨筆(4)
數(shù)據(jù)庫(kù)(6)
設(shè)計(jì)模式
隨筆檔案
2012年7月 (1)
2010年4月 (5)
2010年3月 (4)
2010年2月 (5)
2010年1月 (1)
2009年11月 (2)
2009年10月 (5)
2009年8月 (1)
2009年7月 (4)
2009年4月 (1)
2009年3月 (2)
2009年2月 (2)
2009年1月 (1)
2008年12月 (7)
2008年11月 (13)
2008年10月 (8)
2008年6月 (5)
文章檔案
2008年11月 (1)
2008年10月 (1)
2008年6月 (1)
搜索
最新評(píng)論
1.?re: 使用Digester,對(duì)象和xml進(jìn)行轉(zhuǎn)換
同一節(jié)點(diǎn)下解析成多個(gè)對(duì)象 求解
--flyaway
2.?re: Hibernate常見異常總結(jié)(不斷補(bǔ)充)
評(píng)論內(nèi)容較長(zhǎng),點(diǎn)擊標(biāo)題查看
--re: Hibernate常見異常總結(jié)(不斷補(bǔ)充)
3.?re: Hibernate常見異常總結(jié)(不斷補(bǔ)充)
評(píng)論內(nèi)容較長(zhǎng),點(diǎn)擊標(biāo)題查看
--re: Hibernate常見異常總結(jié)(不斷補(bǔ)充)
4.?re: Iframe跨域訪問(wèn)的問(wèn)題[未登錄](méi)
IEKUAYU
--sdfsd
5.?re: Iframe跨域訪問(wèn)的問(wèn)題
是的方式的
--打發(fā)士大夫
6.?re: MyEclipse8.5漢化
感謝樓主,真的漢化 成功了。。。哈哈哈
--xiqi
7.?re: MyEclipse8.5漢化
@1234565
好二
--大三大四的
8.?re: 在Java web服務(wù)器內(nèi)使用url rewrite(鏈接)
fdsafdsa
--ed
9.?re: XFire與Spring結(jié)合的幾種方式
@羽
xfire已經(jīng)很久沒(méi)有用過(guò)了,而且已經(jīng)轉(zhuǎn)變?yōu)镃XF了,同學(xué)還是看下CXF吧,而且支持注解很方便,要與時(shí)俱進(jìn)嘛
--樓主
10.?re: XFire與Spring結(jié)合的幾種方式
您好,我的wsdl文檔是生成了,但是客戶端調(diào)用service的時(shí)候,service里面的entityManager沒(méi)有被初始化,為null,spring是不是還有配置啊????
--羽
閱讀排行榜
1.?eclipse中集成jad反編譯(11075)
2.?添加xml文件自動(dòng)提示方法,以spring配置文件為例(10547)
3.?Hibernate常見異常總結(jié)(不斷補(bǔ)充) (9258)
4.?Tomcat6下應(yīng)用太多出現(xiàn)java.lang.OutOfMemoryError: PermGen space(8080)
5.?oracle的正則表達(dá)式(7812)
評(píng)論排行榜
1.?MyEclipse8.5漢化(11)
2.?eclipse中集成jad反編譯(7)
3.?Tomcat6下應(yīng)用太多出現(xiàn)java.lang.OutOfMemoryError: PermGen space(4)
4.?XFire與Spring結(jié)合的幾種方式(2)
5.?Iframe跨域訪問(wèn)的問(wèn)題(2)
JAVA操作大型文本文件
在前面的廢話不想看的可以skip-----由于工作需要,經(jīng)常需要打開和查詢250M左右的文本文件(總行數(shù)超過(guò)250萬(wàn)行)分析日志,公司發(fā)的筆記本內(nèi)存才512M,地球人都知道,這樣的電腦根本不能勝任。不知道有沒(méi)有人和我一樣倒霉,面對(duì)這么龐大的文件,曾經(jīng)讓我很無(wú)奈,很彷徨。從尋找優(yōu)秀的類似notepad這樣的文本工具,企圖通過(guò)工具來(lái)提高下速度,其中JIURL這個(gè)記事本工具,我個(gè)人覺(jué)得很好,它陪我走了好長(zhǎng)時(shí)間,雖然它老是有這樣那樣的BUG,不過(guò)它的速度確實(shí)比其他的記事本快很多,對(duì)于巨型文件,不會(huì)像其他記事本那樣直接掛死。工具再好,也是死的,經(jīng)常操作一樣的動(dòng)作,讓我想撞墻。偶又去研究了Linux下的sh命令,哎,不是我說(shuō)MicroSoft的系統(tǒng)不好,Linux的命令確實(shí)好使,特別是查詢文檔,日志分析的時(shí)候,只要你使用的好,基本都能完成你的任務(wù)。像grep,gawk,wc,head,hail etc.這樣的命令總是能給我們帶來(lái)驚喜。Linux系統(tǒng)還是要好好學(xué)的,雖然和widows差別很大,但是很強(qiáng),很方便。 下面我就來(lái)說(shuō)說(shuō)用JAVASE怎么操作這些龐然大物的。 先介紹下我要操作的文本文件 [28-03-2008 00:00:00.101] |SH1|CIC015|->|SH1|AG015| >>IAM|CN15|CD51001500|CG15921351879|PA0|PB1|<< [28-03-2008 00:00:00.101] |SH1|CIC153|->|SH1|AG144| >>IAM|CN144|CD51001500|CG13761957943|PA0|PB1|<< [28-03-2008 00:00:00.117] |SH1|CIC015|<-|SH1|AG015| >>ACM|CN15|<< [28-03-2008 00:00:00.117] |SH1|CIC153|<-|SH1|AG144| >>ACM|CN144|<< [28-03-2008 00:00:00.179] |SH1|CIC111|->|SH1|AG105| >>REL|CN105|PA16|PB0|PC4|<< [28-03-2008 00:00:00.179] |SH1|CIC111|<-|SH1|AG105| >>RLC|CN105|<< [28-03-2008 00:00:00.273] |SH1|CIC1373|->|SH3|AG118| >>ANM|CN118|<< [28-03-2008 00:00:00.383] |SH1|CIC1365|<-|SH3|AG110| >>REL|CN110|PA16|<< [28-03-2008 00:00:00.461] |SH1|CIC1365|->|SH3|AG110| >>RLC|CN110|<< [28-03-2008 00:00:00.586] |SH1|CIC2577|->|S11|AG196| >>ANM|CN196|<< 大家不要頭暈,這個(gè)本來(lái)就是日志,搞過(guò)No.7的人都看的懂,不過(guò)這個(gè)不是重點(diǎn)了。我們要處理就是這樣的文件,JAVASE里有一個(gè)新的包 java.nio,具體干啥的,我就不說(shuō)了,自己去查看API吧。下面貼出具體代碼,大家重點(diǎn)看getResult()
方法
。在里面使用了MappedByteBuffer把文件映射到內(nèi)存里面去,然后使用緩存的技術(shù)把他再讀出來(lái),如果不適用緩存,速度會(huì)很慢,想在這個(gè)里面查個(gè)東西,至少要7分鐘以上,使用了緩存整個(gè)文件遍歷最多13秒,差距很大吧,這個(gè)時(shí)間還是和硬件有關(guān)系的,我的電腦比較爛了,我老婆經(jīng)常說(shuō)我的是“低廉的雙核”,我那個(gè)汗啊!o(∩_∩)o...,大家好好看看里面的代碼吧。希望能對(duì)經(jīng)常和大型文件打交道的人有個(gè)幫助!
1
package
com.xhp.ss7 ;
2
3
import
java.io.File ;
4
import
java.io.RandomAccessFile ;
5
import
java.io.BufferedReader ;
6
import
java.io.StringReader ;
7
import
java.util.List ;
8
import
java.util.ArrayList ;
9
import
java.util.Date ;
10
import
java.util.regex.Pattern ;
11
import
java.util.regex.Matcher ;
12
import
java.nio.MappedByteBuffer ;
13
import
java.nio.channels.FileChannel ;
14
15
16
public
class
SS7Handle
{
17
private
String year ;
18
private
String month ;
19
private
String day ;
20
private
long
length ;
21
private
File file ;
22
private
int
bufferSize
=
0x600000
;
//
默認(rèn)6M的緩沖 ;
23
private
long
diff ;
24
25
26
public
SS7Handle(String path)
{
27
this
.loadFile(path) ;
28
}
29
30
public
List
<
String
>
getResult(
int
type,String number,String time,String minute,String second,
int
count)
{
31
List
<
String
>
result
=
new
ArrayList
<
String
>
() ;
32
String strRex
=
getStrRex(type,number,time,minute,second) ;
33
Pattern pattern
=
Pattern.compile(strRex) ;
34
int
counts
=
count ;
35
Date begin
=
null
;
36
Date end
=
null
;
37
try
{
38
MappedByteBuffer inputBuffer
=
new
RandomAccessFile(file,
"
r
"
).getChannel()
39
.map(FileChannel.MapMode.READ_ONLY,
0
, length);
40
byte
[] dst
=
new
byte
[bufferSize] ;
//
每次讀出6M的內(nèi)容
41
BufferedReader br
=
null
;
42
boolean
findFirst
=
false
;
43
boolean
over
=
false
;
44
begin
=
new
Date() ;
45
for
(
int
offset
=
0
; offset
<
length; offset
+=
bufferSize)
{
46
//
for(int offset=0; offset < 1; offset+= bufferSize){
47
48
if
(length
-
offset
>=
bufferSize)
{
49
for
(
int
i
=
0
;i
<
bufferSize;i
++
)
50
dst[i]
=
inputBuffer.get(offset
+
i);
51
}
else
{
52
for
(
int
i
=
0
;i
<
length
-
offset;i
++
)
53
dst[i]
=
inputBuffer.get(offset
+
i);
54
}
55
//
把得到的dst 交給 BufferedReader 按行讀出
56
br
=
new
BufferedReader(
new
StringReader(
new
String(dst))) ;
57
String line ;
58
StringBuffer next
=
new
StringBuffer() ;
59
60
while
((line
=
br.readLine())
!=
null
)
{
61
if
(
!
findFirst)
{
62
if
(pattern.matcher(line).find())
{
63
System.out.println(
"
line-->
"
+
line) ;
64
counts
--
;
65
result.add(line) ;
66
findFirst
=
true
;
67
String[] temp
=
line.split(
"
\|
"
) ;
68
next.append(temp[
4
]).append(
"
|
"
).append(temp[
5
]) ;
69
}
70
}
else
if
(findFirst)
{
71
if
(line.contains(next.toString()))
{
72
System.out.println(next) ;
73
result.add(line) ;
74
counts
--
;
75
}
76
}
77
if
(counts
<
0
)
{
78
over
=
true
;
79
break
;
80
}
81
}
82
br.close() ;
83
if
(over)
{
84
break
;
85
}
86
}
87
end
=
new
Date();
88
this
.setDiff(end.getTime()
-
begin.getTime()) ;
89
}
catch
(Exception e)
{
90
System.out.println(
"
error
"
) ;
91
}
92
return
result ;
93
}
94
95
96
private
String getStrRex(
int
type,String number,String hour,String minute,String second)
{
97
if
(
0
<
minute.length()
&&
minute.length()
<
2
)
{
98
minute
=
"
0
"
+
minute ;
99
}
else
if
(
0
<
second.length()
&&
second.length()
<
2
)
{
100
second
=
"
0
"
+
second ;
101
}
102
StringBuffer temp
=
new
StringBuffer() ;
103
if
(type
==
1
)
{
//
查詢 系統(tǒng) 打出的電話
104
String outnumber
=
"
CD
"
+
number ;
105
temp.append(
"
^\[
"
).append(day).append(
"
\-
"
).append(month).append(
"
\-
"
).append(year)
106
.append(
"
"
).append(hour)
107
.append(
!
(minute.trim()
==
null
||
""
.equals(minute.trim()))
?
(
"
\:
"
+
minute):
""
)
108
.append(
!
(second.trim()
==
null
||
""
.equals(second.trim()))
?
(
"
\:
"
+
second):
""
)
109
.append(
"
.*\<\-.*IAM.*
"
).append(outnumber).append(
"
.*
"
) ;
110
System.out.println(temp.toString()) ;
111
}
else
if
(type
==
2
)
{
//
查詢 用戶打進(jìn)的 電話
112
String innumber
=
"
CG
"
+
number ;
113
temp.append(
"
^\[
"
).append(day).append(
"
\-
"
).append(month).append(
"
\-
"
).append(year)
114
.append(
"
"
).append(hour)
115
.append(
!
(minute.trim()
==
null
||
""
.equals(minute.trim()))
?
(
"
\:
"
+
minute):
""
)
116
.append(
!
(second.trim()
==
null
||
""
.equals(second.trim()))
?
(
"
\:
"
+
second):
""
)
117
.append(
"
.*\-\>.*IAM.*
"
).append(innumber).append(
"
.*
"
) ;
118
}
119
return
temp.toString() ;
120
}
121
122
public
void
loadFile(String path)
{
123
this
.file
=
new
File(path) ;
124
this
.setLength(file.length()) ;
125
int
pathLength
=
path.length() ;
126
int
start
=
pathLength
-
new
String(
"
xxxxxxxx_xxx.xxx
"
).length() ;
127
this
.setYear(path.substring(start,start
+
4
)) ;
128
this
.setMonth(path.substring(start
+
4
,start
+
6
)) ;
129
this
.setDay(path.substring(start
+
6
,start
+
8
)) ;
130
}
131
132
public
void
setYear(String year)
{
133
this
.year
=
year ;
134
}
135
public
void
setMonth(String month)
{
136
this
.month
=
month ;
137
}
138
public
void
setDay(String day)
{
139
this
.day
=
day ;
140
}
141
private
void
setLength(
long
length)
{
142
this
.length
=
length ;
143
}
144
public
void
setDiff(
long
diff)
{
145
this
.diff
=
diff ;
146
}
147
public
long
getDiff()
{
148
return
this
.diff ;
149
}
150
public
long
getLength()
{
151
return
this
.length ;
152
}
153
public
String getYear()
{
154
return
this
.year ;
155
}
156
public
String getMonth()
{
157
return
this
.month ;
158
}
159
public
String getDay()
{
160
return
this
.day ;
161
}
162
public
String toString()
{
163
return
this
.year
+
"
-
"
+
this
.month
+
"
-
"
+
this
.day ;
164
}
165
166
}
posted on 2008-11-02 14:51
小蟲旺福
閱讀(961)
評(píng)論(0)
編輯
收藏
所屬分類:
javaEE
新用戶注冊(cè)
刷新評(píng)論列表
只有注冊(cè)用戶
登錄
后才能發(fā)表評(píng)論。
網(wǎng)站導(dǎo)航:
博客園
IT新聞
Chat2DB
C++博客
博問(wèn)
管理
相關(guān)文章:
XFire生火指南(下)轉(zhuǎn)
XFire 最新生火指南(上)轉(zhuǎn)
XFire與Spring結(jié)合的幾種方式
使用XFire插件創(chuàng)建獨(dú)立的Webservice
OGNL —— 數(shù)據(jù)運(yùn)轉(zhuǎn)的催化劑
在Java web服務(wù)器內(nèi)使用url rewrite(鏈接)
使用Digester,對(duì)象和xml進(jìn)行轉(zhuǎn)換
利用cucurrent 的Callable 改進(jìn)方法調(diào)用性能
JAVA操作大型文本文件
Powered by:
BlogJava
Copyright © 小蟲旺福
主站蜘蛛池模板:
岳池县
|
宁德市
|
南溪县
|
和田市
|
家居
|
封开县
|
乾安县
|
聂拉木县
|
重庆市
|
绥宁县
|
定西市
|
清徐县
|
福州市
|
聊城市
|
嘉祥县
|
汶川县
|
靖远县
|
丹巴县
|
五莲县
|
东辽县
|
巩义市
|
临海市
|
宁武县
|
宽甸
|
乐陵市
|
高邑县
|
隆回县
|
区。
|
江门市
|
灵石县
|
资溪县
|
金秀
|
崇文区
|
莱西市
|
辰溪县
|
宁武县
|
丰县
|
双牌县
|
信阳市
|
德阳市
|
陕西省
|