zoukankan      html  css  js  c++  java
  • Netty-Channel的读和写

    public class NIOFileChannel01 {
    
        public static void main(String[] args) throws Exception {
            String str = "hello,帅锅";
            //创建一个输出流->channel
            FileOutputStream fileOutputStream = new FileOutputStream("d:\file01.txt");
    
            //通过 fileOutputStream 获取 对应的 FileChannel
            //这个 fileChannel 真实 类型是  FileChannelImpl
            FileChannel fileChannel = fileOutputStream.getChannel();
    
            //创建一个缓冲区 ByteBuffer
            ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
    
            //将 str 放入 byteBuffer
            byteBuffer.put(str.getBytes());
    
    
            //对byteBuffer 进行flip
            byteBuffer.flip();
    
            //将byteBuffer 数据写入到 fileChannel
            fileChannel.write(byteBuffer);
            fileOutputStream.close();
        }
    }
    public class NIOFileChannel02 {
        public static void main(String[] args) throws Exception {
    
            //创建文件的输入流
            File file = new File("d:\file01.txt");
            FileInputStream fileInputStream = new FileInputStream(file);
    
            //通过fileInputStream 获取对应的FileChannel -> 实际类型  FileChannelImpl
            FileChannel fileChannel = fileInputStream.getChannel();
    
            //创建缓冲区
            ByteBuffer byteBuffer = ByteBuffer.allocate((int) file.length());
    
            //将 通道的数据读入到Buffer
            fileChannel.read(byteBuffer);
    
            //将byteBuffer 的 字节数据 转成String
            System.out.println(new String(byteBuffer.array()));
            fileInputStream.close();
    
        }
    }

  • 相关阅读:
    微信小程序传值
    tp查询中2个表格中字段,比较大小
    isNaN与parseInt/parseFloat
    编程技巧之表格驱动编程
    RGB
    矩形重叠检测。
    经验搜索排名---google已经做过类似的了(我想多了)
    有关编程语言的认识
    Nodepad++ 资料整理
    lower()
  • 原文地址:https://www.cnblogs.com/liuyi13535496566/p/14396718.html
Copyright © 2011-2022 走看看