Java IO 字节流基类
InputStream--输入流,
OutPutStream--输出流,
输入流用于读,输出流用于写.
字节流默认一次只读取或输出一个字节。
package jonavin.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 IOUtil {/*** 文件拷贝-- 一个字节一个字节拷贝* @param srcFile* @param destFile*/public static void copyFileByByte(File srcFile,File destFile) throws IOException{if(!srcFile.exists()){throw new IllegalArgumentException("文件:"+srcFile+"不存在!");}if(!srcFile.isFile()){throw new IllegalArgumentException(srcFile+"不是文件!");}FileInputStream in = new FileInputStream(srcFile);//源文件FileOutputStream out = new FileOutputStream(destFile);//目标文件int b;while ((b = in.read()) != -1) {out.write(b);//写入一个字节的低八位 }in.close();out.close();}/*** 拷贝文件一次读取多个字节*/public static void copyFileByByteBuf(File srcFile,File destFile) throws IOException{if(!srcFile.exists()){throw new IllegalArgumentException("文件:"+srcFile+"不存在!");}if(!srcFile.isFile()){throw new IllegalArgumentException(srcFile+"不是文件!");}FileInputStream in = new FileInputStream(srcFile);//源文件FileOutputStream out = new FileOutputStream(destFile);//目标文件int b;byte[] buf = new byte[8*1024];//开辟8K的缓存while ((b = in.read(buf,0,buf.length))!=-1) {out.write(buf, 0, b);}in.close();out.close();}/*** 通过缓冲流copy文件* @throws IOException*/public static void copyFileByBuffed(File srcFile,File destFile)throws IOException{if(!srcFile.exists()){throw new IllegalArgumentException("文件:"+srcFile+"不存在!");}if(!srcFile.isFile()){throw new IllegalArgumentException(srcFile+"不是文件!");}FileInputStream in = new FileInputStream(srcFile);//源文件FileOutputStream out = new FileOutputStream(destFile);//目标文件BufferedInputStream inbuf = new BufferedInputStream(in);BufferedOutputStream outbuf = new BufferedOutputStream(out);int b;while ((b = inbuf.read())!=-1) {outbuf.write(b);}in.close();out.close();inbuf.close();outbuf.close();}/*** 通过缓冲流copy文件 缓冲流一次读取多个字节* @throws IOException*/public static void copyFileByBuffedBuf(File srcFile,File destFile)throws IOException{if(!srcFile.exists()){throw new IllegalArgumentException("文件:"+srcFile+"不存在!");}if(!srcFile.isFile()){throw new IllegalArgumentException(srcFile+"不是文件!");}FileInputStream in = new FileInputStream(srcFile);//源文件FileOutputStream out = new FileOutputStream(destFile);//目标文件BufferedInputStream inbuf = new BufferedInputStream(in);BufferedOutputStream outbuf = new BufferedOutputStream(out);int b;byte[] buf = new byte[8*1024];while ((b = inbuf.read(buf,0,buf.length))!=-1) {outbuf.write(buf,0,b);}in.close();out.close();inbuf.close();outbuf.close();}/*** 文件拷贝* @param srcpath* @param destpath* @throws IOException*/public static void copyFile(String srcpath,String destpath)throws IOException{copyFileByBuffedBuf(new File(srcpath), new File(destpath));} }
调用 : IOUtil.copyFile("C:\apache-tomcat-7.0.57.zip", "c:\ret.dat");//源文件大小9M
copyFileByByte ----》 62229 毫秒
copyFileByByteBuf -----》 97 毫秒
copyFileByBuffed -----》 457 毫秒
copyFileByBuffedBuf ----》 28 毫秒 效率最好