尚硅谷2019年Netty教程 FileChanle 写 读 示例 ----目标netty---step2.01

FileOutputStream
ByteBuffer
FileChannel

// 01 写 ///

https://www.bilibili.com/video/BV1jK4y1s7GV?p=13

在这里插入图片描述

在这里插入图片描述


package com.atguigu.nio;



import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * Author: tz_wl
 * Date: 2020/9/20 9:50
 * Content:
 */
public class NioFileChannel01 {
    public static void main(String[] args) {

        try {

            //step1  新建一个通道  指向本地文件  d:/111.txt
            FileOutputStream fileOutputStream = new FileOutputStream("D:\\tmp\\netty\\1111.txt");
            FileChannel fileChannel = fileOutputStream.getChannel();

            //step2  新建一个 byteByffer 将文字赛道byteBuffer里面
            String strInput="strInput ttttt";
            ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
            byteBuffer.put(strInput.getBytes());

            //step3  将byteBuffer 写入到  channel
            byteBuffer.flip();  //flip 前是put  flip后是get
            fileChannel.write(byteBuffer);


            //step4 扫尾工作 
            fileOutputStream.close();


        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}



// 02 读 ///

https://www.bilibili.com/video/BV1jK4y1s7GV?p=14

在这里插入图片描述

package com.atguigu.nio;

import com.sun.xml.internal.ws.encoding.MtomCodec;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * Author: tz_wl
 * Date: 2020/9/20 10:48
 * Content:
 */
public class NioFileChannel02Read {
    public static void main(String[] args) {
        try {

            File file = new File("D:\\tmp\\netty\\1111.txt");  //111.txt 有文字 2222
            FileInputStream fileInputStream = new FileInputStream(file);

            FileChannel channel = fileInputStream.getChannel();

            ByteBuffer byteBuffer = ByteBuffer.allocate((int)file.length());

            channel.read(byteBuffer);

            System.out.println(new String(byteBuffer.array()));
            //2222222222222222222

            fileInputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

// 023 读写 ///

https://www.bilibili.com/video/BV1jK4y1s7GV?p=15

在这里插入图片描述


package com.atguigu.nio;

import com.sun.jdi.PathSearchingVirtualMachine;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * Author: tz_wl
 * Date: 2020/9/20 11:14
 * Content:
 */
public class NioFileChannel03ReadWrite {

    public static void main(String[] args) throws FileNotFoundException,IOException {

        File fileInput = new File("D:\\tmp\\netty\\input01.txt");  //111.txt 有文字 2222
        FileInputStream fileInputStream = new FileInputStream(fileInput);
        FileChannel channel = fileInputStream.getChannel();


        File fileOutput = new File("D:\\tmp\\netty\\output01.txt");  //111.txt 有文字 2222
        FileOutputStream fileOutputStream = new FileOutputStream(fileOutput);
        FileChannel channel2 = fileOutputStream.getChannel();


        ByteBuffer byteBuffer = ByteBuffer.allocate(10);
        while(true){
            //注意每次读要清空一下  byteBuffer ,不然要一直读的,可以注释下面内容测试
            byteBuffer.clear();

            //step1  读channel 数据到 byteBuffer
            int count = channel.read(byteBuffer);
            System.out.println("count is "+ count);
            if(count==-1){
                break;
            }
            //step2  反转
            byteBuffer.flip();
            // int limit = byteBuffer.limit();

            //step3  写byteBuffer 数据到 channel
            channel2.write(byteBuffer);

        }




        fileInputStream.close();
        fileOutputStream.close();
    }


}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值