这个是我用字节流复制图片的代码。
- package cn.itcast.day;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- public class StreamTest {
- public static void main(String[] args) throws IOException{
- // TODO Auto-generated method stub
- long start = System.currentTimeMillis();
- copy();
- long end = System.currentTimeMillis();
- System.out.println("time = "+ (end - start));
- }
- public static void copy_1()throws IOException{
- BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("d:\\day01\\1.jpg"));
- BufferedInputStream bis = new BufferedInputStream(new FileInputStream("d:\\day01\\a.jpg"));
- int len = 0;
- while((len = bis.read())!= -1){
- bos.write(len);
- bos.flush();
- }
- bos.close();
- bis.close();
- }
- public static void copy()throws IOException{
- FileInputStream fis = new FileInputStream("d:\\day01\\a.jpg");
- My_Stream ms = new My_Stream(fis);
- BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("d:\\day01\\1.jpg"));
- int len = 0;
- //System.out.println(ms.myRead());
- while((len = ms.myRead())!= -1){
- bos.write(len);
- bos.flush();
- }
- ms.myClose();
- bos.close();
- }
- }
- class My_Stream{
- private InputStream ips;
- private byte[] buf = new byte[1024*100];
- private int len = 0;
- private int count = 0;
- public My_Stream(InputStream ips) {
- this.ips = ips;
- }
- public int myRead()throws IOException{
- if(len == 0){
- count = 0;
- len = ips.read(buf);
- if(len < 0)
- return -1;
- byte b = buf[count];
- len--;
- count++;
- return b&255;
- }else if(len > 0){
- byte b = buf[count];
- len--;
- count++;
- return b&255;
- }
- return -1;
- }
- public void myClose()throws IOException{
- ips.close();
- }
- }
复制代码
问题是:这个是啥原因啊,我电脑用的 i 5 的cpu啊。
|