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

2

3

4

5

6

7

8

9

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

下面簡單說幾個注意的地方:
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
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

一看便知二者的區別。
利用NIO確實方便,以后繼續研究->NIO網絡編程