把d:\\bbb.mp4复制到当前项目目录下的copy.mp4中 分别用FileInputStream和BufferedInputStream类中的两种read()方法,共四种方式复制并比较效率。给出具体解释并分析哪个更好

时间: 2024-02-18 18:00:36 浏览: 30
好的,您的问题是如何用Java实现将d:\\bbb.mp4复制到当前项目目录下的copy.mp4中,并使用FileInputStream和BufferedInputStream类中的两种read()方法,共四种方式复制并比较效率。 下面是代码示例: ```java import java.io.*; public class FileCopyDemo { public static void main(String[] args) { String srcFileName = "d:\\bbb.mp4"; String destFileName = "copy.mp4"; File srcFile = new File(srcFileName); File destFile = new File(destFileName); // 使用FileInputStream和BufferedInputStream类中的read()方法复制文件 long start1 = System.currentTimeMillis(); copyByFileInputStream(srcFile, destFile); long end1 = System.currentTimeMillis(); System.out.println("使用FileInputStream和read()方法复制文件所需时间:" + (end1 - start1) + "ms"); long start2 = System.currentTimeMillis(); copyByBufferedInputStream(srcFile, destFile); long end2 = System.currentTimeMillis(); System.out.println("使用BufferedInputStream和read()方法复制文件所需时间:" + (end2 - start2) + "ms"); long start3 = System.currentTimeMillis(); copyByFileInputStreamWithBuffer(srcFile, destFile); long end3 = System.currentTimeMillis(); System.out.println("使用FileInputStream和read(byte[])方法复制文件所需时间:" + (end3 - start3) + "ms"); long start4 = System.currentTimeMillis(); copyByBufferedInputStreamWithBuffer(srcFile, destFile); long end4 = System.currentTimeMillis(); System.out.println("使用BufferedInputStream和read(byte[])方法复制文件所需时间:" + (end4 - start4) + "ms"); } /** * 使用FileInputStream和read()方法复制文件 * @param srcFile 源文件 * @param destFile 目标文件 */ public static void copyByFileInputStream(File srcFile, File destFile) { FileInputStream fis = null; FileOutputStream fos = null; try { fis = new FileInputStream(srcFile); fos = new FileOutputStream(destFile); int len; while ((len = fis.read()) != -1) { fos.write(len); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (fis != null) { fis.close(); } if (fos != null) { fos.close(); } } catch (IOException e) { e.printStackTrace(); } } } /** * 使用BufferedInputStream和read()方法复制文件 * @param srcFile 源文件 * @param destFile 目标文件 */ public static void copyByBufferedInputStream(File srcFile, File destFile) { BufferedInputStream bis = null; BufferedOutputStream bos = null; try { bis = new BufferedInputStream(new FileInputStream(srcFile)); bos = new BufferedOutputStream(new FileOutputStream(destFile)); int len; while ((len = bis.read()) != -1) { bos.write(len); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (bis != null) { bis.close(); } if (bos != null) { bos.close(); } } catch (IOException e) { e.printStackTrace(); } } } /** * 使用FileInputStream和read(byte[])方法复制文件 * @param srcFile 源文件 * @param destFile 目标文件 */ public static void copyByFileInputStreamWithBuffer(File srcFile, File destFile) { FileInputStream fis = null; FileOutputStream fos = null; try { fis = new FileInputStream(srcFile); fos = new FileOutputStream(destFile); byte[] buffer = new byte[1024 * 1024 * 10]; // 10MB缓冲区 int len; while ((len = fis.read(buffer)) != -1) { fos.write(buffer, 0, len); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (fis != null) { fis.close(); } if (fos != null) { fos.close(); } } catch (IOException e) { e.printStackTrace(); } } } /** * 使用BufferedInputStream和read(byte[])方法复制文件 * @param srcFile 源文件 * @param destFile 目标文件 */ public static void copyByBufferedInputStreamWithBuffer(File srcFile, File destFile) { BufferedInputStream bis = null; BufferedOutputStream bos = null; try { bis = new BufferedInputStream(new FileInputStream(srcFile)); bos = new BufferedOutputStream(new FileOutputStream(destFile)); byte[] buffer = new byte[1024 * 1024 * 10]; // 10MB缓冲区 int len; while ((len = bis.read(buffer)) != -1) { bos.write(buffer, 0, len); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (bis != null) { bis.close(); } if (bos != null) { bos.close(); } } catch (IOException e) { e.printStackTrace(); } } } } ``` 以上代码中,我们定义了四个方法分别使用FileInputStream和BufferedInputStream类中的两种read()方法,共四种方式复制文件,具体来说,分别是: - 使用FileInputStream和read()方法复制文件 - 使用BufferedInputStream和read()方法复制文件 - 使用FileInputStream和read(byte[])方法复制文件 - 使用BufferedInputStream和read(byte[])方法复制文件 其中,第三种和第四种方法在读取数据时使用了缓冲区,缓冲区大小为10MB。 我们在主方法中,分别调用这四个方法,并分别计算它们所需的时间。最终输出结果,比较这四种方式的效率。 总体来说,第三种和第四种方法比第一种和第二种方法要快,因为它们使用了缓冲区,减少了每次读取和写入数据时与磁盘的交互次数。而第四种方法又比第三种方法要快,因为它使用了BufferedInputStream,进一步提高了效率。 当然,在实际使用中,我们需要根据具体情况选择最适合的方法,例如,如果需要读取的文件较小,可以使用第一种或第二种方法;如果需要读取的文件较大,可以使用第三种或第四种方法。

