A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

四种方式分别是
1:基本字节流一次读取一个字节;
2:基本字节流一次读取一个字节数组;:
3:高效字节流一次读取一个字节;
4:高效字节流一次读取一个字节数组;
实验证明第四种方式效率最高。
  1. <p>
  2. /*
  3. * 将 F:\\Visual_C++网络编程.pdf  复制到当前项目目录下,
  4. * 要求使用字节流的四种方式
  5. */
  6. public class Demo3 {
  7. public static void main(String[] args) throws IOException {
  8.   long start = System.currentTimeMillis();
  9.   // 共耗时192236毫秒
  10.   // method1("F:\\Visual_C++网络编程.pdf", "1.pdf");
  11.   // 共耗时346毫秒
  12.   method2("F:\\Visual_C++网络编程.pdf", "2.pdf");
  13.   // 共耗时1211毫秒
  14.   method3("F:\\Visual_C++网络编程.pdf", "3.pdf");
  15.   // 共耗时51毫秒
  16.   method4("F:\\Visual_C++网络编程.pdf", "4.pdf");</p><p>  long end = System.currentTimeMillis();
  17.   System.out.println("共耗时" + (end - start) + "毫秒");
  18. }</p><p> private static void method4(String src, String dest) throws IOException {
  19.   BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
  20.     src));
  21.   BufferedOutputStream bos = new BufferedOutputStream(
  22.     new FileOutputStream(dest));
  23.   byte[] bys = new byte[1024];
  24.   int len = 0;
  25.   while ((len = bis.read(bys)) != -1) {
  26.    bos.write(bys, 0, len);
  27.   }
  28.   bis.close();
  29.   bos.close();
  30. }</p><p> private static void method3(String src, String dest) throws IOException {
  31.   BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
  32.     src));
  33.   BufferedOutputStream bos = new BufferedOutputStream(
  34.     new FileOutputStream(dest));
  35.   int by = 0;
  36.   while ((by = bis.read()) != -1) {
  37.    bos.write(by);
  38.   }
  39.   bis.close();
  40.   bos.close();
  41. }</p><p> private static void method2(String src, String dest) throws IOException {
  42.   FileInputStream fis = new FileInputStream(src);
  43.   FileOutputStream fos = new FileOutputStream(dest);
  44.   byte[] bys = new byte[1024];
  45.   int len = 0;
  46.   while ((len = fis.read(bys)) != -1) {
  47.    fos.write(bys, 0, len);
  48.   }
  49.   fis.close();
  50.   fos.close();
  51. }</p><p> // private static void method1(String src, String dest) throws IOException {
  52. // FileInputStream fis = new FileInputStream(src);
  53. // FileOutputStream fos = new FileOutputStream(dest);
  54. // int by = 0;
  55. // while ((by = fis.read()) != -1) {
  56. // fos.write(by);
  57. // }
  58. // fis.close();
  59. // fos.close();
  60. // }
  61. }

  62. </p><p> </p>
复制代码

1 个回复

倒序浏览
来学习下
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马