直接修改成这样算了- import java.io.*;
- import java.util.*;
- class MyBufferInputStream
- {
- private InputStream in;
- private byte buf[]=new byte[1024*20];
- private int pos=0,count=0;
- MyBufferInputStream(InputStream in)
- {
- this.in=in;
- }
- //一次读一个字节,从缓冲区(字节数组)获取
- public int myRead() throws IOException
- {
- if(count==0)
- {
- count=in.read(buf);
- if(count<0)
- return -1;
- pos=0;
- byte b=buf[pos];
- count--;
- pos++;
- return b&255;
- }
- else if(count>0)
- {
- byte b=buf[pos];
- count--;
- pos++;
- return b&0xff;
-
- }
- return -1;
- }
- public void MyClose() throws IOException
- {
- in.close();
-
- }
- }
- class FileCopy
- {
- //字节流的方式不仅可以拷贝普通文件,还可以拷贝任意的二进制文件例如exe文件
- public static void main(String[] args) throws IOException
- { System.out.printf("请输入源文件,与目的地:");
- Scanner input=new Scanner(System.in);
- String name=input.next();
- String name1=input.next();
-
- long start=System.currentTimeMillis();
- FileCopyBuffer(name,name1);//FileCopyBuffer(源文件,目标位置)
- long end=System.currentTimeMillis();
- System.out.println((end-start)+"毫秒");
- }
- public static void FileCopyBuffer(String name,String name2) throws IOException
- {
- MyBufferInputStream fr= new MyBufferInputStream(new FileInputStream(name));
- BufferedOutputStream fw= new BufferedOutputStream(new FileOutputStream(name2));
- int byte1=0;
- while((byte1=fr.myRead())!=-1)
- {
- fw.write(byte1);
- }
- fr.MyClose();
- fw.close();
-
- }
-
-
- }
复制代码 |