NIO 按行处理大文件

本文介绍如何利用Java NIO技术实现文件的高效读写操作,包括使用FileChannel、ByteBuffer等类进行文件操作。
 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 } 

转载于:https://www.cnblogs.com/jishuixiansheng/archive/2012/08/01/2618189.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值