java 将一张图片拷贝到另外一个地方,几种方式的速度测试

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

@SpringBootApplication
public class DemoApplication {

public static void main(String[] args) throws InterruptedException {
    showtest("aaaa","1");
    Thread.sleep(5000);
    showtest("bbbb","2");
    Thread.sleep(5000);
    showtest("cccc","3");
    Thread.sleep(5000);
    showtest("dddd","4");
}
public static void showtest(String a1,String a2){
    long start3 = System.currentTimeMillis();
    try {
        if(a2=="1"){
            for(int i=1;i<=1000;i++){
                copyByStream("F:/1.jpg","F:/1/"+String.valueOf(i)+".jpg");
            }
        }else
        if(a2=="2"){
            for(int i=1;i<=1000;i++){
                copyByStream1("F:/1.jpg","F:/2/"+String.valueOf(i)+".jpg");
            }
        }else
        if(a2=="3"){
            for(int i=1;i<=1000;i++){
                copyByStream2("F:/1.jpg","F:/3/"+String.valueOf(i)+".jpg");
            }
        }else
        if(a2=="4"){
            for(int i=1;i<=1000;i++){
                copyByImageIO("F:/1.jpg","F:/4/"+String.valueOf(i)+".jpg");
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    long end3 = System.currentTimeMillis();
    System.out.println(a1+"'s time---------" + (start3 - end3));
}
public static void copyByStream(String sourcePath,String targetPath) throws Exception {
    // 打开输入流
    FileInputStream fis = new FileInputStream(sourcePath);
    // 打开输出流
    FileOutputStream fos = new FileOutputStream(targetPath);

    // 读取和写入信息
    int len = 0;
    while ((len = fis.read()) != -1) {
        fos.write(len);
    }

    // 关闭流  先开后关  后开先关
    fos.close(); // 后开先关
    fis.close(); // 先开后关
}
public static void copyByStream1(String sourcePath,String targetPath) throws Exception {
    // 打开输入流
    FileInputStream fis = new FileInputStream(sourcePath);
    // 打开输出流
    FileOutputStream fos = new FileOutputStream(targetPath);

    // 读取和写入信息
    int len = 0;
    // 创建一个字节数组,当做缓冲区
    byte[] b = new byte[1024];
    while ((len = fis.read(b)) != -1) {
        fos.write(b);
    }

    // 关闭流  先开后关  后开先关
    fos.close(); // 后开先关
    fis.close(); // 先开后关
}
public static void copyByStream2(String sourcePath,String targetPath) throws Exception {
    // 打开输入流
    FileInputStream fis = new FileInputStream(sourcePath);
    // 打开输出流
    FileOutputStream fos = new FileOutputStream(targetPath);

    // 读取和写入信息
    int len = 0;
    // 创建一个字节数组,当做缓冲区
    byte[] b = new byte[1024];
    while ((len = fis.read(b)) != -1) {
        fos.write(b, 0, len);
    }

    // 关闭流  先开后关  后开先关
    fos.close(); // 后开先关
    fis.close(); // 先开后关

}
public static void copyByImageIO(String sourcePath,String targetPath) throws Exception {
    try {
        File input = new File(sourcePath);
        BufferedImage bim = ImageIO.read(input);
        File output = new File(targetPath);
        ImageIO.write(bim, "jpg", output);
    } catch (IOException e) {
        e.getMessage();
    }
}

}
以下是结果,自己看
“C:\Program Files\Java\jdk1.8.0_191\bin\java.exe”。。。。。
aaaa’s time----------85412
bbbb’s time----------863
cccc’s time----------657
dddd’s time----------14309

Process finished with exit code 0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值