NIO 按行处理大文件

 1 import java.io.BufferedReader; 
 2 import java.io.File; 
 3 import java.io.FileInputStream; 
 4 import java.io.IOException; 
 5 import java.io.RandomAccessFile; 
 6 import java.nio.ByteBuffer; 
 7 import java.nio.channels.FileChannel; 
 8 
 9 public class TestNio { 
10 
11 
12     public static void main(String args[]) throws Exception{ 
13 
14     //String infile = "D:\\workspace\\test\\usagetracking.log"; 
15     //FileInputStream fin= new FileInputStream(infile); 
16     //FileChannel fcin = fin.getChannel(); 
17 
18     int bufSize = 100; 
19     File fin = new File("D:\\workspace\\test\\usagetracking.log"); 
20     File fout = new File("D:\\workspace\\test\\usagetracking2.log"); 
21 
22     FileChannel fcin = new RandomAccessFile(fin, "r").getChannel(); 
23     ByteBuffer rBuffer = ByteBuffer.allocate(bufSize); 
24 
25     FileChannel fcout = new RandomAccessFile(fout, "rws").getChannel(); 
26     ByteBuffer wBuffer = ByteBuffer.allocateDirect(bufSize); 
27 
28 
29     readFileByLine(bufSize, fcin, rBuffer, fcout, wBuffer); 
30 
31     System.out.print("OK!!!"); 
32     } 
33 
34     public static void readFileByLine(int bufSize, FileChannel fcin, ByteBuffer rBuffer, FileChannel fcout, ByteBuffer wBuffer){ 
35         String enterStr = "\n"; 
36         try{ 
37         byte[] bs = new byte[bufSize]; 
38 
39         int size = 0; 
40         StringBuffer strBuf = new StringBuffer(""); 
41         //while((size = fcin.read(buffer)) != -1){ 
42         while(fcin.read(rBuffer) != -1){ 
43               int rSize = rBuffer.position(); 
44               rBuffer.rewind(); 
45               rBuffer.get(bs); 
46               rBuffer.clear(); 
47               String tempString = new String(bs, 0, rSize); 
48               //System.out.print(tempString); 
49               //System.out.print("<200>"); 
50 
51               int fromIndex = 0; 
52               int endIndex = 0; 
53               while((endIndex = tempString.indexOf(enterStr, fromIndex)) != -1){ 
54                String line = tempString.substring(fromIndex, endIndex); 
55                line = new String(strBuf.toString() + line); 
56                //System.out.print(line); 
57                //System.out.print("</over/>"); 
58                //write to anthone file 
59                writeFileByLine(fcout, wBuffer, line); 
60 
61                
62                strBuf.delete(0, strBuf.length()); 
63                fromIndex = endIndex + 1; 
64               } 
65               if(rSize > tempString.length()){ 
66               strBuf.append(tempString.substring(fromIndex, tempString.length())); 
67               }else{ 
68               strBuf.append(tempString.substring(fromIndex, rSize)); 
69               } 
70         } 
71         } catch (IOException e) { 
72         // TODO Auto-generated catch block 
73         e.printStackTrace(); 
74         } 
75     } 
76 
77     public static void writeFileByLine(FileChannel fcout, ByteBuffer wBuffer, String line){ 
78         try { 
79             //write on file head 
80             //fcout.write(wBuffer.wrap(line.getBytes())); 
81             //wirte append file on foot 
82             fcout.write(wBuffer.wrap(line.getBytes()), fcout.size()); 
83 
84         } catch (IOException e) { 
85             // TODO Auto-generated catch block 
86             e.printStackTrace(); 
87         } 
88     } 
89 
90 } 
posted @ 2012-08-01 14:01  绒花雪冷  阅读(444)  评论(0编辑  收藏  举报