标签:exists copy create exception cep src stream write put
package liuer; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class CopyTest { public static void main(String[] args) { } //复制文件, public static void copy(String src,String dest) throws IOException{ //1.创建两个流 File f=new File(dest); if(!f.exists()){ f.createNewFile(); } FileInputStream fis=new FileInputStream(src); FileOutputStream fos=new FileOutputStream(f); //2.边读边写 //方法一 //int a =-1; //while((a=fis.read())!=-1){ // fos.write(a); //} //方法二 //byte[]b=new byte[fis.available()]; //fis.read(b); //fos.write(b); //方法三 byte[]b=new byte[8192]; int size=-1; while((size=fis.read(b))!=-1){ fos.write(b,0,size); } //3.关闭两个流 fis.close(); fos.close(); } }
标签:exists copy create exception cep src stream write put
原文地址:http://www.cnblogs.com/zyy1130/p/6846854.html