博客
关于我
利用字节流拷贝文件
阅读量:680 次
发布时间:2019-03-16

本文共 1544 字,大约阅读时间需要 5 分钟。

    public class FileCopy {        public static void main(String[] args) throws IOException {            // 输入流            FileInputStream fileInputStream = new FileInputStream(new File("D:\\abc.txt"));            // 输出流            FileOutputStream fileOutputStream = new FileOutputStream("D:\\abc(字节方式).txt");            // 缓冲区(每次读10字节)            byte[] bytes = new byte[10];            // 写到文件中            int length;            while ((length = fileInputStream.read(bytes)) != -1) {                fileOutputStream.write(bytes, 0, length);            }            // 关闭流            fileInputStream.close();            fileOutputStream.close();        }    

文件字节复制示例

以下代码示例演示了如何使用Java进行文件字节复制操作。该方法通过读取输入流并写入输出流的方式实现文件内容的复制。

    public class FileCopy {        public static void main(String[] args) throws IOException {            // 创建输入流和输出流            FileInputStream inputStream = new FileInputStream("D:\\abc.txt");            FileOutputStream outputStream = new FileOutputStream("D:\\abc(字节方式).txt");            // 创建缓冲区            byte[] buffer = new byte[10];            int readLength;            while ((readLength = inputStream.read(buffer)) != -1) {                outputStream.write(buffer, 0, readLength);            }            // 关闭流            inputStream.close();            outputStream.close();        }    

代码解释: - 首先创建了一个字节输入流(FileInputStream)和一个字节输出流(FileOutputStream)。 - 使用了10字节的缓冲区来读取输入流数据。 - 在循环中读取输入流的内容,并将读取到的字节写入输出流。 - 当输入流结束时,循环结束并关闭两个流。

转载地址:http://abrqz.baihongyu.com/

你可能感兴趣的文章
np.power的使用
查看>>
NPM 2FA双重认证的设置方法
查看>>
npm build报错Cannot find module ‘webpack/lib/rules/BasicEffectRulePlugin‘解决方法
查看>>
npm build报错Cannot find module ‘webpack‘解决方法
查看>>
npm ERR! ERESOLVE could not resolve报错
查看>>
npm ERR! fatal: unable to connect to github.com:
查看>>
npm ERR! Unexpected end of JSON input while parsing near '...on":"0.10.3","direc to'
查看>>
npm ERR! Unexpected end of JSON input while parsing near ‘...“:“^1.2.0“,“vue-html-‘ npm ERR! A comp
查看>>
npm error Missing script: “server“npm errornpm error Did you mean this?npm error npm run serve
查看>>
npm error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装
查看>>
npm install CERT_HAS_EXPIRED解决方法
查看>>
npm install digital envelope routines::unsupported解决方法
查看>>
npm install 卡着不动的解决方法
查看>>
npm install 报错 EEXIST File exists 的解决方法
查看>>
npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
查看>>
npm install 报错 Failed to connect to github.com port 443 的解决方法
查看>>
npm install 报错 fatal: unable to connect to github.com 的解决方法
查看>>
npm install 报错 no such file or directory 的解决方法
查看>>
npm install 权限问题
查看>>
npm install报错,证书验证失败unable to get local issuer certificate
查看>>