今天同事碰到了一個問題,從游戲服務器下載下來的輸出log有一個多G大。用記事本打不開,EditPlus也打不開,都提示文件太大。用word也打不開,提示文件大於512M。打不開怎么查找錯誤啊。於是他問我解決辦法。我想了想,決定寫一個簡單的程序讀取這個log,把這個log切分成一些小的可以用Editplus打開的文本。正好前段時間看了一些NIO的東西,所以決定用NIO來寫。沒想到,10幾行代碼就搞定了。下面附上源代碼:
ReadLargeTextWithNIO.java
1
package com.nio.entrace;
2
3
import java.io.FileInputStream;
4
import java.io.FileOutputStream;
5
import java.io.IOException;
6
import java.nio.ByteBuffer;
7
import java.nio.channels.FileChannel;
8
9
/**
10
*
11
* 用NIO讀取大文本(1G以上)
12
*
13
* @author landon
14
*
15
*/
16
public class ReadLargeTextWithNIO
17
{
18
public static void main(String
args) throws IOException
19
{
20
FileInputStream fin = new FileInputStream("d:\\temp\\outlineA1.log");
21
FileChannel fcin = fin.getChannel();
22
23
ByteBuffer buffer = ByteBuffer.allocate(1024 * 1024 * 50);
24
25
while(true)
26
{
27
buffer.clear();
28
29
int flag = fcin.read(buffer);
30
31
if(flag == -1)
32
{
33
break;
34
}
35
36
buffer.flip();
37
38
FileOutputStream fout = new FileOutputStream("d:\\temp\\" + Math.random() + ".log");
39
FileChannel fcout = fout.getChannel();
40
41
fcout.write(buffer);
42
}
43
}
44
}
45
46
package com.nio.entrace;
2
3
import java.io.FileInputStream;
4
import java.io.FileOutputStream;
5
import java.io.IOException;
6
import java.nio.ByteBuffer;
7
import java.nio.channels.FileChannel;
8
9
/**10
* 11
* 用NIO讀取大文本(1G以上)12
* 13
* @author landon14
*15
*/16
public class ReadLargeTextWithNIO 17
{18
public static void main(String
args) throws IOException19
{20
FileInputStream fin = new FileInputStream("d:\\temp\\outlineA1.log");21
FileChannel fcin = fin.getChannel();22
23
ByteBuffer buffer = ByteBuffer.allocate(1024 * 1024 * 50);24
25
while(true)26
{27
buffer.clear();28
29
int flag = fcin.read(buffer);30
31
if(flag == -1)32
{33
break;34
}35
36
buffer.flip();37
38
FileOutputStream fout = new FileOutputStream("d:\\temp\\" + Math.random() + ".log");39
FileChannel fcout = fout.getChannel();40
41
fcout.write(buffer);42
}43
}44
}45

46
下面簡單說幾個注意的地方:
a.因為要把超大文本切分成小的部分,所以分配buffer的時候盡量大一些,這里我分配的大小是50M,不過如果太大了,可能會報內存溢出。
b.說一下clear和flip的方法,直接上源碼:
1
public final Buffer clear()
2
{
3
position = 0;
4
limit = capacity;
5
mark = -1;
6
return this;
7
}
8
9
public final Buffer flip()
10
{
11
limit = position;
12
position = 0;
13
mark = -1;
14
return this;
15
}
public final Buffer clear()
2
{3
position = 0;4
limit = capacity;5
mark = -1;6
return this;7
}8

9
public final Buffer flip()10
{11
limit = position;12
position = 0;13
mark = -1;14
return this;15
}
一看便知二者的區別。
c.跳出循環也即讀完的判斷是read返回的flag是-1
利用NIO確實方便,以后繼續研究->NIO網絡編程
