IO流拷贝图片的四种方式

来源:互联网 发布:双程网络剧微博 编辑:程序博客网 时间:2024/04/28 09:16
package IO;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;public class CopyImg {/** * * IO流复制图片(记事本打开不能读懂),使用字节流 * 4种方式,推荐使用第四种 *  */public static void main(String[] args) throws IOException {//数据源File srcfile=new File("数据源");//目的地File destfile=new File("目的地");//methd1(srcfile,destfile);//methd2(srcfile,destfile);//methd3(srcfile,destfile);methd4(srcfile,destfile);}//方式四:缓冲字节流一次读写一个字节数组private static void methd4(File srcfile, File destfile) throws IOException {BufferedInputStream bis=new BufferedInputStream(new FileInputStream(srcfile));BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(destfile));byte[] by=new byte[1024];int length=0;while((length=bis.read(by))!=-1){bos.write(by,0,length);}//关闭资源bos.close();bis.close();}/*//方式三:缓冲字节流一次读写一个字节private static void methd3(File srcfile, File destfile) throws IOException {BufferedInputStream bis=new BufferedInputStream(new FileInputStream(srcfile));BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(destfile));int by=0;while((by=bis.read())!=-1){bos.write(by);}bos.close();bis.close();}*//*//方式二:基本字节流一次读写一个字节数组private static void methd2(File srcfile, File destfile) throws IOException {FileInputStream fis=new FileInputStream(srcfile);FileOutputStream fos=new FileOutputStream(destfile);byte[] by=new byte[1024];int length=0;while((length=fis.read(by))!=-1){fos.write(by,0,length);}fos.close();fis.close();}*//*//方式一:基本字节流一次读写一个字节private static void methd1(File srcfile, File destfile) throws IOException {FileInputStream fis=new FileInputStream(srcfile);FileOutputStream fos=new FileOutputStream(destfile);int by=0;while((by=fis.read())!=-1){fos.write(by);}fos.close();fis.close();}*/}

0 0
原创粉丝点击