相关推荐

import java.io.IOException; import java.io.FileOutputStream; import java.io.FileInputStream; import java.io.BufferedInputStream; public class fileDemo { public static void main(String[] args) throws IOException { method1(); method2(); method3(); method4(); } // 一次读取一个字节 public static void method1() throws IOException { FileInputStream fis=new FileInputStream("D:\\bbb.mp4"); FileOutputStream fos = new FileOutputStream("copy.mp4"); long start1 = System.currentTimeMillis(); int b=0; while((b=fis.read())!=-1) { //System.out.println((char)b); fos.write((char)b); } long end1 = System.currentTimeMillis(); System.out.println("使用FileInputStream的read()方法复制时间:" + (end1 - start1) + "ms"); fis.close(); fos.close(); } // 一次读取一个字节数组 public static void method2() throws IOException { FileInputStream fis=new FileInputStream("D:\\bbb.mp4"); FileOutputStream fos = new FileOutputStream("copy.mp4"); long start1 = System.currentTimeMillis(); int b=0; byte[] bs=new byte[1024]; while((b=fis.read(bs))!=-1) { //System.out.println(new String(bs)); fos.write(bs,0,b); } long end1 = System.currentTimeMillis(); System.out.println("使用FileInputStream的readread(byte[] b)方法复制时间:" + (end1 - start1) + "ms"); fis.close(); fos.close(); } // 缓冲流+一次读一个字节 public static void method3() throws IOException { BufferedInputStream bis=new BufferedInputStream(new FileInputStream("D:\\bbb.mp4")); FileOutputStream fos = new FileOutputStream("copy.mp4"); long start1 = System.currentTimeMillis(); int b=0; while((b=bis.read())!=-1) { //System.out.println((char)(b)); fos.write((char)b); } long end1 = System.currentTimeMillis(); System.out.println("使用BufferedInputStream的read()方法复制时间:" + (end1 - start1) + "ms"); bis.close(); fos.close(); } //缓冲流+一次读一个字节数组 public static void method4() throws IOException { BufferedInputStream bis=new BufferedInputStream(new FileInputStream("D:\\bbb.mp4")); FileOutputStream fos = new FileOutputStream("copy.mp4"); long start1 = System.currentTimeMillis(); int len=0; byte[] bs=new byte[1024]; while((len=bis.read(bs))!=-1) { //System.out.println(new String(bs,0,len)); fos.write(bs,0,len); } long end1 = System.currentTimeMillis(); System.out.println("使用BufferedInputStream的read(byet[] b)方法复制时间:" + (end1 - start1) + "ms"); bis.close(); fos.close(); } } 给出每一行代码的注释

最新推荐

recommend-type

Android 数据存储之 FileInputStream 工具类及FileInputStream类的使用

主要介绍了Android 数据存储之 FileInputStream 工具类及FileInputStream类的使用的相关资料,需要的朋友可以参考下
recommend-type

小xlsx1111111111111

小xlsx1111111111111
recommend-type

ATMega board for VisualStudio IDE

APM Mega board for VisualStudio. 對於想要使用 ArduPilot 在 VisualStudio 下編譯的人所需要的一個主板芯片設置檔案.
recommend-type

一款极好用的 Office/WPS/Word/Excel/PPT/PDF工具箱软件 OfficeUtils 2.8

OfficeUtils(Office助手/工具箱)软件是一款极好用的、绿色的 Office/WPS/PDF 辅助处理工具,可用于处理一些 Office 无法解决或轻易解决的问题(如PDF转Word、PDF图片提取、Excel多列组合排序、Excel表合并、Excel提取身份证生日、Word口算题等)。该工具很适合文职工作人员,不需要掌握数据库和编程知识,可批量高效地处理文档,增加办公效率。 https://blog.csdn.net/surfsky/article/details/138686503 # 最新版功能 Excel - Excel 高级查询(列处理、条件过滤、组合排序) - Excel 高级统计(数量、求和、最大值、最小值、平均值、方差) - Excel 关联合并 - Excel 拆分单元格 - Excel 拆分文件 - Excel 合并文件 Word - Word 模板文件生成 - Word 口算生成器 PPT - PPT 模板页面生成 PDF - PDF 转 Word
recommend-type

计算机图形学-从0开始构建一个OpenGL软光栅

视频课程下载——计算机图形学—从0开始构建一个OpenGL软光栅
recommend-type

中文翻译Introduction to Linear Algebra, 5th Edition 2.1节

中文翻译Introduction to Linear Algebra, 5th Edition 2.1节 线性代数的核心问题是求解方程组。这些方程都是线性的,即未知数仅与数相乘——我们绝不会 遇见 x 乘以 y。我们的第一个线性方程组较小。接下来你来看看它引申出多远: 两个方程 两个未知数 x − 2y = 1 3x + 2y = 11 (1) 我们一次从一个行开始。第一个方程 x − 2y = 1 得出了 xy 平面的一条直线。由于点 x = 1, y = 0 解 出该方程,因此它在这条直线上。因为 3 − 2 = 1,所以点 x = 3, y = 1 也在这条直线上。若我们选择 x = 101,那我们求出 y = 50。 这条特定直线的斜率是 12,是因为当 x 变化 2 时 y 增加 1。斜率在微积分中很重要,然而这是线 性代数! 图 2.1 将展示第一条直线 x − 2y = 1。此“行图”中的第二条直线来自第二个方程 3x + 2y = 11。你 不能错过两条线的交点 x = 3, y = 1。点 (3, 1) 位于两条线上并且解出两个方程。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

识别MATLAB微分方程求解中的混沌行为:分析非线性方程混沌行为的实用技巧

![matlab求解微分方程](https://img-blog.csdnimg.cn/2021062810300367.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTQ3OTY3OA==,size_16,color_FFFFFF,t_70) # 1. MATLAB微分方程求解概述 微分方程是描述物理、工程和金融等领域中动态系统的数学模型。MATLAB提供了强大的工具来求解微分方程,包括内置求解器和自定义函数
recommend-type

physon如何做ERP系统

作为一个AI语言模型,我可以给您提供一些基本的建议和思路: 1.明确需求和目标:在开始开发ERP系统之前,需要明确您的业务需求和目标,了解您的业务流程、流程数据、员工角色和权限、财务管理等方面的要求,这样才能更好的设计和开发ERP系统。 2.选择技术框架和开发工具:选择合适的技术框架和开发工具是ERP系统开发的关键。选择一种流行的技术框架和工具可以提高开发效率和质量。 3.设计数据库:ERP系统需要一个功能强大的数据库来存储数据。设计数据库需要考虑数据的完整性、安全性和可扩展性。 4.设计系统架构:系统架构是ERP系统的骨架,需要考虑系统的可扩展性、可维护性和性能。 5.开发和测试:
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。