62,628
社区成员
发帖
与我相关
我的任务
分享
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();
}
}