JAVA NIO 操作文件

import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;

import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.nio.channels.FileChannel.MapMode;

public class Demo {


    @Test
    public void test(){

        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

        byteBuffer.put("abcde".getBytes());

        System.out.println(byteBuffer.position());

        System.out.println(new String(byteBuffer.array(),0,byteBuffer.position()));


    }

    /**
     *
     * 利用通道文件复制
     * 非直接缓冲区
     */
    @Test
    public void test1() {

        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;

        FileChannel inputChannel = null;
        FileChannel outputChannel = null;
        try {
                fileInputStream = new FileInputStream("1.txt");
                fileOutputStream = new FileOutputStream("2.txt");

                inputChannel = fileInputStream.getChannel();
                outputChannel = fileOutputStream.getChannel();

                ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

                while (inputChannel.read(byteBuffer) != -1) {
                    byteBuffer.flip();
                    System.out.println(new String(byteBuffer.array(), 0, byteBuffer.limit()));
                    outputChannel.write(byteBuffer);
                    byteBuffer.clear();
                }

        }catch (Exception e){

        }
        finally {
            try{
                if (outputChannel != null){
                    outputChannel.close();
                }
            }
            catch (Exception e){}
            try{
                if (inputChannel != null){
                    inputChannel.close();
                }
            }
            catch (Exception e){}try{
                if (fileOutputStream != null){
                    fileOutputStream.close();
                }
            }
            catch (Exception e){}try{
                if (fileInputStream != null){
                    fileInputStream.close();
                }
            }
            catch (Exception e){}


        }
    }

    /**
     *
     *利用直接缓冲区完成文件的复制(内存映射文件)
     */
    @Test

    public void test2() throws IOException {
        FileChannel inputChannel = FileChannel.open(Paths.get("1.txt"), StandardOpenOption.READ);
        FileChannel outputChannel = FileChannel.open(Paths.get("2.txt"), StandardOpenOption.WRITE,StandardOpenOption.READ,StandardOpenOption.CREATE);


        MappedByteBuffer inputMapperByteBuffer = inputChannel.map(MapMode.READ_ONLY,0,inputChannel.size());
        MappedByteBuffer outputMapperByteBuffer = outputChannel.map(MapMode.READ_WRITE,0,inputChannel.size());

        byte[] bytes = new byte[inputMapperByteBuffer.limit()];

        inputMapperByteBuffer.get(bytes);

        outputMapperByteBuffer.put(bytes);

        outputChannel.close();
        inputChannel.close();
        outputChannel.close();
        inputChannel.close();
    }


    /**
     *
     * 直接通过通道传输数据,也为直接缓冲区传输
     * @throws IOException
     */
    @Test
    public void test3() throws IOException {
        FileChannel inputChannel = FileChannel.open(Paths.get("1.txt"), StandardOpenOption.READ);
        FileChannel outputChannel = FileChannel.open(Paths.get("2.txt"), StandardOpenOption.WRITE,StandardOpenOption.READ,StandardOpenOption.CREATE);

        inputChannel.transferTo(0,inputChannel.size(),outputChannel);

    }
}

你可能感兴趣的:(java)