为什么单个字节和字节数组还有缓冲区的读写没有区别。。。

年轻的猴 2020-07-26 10:48:10

import java.io.*;
//需求:把e: \ \笔记.doc复制到当前项目目录下的copy .doc中
// 字节流四种方式复制文件:
// 基本字节流-次读写一个字节:
// 基本字节流-次读写一个字节数组:
// 高效字节流-次读写一个字节:
// 高效字节流一次读写一个字节数组:
public class reviewDemo {
public static void main(String[] args) {
long star1 = System.currentTimeMillis();
try {
method1("D:\\java项目\\笔记.doc","D:\\example\\copy1.doc");
} catch (IOException e) {
e.printStackTrace();
}
long end1 =System.currentTimeMillis();
System.out.println("基本字节流单字节读写总计耗时:"+(end1-star1)+"毫秒");
// 基本字节流-次读写一个字节:
long star2 = System.currentTimeMillis();
try {
method1("D:\\java项目\\笔记.doc","D:\\example\\copy2.doc");
} catch (IOException e) {
e.printStackTrace();
}
long end2 =System.currentTimeMillis();
System.out.println("基本字节用字节数组流总计耗时:"+(end2-star2)+"毫秒");
// 基本字节流-次读写一个字节数组:
long star3 = System.currentTimeMillis();
try {
method1("D:\\java项目\\笔记.doc","D:\\example\\copy3.doc");
} catch (IOException e) {
e.printStackTrace();
}
long end3 =System.currentTimeMillis();
System.out.println("高效字节流读写一个字节的总计耗时:"+(end3-star3) +"毫秒");
//高效字节流-次读写一个字节:

long star4 = System.currentTimeMillis();
try {
method1("D:\\java项目\\笔记.doc","D:\\example\\copy4.doc");
} catch (IOException e) {
e.printStackTrace();
}
long end4 =System.currentTimeMillis();
System.out.println("高效字节流读写一个字节数组的总计耗时:"+(end4-star4)+"毫秒");
//高效字节流一次读写一个字节数组:

}

private static void method4(String srcstring,String deststring) throws IOException {
FileInputStream fis = new FileInputStream(srcstring);
FileOutputStream fos = new FileOutputStream(deststring);
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int len;
byte[] byts= new byte[1024];
while ((len = bis.read(byts)) != -1)
{
bos.write(byts,0,len);
}
bis.close();
bos.close();
}

private static void method3(String srcstring,String deststring) throws IOException {
FileInputStream fis = new FileInputStream(srcstring);
FileOutputStream fos = new FileOutputStream(deststring);
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int by;
while ((by = bis.read()) != -1)
{
bos.write(by);
}
bis.close();
bos.close();
}

private static void method2(String srcstring,String deststring) throws IOException {
FileInputStream fis=new FileInputStream(srcstring);
FileOutputStream fos=new FileOutputStream(deststring);
byte[] byts =new byte[1024];
int len;

while((len=fis.read(byts))!=-1)
{
fos.write(byts,0,len);
}
fis.close();
fos.close();
}

private static void method1(String srcstring,String deststring) throws IOException {
FileInputStream fis=new FileInputStream(srcstring);
FileOutputStream fos=new FileOutputStream(deststring);
int by=0;
while((by=fis.read())!=-1)
{
fos.write(by);
}
fis.close();
fos.close();
}
}

...全文
2868 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
年轻的猴 2020-07-26
  • 打赏
  • 举报
回复
找到原因了 方法都用的1
年轻的猴 2020-07-26
  • 打赏
  • 举报
回复
可是他写的这个区别就很大
鸡窝里的毛 2020-07-26
  • 打赏
  • 举报
回复
因为最终都是8位的二进制啊。

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