码迷,mamicode.com
首页 > 编程语言 > 详细

java 字节流文件复制方法总结

时间:2019-02-21 09:45:21      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:字节   buffer   缓冲流   str   byte   ring   tput   color   []   

1、使用字节流每次读写单个字节

 1 public static void main(String[] args) throws IOException {
 2         FileInputStream fis = new FileInputStream("C:\\CloudMusic\\1.mp3");
 3         FileOutputStream fos = new FileOutputStream("e:\\1.mp3");
 4         int len = 0;
 5         while ((len = fis.read()) != -1) {
 6             fos.write(len);
 7         }
 8         if (fos != null) {
 9             fos.close();
10         }
11         if (fis != null) {
12             fis.close();
13         }
14     }

2、使用字节流每次读写多个字节

 1 public static void main(String[] args) throws IOException {
 2         FileInputStream fis = new FileInputStream("C:\\CloudMusic\\1.mp3");
 3         FileOutputStream fos = new FileOutputStream("e:\\1.mp3");
 4         byte[] b = new byte[1024];
 5         int len = 0;
 6         while ((len = fis.read(b)) != -1) {
 7             fos.write(b,0,len);
 8         }
 9         if (fos != null) {
10             fos.close();
11         }
12         if (fis != null) {
13             fis.close();
14         }
15     }

3、使用字节缓冲流每次读写单个字节

 1 public static void main(String[] args) throws IOException {
 2         BufferedInputStream bis = new BufferedInputStream(new FileInputStream("C:\\CloudMusic\\1.mp3"));
 3         BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("e:\\1.mp3"));
 4         int len = 0;
 5         while ((len = bis.read()) != -1) {
 6             bos.write(len);
 7         }
 8         if (bos != null) {
 9             bos.close();
10         }
11         if (bis != null) {
12             bis.close();
13         }
14     }

4、使用字节缓冲流每次读写多个字节

 1 public static void main(String[] args) throws IOException {
 2         BufferedInputStream bis = new BufferedInputStream(new FileInputStream("C:\\CloudMusic\\1.mp3"));
 3         BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("e:\\1.mp3"));
 4         byte[] b = new byte[1024];
 5         int len = 0;
 6         while ((len = bis.read(b)) != -1) {
 7             bos.write(b, 0, len);
 8         }
 9         if (bos != null) {
10             bos.close();
11         }
12         if (bis != null) {
13             bis.close();
14         }
15     }

 

java 字节流文件复制方法总结

标签:字节   buffer   缓冲流   str   byte   ring   tput   color   []   

原文地址:https://www.cnblogs.com/voidchar/p/10410278.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!