<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    The NoteBook of EricKong

      BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
      611 Posts :: 1 Stories :: 190 Comments :: 0 Trackbacks
    import java.io.BufferedReader;
     
    import java.io.File;
     
    import java.io.FileInputStream;
     
    import java.io.IOException;
     
    import java.io.RandomAccessFile;
     
    import java.nio.ByteBuffer;
     
    import java.nio.channels.FileChannel;
     
     
     
    public class TestNio {
     
     
     
     
     
        
    public static void main(String args[]) throws Exception{
     
     
     
        
    //String infile = "D:\\workspace\\test\\usagetracking.log";
     
        
    //FileInputStream fin= new FileInputStream(infile);
     
        
    //FileChannel fcin = fin.getChannel();
     
     
     
        
    int bufSize = 100;
     
        File fin 
    = new File("D:\\workspace\\test\\usagetracking.log");
     
        File fout 
    = new File("D:\\workspace\\test\\usagetracking2.log");
     
     
     
        FileChannel fcin 
    = new RandomAccessFile(fin, "r").getChannel();
     
        ByteBuffer rBuffer 
    = ByteBuffer.allocate(bufSize);
     
     
     
        FileChannel fcout 
    = new RandomAccessFile(fout, "rws").getChannel();
     
        ByteBuffer wBuffer 
    = ByteBuffer.allocateDirect(bufSize);
     
     
     
     
     
        readFileByLine(bufSize, fcin, rBuffer, fcout, wBuffer);
     
     
     
        System.out.print(
    "OK!!!");
     
        }
     
     
     
        
    public static void readFileByLine(int bufSize, FileChannel fcin, ByteBuffer rBuffer, FileChannel fcout, ByteBuffer wBuffer){
     
            String enterStr 
    = "\n";
     
            
    try{
     
            
    byte[] bs = new byte[bufSize];
     
     
     
            
    int size = 0;
     
            StringBuffer strBuf 
    = new StringBuffer("");
     
            
    //while((size = fcin.read(buffer)) != -1){
     
            
    while(fcin.read(rBuffer) != -1){
     
                  
    int rSize = rBuffer.position();
     
                  rBuffer.rewind();
     
                  rBuffer.get(bs);
     
                  rBuffer.clear();
     
                  String tempString 
    = new String(bs, 0, rSize);
     
                  
    //System.out.print(tempString);
     
                  
    //System.out.print("<200>");
     
     
     
                  
    int fromIndex = 0;
     
                  
    int endIndex = 0;
     
                  
    while((endIndex = tempString.indexOf(enterStr, fromIndex)) != -1){
     
                   String line 
    = tempString.substring(fromIndex, endIndex);
     
                   line 
    = new String(strBuf.toString() + line);
     
                   
    //System.out.print(line);
     
                   
    //System.out.print("</over/>");
     
                   
    //write to anthone file
     
                   writeFileByLine(fcout, wBuffer, line);
     
     
     
                    
     
                   strBuf.delete(
    0, strBuf.length());
     
                   fromIndex 
    = endIndex + 1;
     
                  }
     
                  
    if(rSize > tempString.length()){
     
                  strBuf.append(tempString.substring(fromIndex, tempString.length()));
     
                  }
    else{
     
                  strBuf.append(tempString.substring(fromIndex, rSize));
     
                  }
     
            }
     
            } 
    catch (IOException e) {
     
            
    // TODO Auto-generated catch block
     
            e.printStackTrace();
     
            }
     
        }
     
     
     
        
    public static void writeFileByLine(FileChannel fcout, ByteBuffer wBuffer, String line){
     
            
    try {
     
                
    //write on file head
     
                
    //fcout.write(wBuffer.wrap(line.getBytes()));
     
                
    //wirte append file on foot
     
                fcout.write(wBuffer.wrap(line.getBytes()), fcout.size());
     
     
     
            } 
    catch (IOException e) {
     
                
    // TODO Auto-generated catch block
     
                e.printStackTrace();
     
            }
     
        }
     
     
     
    }
     
    posted on 2013-12-17 15:14 Eric_jiang 閱讀(1796) 評論(0)  編輯  收藏 所屬分類: Java
    主站蜘蛛池模板: 亚洲日韩乱码中文字幕| 在线a亚洲v天堂网2018| 亚洲大片在线观看| 永久免费av无码入口国语片| 亚洲熟妇无码AV在线播放 | 亚洲欧美日韩国产成人| 久久受www免费人成_看片中文| 亚洲理论在线观看| 可以免费看的卡一卡二| 亚洲精品无码专区在线| 四虎影库久免费视频| eeuss草民免费| 久久精品国产96精品亚洲 | 野花高清在线电影观看免费视频| 亚洲人成电影网站久久| 日韩精品免费电影| 一区二区三区免费看| 亚洲国产精品无码久久久秋霞2| 9277手机在线视频观看免费| 亚洲性线免费观看视频成熟| 凹凸精品视频分类国产品免费| 51午夜精品免费视频| 91精品国产亚洲爽啪在线观看| 无码国产精品一区二区免费式直播 | 成人性生活免费视频| 免费夜色污私人影院网站电影| 亚洲日产无码中文字幕| 国产免费的野战视频| 精品亚洲av无码一区二区柚蜜| 亚洲精品午夜国产VA久久成人 | 德国女人一级毛片免费| 亚洲免费在线观看| 亚洲高清视频免费| 国产极品粉嫩泬免费观看| 大地资源网高清在线观看免费 | 一个人晚上在线观看的免费视频| 亚洲成色999久久网站| 好爽好紧好大的免费视频国产| GOGOGO免费观看国语| 亚洲无吗在线视频| 亚洲男同帅GAY片在线观看|