package java7; import com.sun.org.apache.bcel.internal.generic.NEW; import org.junit.Test; import java.io.*; /** * @author 冀帅 * @date 2020/8/19-11:40 */ public class InOutTest { @Test public void test(){ //1 FileInputStream in = null; FileOutputStream out = null; try { File file = new File("IMG_20200817_113832.jpg"); File file1 = new File("Isa.jpg"); //2 in = new FileInputStream(file); out = new FileOutputStream(file1); //3 byte[] Byte = new byte[1024]; int len; while ((len = in.read(Byte))!=-1){ out.write(Byte,0,len); } } catch (IOException e) { e.printStackTrace(); } finally { //4 try { if (in!=null) in.close(); } catch (IOException e) { e.printStackTrace(); } try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } // 指定路径下的文件的复制 public void copyFile(String srcPath,String destPath){ //1 FileInputStream in = null; FileOutputStream out = null; try { File file = new File(srcPath); File file1 = new File(destPath); //2 in = new FileInputStream(file); out = new FileOutputStream(file1); //3 byte[] Byte = new byte[1024]; int len; while ((len = in.read(Byte))!=-1){ out.write(Byte,0,len); } } catch (IOException e) { e.printStackTrace(); } finally { //4 try { if (in!=null) in.close(); } catch (IOException e) { e.printStackTrace(); } try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } @Test public void test1(){ long start = System.currentTimeMillis(); String srcPath = "D:\\狂神说java\\_狂神说Java_JavaWeb入门到实战3.mp4"; String destPath = "D:\\狂神说java\\_狂神说Java_JavaWeb入门到实战555.mp4"; copyFile(srcPath,destPath); long end = System.currentTimeMillis(); System.out.println("复制操作花费的时间为:"+(end-start));//2132 } }