(3)I/O流对象-----复制图片/文件/视频的几种I/O流方式

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_27706119/article/details/85057032
  • 根据数据的走向分为:输入流、输出流
  • 根据处理的数据类型分为:字节流、字符流

IO流中对于图片/文件/视频的复制传输方式,可以采用字节流进行输入输出操作,不同之处在于传输效率上。

以图片传输为例:

1.基础字节流

*使用最基础的字节流(文件输入输出流)进行输入输出操作,每次读/写一个字节

public static void run4() throws IOException{
        long begin = System.currentTimeMillis();
	String imgSuffix = ".jpg";
	File srcFile = new File("F:\\Test\\imgs\\A.jpg");
	String newImg = UUID.randomUUID().toString() + imgSuffix;
	File destFile = new File("F:\\Test\\imgs\\" + newImg);
	FileInputStream fis = new FileInputStream(srcFile);
	FileOutputStream fos = new FileOutputStream(destFile);
	int len = 0;
	while ((len = fis.read()) != -1) {
		fos.write(len);
	}
	fos.close();
	fis.close();
        long end = System.currentTimeMillis();
        long useMills = end - begin;
        System.out.println("用时" + useMills + "ms");
}

源图片数据放在文件输入流中,读取时按一个字节的方式操作,写入目标图片方式亦同理,如果你的图片是那种超清、大图、好几兆的话,这读写速度颇低。

*使用最基础的字节流(文件输入输出流)进行输入输出操作,每次读/写一个字节数组

每次读写一个字节,简直是有点慢了。看来,试一把读/写字节数组方式先。

定义一个1024长度的字节数组作为缓冲区间,我们每次读/写1024长度的字节。

public static void run3() throws IOException{
        long begin= System.currentTimeMillis();
	String imgSuffix = ".jpg";
	File srcFile = new File("F:\\Test\\imgs\\A.jpg");
	String newImg = UUID.randomUUID().toString() + imgSuffix;
	File destFile = new File("F:\\Test\\imgs\\" + newImg);
	FileInputStream fis = new FileInputStream(srcFile);
	FileOutputStream fos = new FileOutputStream(destFile);
	int len = 0;
	byte[] bytes = new byte[1024];
	while ((len = fis.read(bytes)) != -1) {
		fos.write(bytes,0,len);
	}
	fos.close();
	fis.close();
        long end = System.currentTimeMillis();
        long useMills = end - begin;
        System.out.println("用时" + useMills + "ms");
}

2.高效字节流

*使用最高效的字节流进行输入输出操作,每次读/写一个字节

public static void run2() throws IOException{
	long begin = System.currentTimeMillis();
	String imgSuffix = ".jpg";
	File srcFile = new File("F:\\Test\\imgs\\A.jpg");
	String newImg = UUID.randomUUID().toString().replace("-", "") + imgSuffix;
	File destFile = new File("F:\\Test\\imgs\\" + newImg);
	BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));
	int len = 0;
	while ((len = bis.read()) != -1) {
		bos.write(len);
		bos.flush();
	}
	bos.close();
	bis.close();
	long end = System.currentTimeMillis();
        long useMills = end - begin;
        System.out.println("用时" + useMills + "ms");
}

注:如果在复制文件时,我们突然又不想使用原来文件名了,调用UUID类的randomUUID()方法随机产生名称,保证每次复制过来的图片名称不一样,哈哈哈哈。

这个时候,我们图片复制的效率相对前两种方式已提高很多了,但是Java却不满足于此,基础字节流都可以每次读写一个字节数组,在缓冲区中字节流必然也能够读取字节数组,至此读写效率发生飞跃式提升。

*使用最高效的字节流进行输入输出操作,每次读/写一个字节数组

public static void run1() throws IOException{
	long begin = System.currentTimeMillis();
	String imgSuffix = ".jpg";
	final String IMG_NAME = "A.jpg";
	File srcFile = new File("F:\\Test\\imgs\\" + IMG_NAME);
	if (srcFile.exists()) {
           for (int i = 0; i < 5; i++) {
		String newImg = UUID.randomUUID().toString().replace("-", "") + imgSuffix;
	        File destFile = new File("F:\\Test\\imgs\\" + newImg);
	        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));
		int len = 0;
		byte[] bytes = new byte[1024];
		while ((len = bis.read(bytes)) != -1) {
        		bos.write(bytes,0,len);
	        	bos.flush();
		}
		bos.close();
		bis.close();	
		}
	long end = System.currentTimeMillis();
        long useMills = end - begin;
        System.out.println("用时" + useMills + "ms");
	}
}

愿你就像早晨八九点钟的太阳,活力十足,永远年轻。

猜你喜欢

转载自blog.csdn.net/qq_27706119/article/details/85057032
今日推荐