Great Deng的博客 Great Deng的博客
首页
  • 代码片段
  • 学习笔记

    • JavaScript笔记
  • MQ

    • 常见面试题
  • VuePress
更多
关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Great Deng

有志者事竟成
首页
  • 代码片段
  • 学习笔记

    • JavaScript笔记
  • MQ

    • 常见面试题
  • VuePress
更多
关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 代码片段

    • IO
    • java
    • 代码片段
    Great Deng
    2021-10-27
    目录

    IO

    # NIO-Buffer&Chanel的使用

    public class CopyFile {  
        public static void main(String[] args) throws Exception {  
            String infile = "C:\\copy.sql";  
            String outfile = "C:\\copy.txt";  
            // 获取源文件和目标文件的输入输出流  
            FileInputStream fin = new FileInputStream(infile);  
            FileOutputStream fout = new FileOutputStream(outfile);  
            // 获取输入输出通道  
            FileChannel fcin = fin.getChannel();  
            FileChannel fcout = fout.getChannel();  
            // 创建缓冲区  
            ByteBuffer buffer = ByteBuffer.allocate(1024);  
            while (true) {  
                // clear方法重设缓冲区,使它可以接受读入的数据  
                buffer.clear();  
                // 从输入通道中将数据读到缓冲区  
                int r = fcin.read(buffer);  
                // read方法返回读取的字节数,可能为零,如果该通道已到达流的末尾,则返回-1  
                if (r == -1) {  
                    break;  
                }  
                // flip方法让缓冲区可以将新读入的数据写入另一个通道  
                buffer.flip();  
                // 从输出通道中将数据写入缓冲区  
                fcout.write(buffer);  
            }  
        }  
    }  
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    编辑 (opens new window)
    #io
    上次更新: 2026/02/06, 03:37:18
    最近更新
    01
    推荐书籍
    02-06
    02
    推荐书籍
    02-06
    03
    常见面试题
    02-04
    更多文章>
    Theme by Vdoing | Copyright © 2026-2026 Great Deng
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式