Java新IO】_通道(Channel)\代碼

來源:互聯網
上載者:User

標籤:app   exception   []   getc   foo   static   leo   length   cep   

//******

 

import java.nio.ByteBuffer ;
import java.nio.channels.FileChannel ;
import java.io.File ;
import java.io.FileOutputStream ;
public class FileChannelDemo01{
    public static void main(String args[]) throws Exception{
        String info[] = {"MLDN","MLDNJAVA","www.mldn.cn","www.mldnjava.cn","李興華","lixinghua"} ;
        File file = new File("." + File.separator + "out.txt") ;
        FileOutputStream output = null ;
        output = new FileOutputStream(file) ;
        FileChannel fout = null ;    // 聲明FileChannel對象
        fout = output.getChannel() ;    // 得到輸出的通道
        ByteBuffer buf = ByteBuffer.allocate(1024) ;
        for(int i=0;i<info.length;i++){
            buf.put(info[i].getBytes()) ;    // 字串變為位元組數組放進緩衝區之中
        }
        buf.flip() ;
        fout.write(buf) ;    // 輸出緩衝區的內容
        fout.close() ;
        output.close() ;
    }
}
//*****

 

 

import java.nio.ByteBuffer ;
import java.nio.channels.FileChannel ;
import java.io.File ;
import java.io.FileOutputStream ;
import java.io.FileInputStream ;
public class FileChannelDemo02{
    public static void main(String args[]) throws Exception{
        File file1 = new File("." + File.separator + "note.txt") ;
        File file2 = new File("." + File.separator + "outnote.txt") ;
        FileInputStream input = null ;
        FileOutputStream output = null ;
        output = new FileOutputStream(file2) ;
        input = new FileInputStream(file1) ;
        FileChannel fout = null ;    // 聲明FileChannel對象
        FileChannel fin = null ;    // 定義輸入的通道
        fout = output.getChannel() ;    // 得到輸出的通道
        fin = input.getChannel() ;    // 得到輸入的通道
        ByteBuffer buf = ByteBuffer.allocate(1024) ;
        
        int temp = 0 ;

        while((temp=fin.read(buf))!=-1){
            buf.flip() ;
            fout.write(buf) ;
            buf.clear() ;    // 清空緩衝區,所有的狀態變數的位置恢複到原點
        }
        fin.close() ;
        fout.close() ;
        input.close() ;
        output.close() ;
    }
}

//*****

import java.nio.ByteBuffer ;
import java.nio.MappedByteBuffer ;
import java.nio.channels.FileChannel ;
import java.io.File ;
import java.io.FileOutputStream ;
import java.io.FileInputStream ;
public class FileChannelDemo03{
    public static void main(String args[]) throws Exception{
        File file = new File("." + File.separator + "mldn.txt") ;  
        FileInputStream input = null ;
        input = new FileInputStream(file) ;
        FileChannel fin = null ;    // 定義輸入的通道
        fin = input.getChannel() ;    // 得到輸入的通道
        MappedByteBuffer mbb = null ;
        mbb = fin.map(FileChannel.MapMode.READ_ONLY,0,file.length()) ;
        byte data[] = new byte[(int)file.length()] ;    // 開闢空間接收內容
        int foot = 0 ;
        while(mbb.hasRemaining()){
            data[foot++] = mbb.get() ;    // 讀取資料
        }
        System.out.println(new String(data)) ;    // 輸出內容
        fin.close() ;
        input.close() ;
    }
}
 //****

import java.nio.ByteBuffer ;
import java.nio.channels.FileChannel ;
import java.io.File ;
import java.io.FileOutputStream ;
public class FileChannelDemo01{
    public static void main(String args[]) throws Exception{
        String info[] = {"MLDN","MLDNJAVA","www.mldn.cn","www.mldnjava.cn","李興華","lixinghua"} ;
        File file = new File("." + File.separator + "out.txt") ;
        FileOutputStream output = null ;
        output = new FileOutputStream(file) ;
        FileChannel fout = null ;    // 聲明FileChannel對象
        fout = output.getChannel() ;    // 得到輸出的通道
        ByteBuffer buf = ByteBuffer.allocate(1024) ;
        for(int i=0;i<info.length;i++){
            buf.put(info[i].getBytes()) ;    // 字串變為位元組數組放進緩衝區之中
        }
        buf.flip() ;
        fout.write(buf) ;    // 輸出緩衝區的內容
        fout.close() ;
        output.close() ;
    }
}
 

Java新IO】_通道(Channel)\代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.