IO流复制文件
方法一
字符
注意点:写和读需要保持一致的编码形式。
public class eee {public static void main(String[] args) {FileInputStream in = null;FileOutputStream out = null;OutputStreamWriter outw = null;InputStreamReader inr = null;try{out = new FileOutputStream("D:\\远程\\11.txt");in = new FileInputStream("D:\\远程\\22.txt");outw = new OutputStreamWriter(out,"UTF-8");inr = new InputStreamReader(in,"UTF-8");int len = 0;char[] c = new char[1024];while((len=inr.read(c))!=-1){outw.write(c, 0, len);}}catch(IOException ex){System.out.println(ex);throw new RuntimeException("文件写入失败,重试");}finally{try{if(outw!=null)out.flush();outw.close();}catch(IOException e){throw new RuntimeException("关闭资源失败");}finally{try{if(inr!=null){inr.close();}}catch(IOException ea){throw new RuntimeException("关闭资源失败");}}}}
}
方法二
/** 将数据源 c:\\a.txt* 复制到 d:\\a.txt 数据目的* 字节输入流,绑定数据源* 字节输出流,绑定数据目的* * 输入,读取1个字节* 输出,写1个字节*/
public class Copy {public static void main(String[] args) {//定义两个流的对象变量FileInputStream fis = null;FileOutputStream fos = null;try{//建立两个流的对象,绑定数据源和数据目的fis = new FileInputStream("c:\\t.zip");fos = new FileOutputStream("d:\\t.zip");//字节输入流,读取1个字节,输出流写1个字节int len = 0 ;while((len = fis.read())!=-1){fos.write(len);}}catch(IOException ex){System.out.println(ex);throw new RuntimeException("文件复制失败");}finally{try{if(fos!=null)fos.close();}catch(IOException ex){throw new RuntimeException("释放资源失败");}finally{try{if(fis!=null)fis.close();}catch(IOException ex){throw new RuntimeException("释放资源失败");}}}}
}
方法二可以参考:
public static void main(String[] args) throws IOException {FileInputStream fis = new FileInputStream("c:\\a.txt");//创建字节数组byte[] b = new byte[1024];int len = 0 ;while( (len = fis.read(b)) !=-1){System.out.print(new String(b,0,len));}fis.close();}
方法三
/** 字符流复制文本文件,必须文本文件* 字符流查询本机默认的编码表,简体中文GBK* FileReader读取数据源* FileWriter写入到数据目的*/
public class Copy_2 {public static void main(String[] args) {FileReader fr = null;FileWriter fw = null;try{fr = new FileReader("c:\\1.txt");fw = new FileWriter("d:\\1.txt");char[] cbuf = new char[1024];int len = 0 ;while(( len = fr.read(cbuf))!=-1){fw.write(cbuf, 0, len);fw.flush();}}catch(IOException ex){System.out.println(ex);throw new RuntimeException("复制失败");}finally{try{if(fw!=null)fw.close();}catch(IOException ex){throw new RuntimeException("释放资源失败");}finally{try{if(fr!=null)fr.close();}catch(IOException ex){throw new RuntimeException("释放资源失败");}}}}
}
知识点
缓冲流
字节缓冲流
/** 文件复制方式,字节流,一共4个方式* 1. 字节流读写单个字节 125250 毫秒* 2. 字节流读写字节数组 193 毫秒 OK* 3. 字节流缓冲区流读写单个字节 1210 毫秒* 4. 字节流缓冲区流读写字节数组 73 毫秒 OK*/
public class Copy {public static void main(String[] args)throws IOException {long s = System.currentTimeMillis();copy_4(new File("c:\\q.exe"), new File("d:\\q.exe"));long e = System.currentTimeMillis();System.out.println(e-s);}/** 方法,实现文件复制* 4. 字节流缓冲区流读写字节数组*/public static void copy_4(File src,File desc)throws IOException{BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(desc));int len = 0 ;byte[] bytes = new byte[1024];while((len = bis.read(bytes))!=-1){bos.write(bytes,0,len);}bos.close();bis.close();}/** 方法,实现文件复制* 3. 字节流缓冲区流读写单个字节*/public static void copy_3(File src,File desc)throws IOException{BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(desc));int len = 0 ;while((len = bis.read())!=-1){bos.write(len);}bos.close();bis.close();}/** 方法,实现文件复制* 2. 字节流读写字节数组*/public static void copy_2(File src,File desc)throws IOException{FileInputStream fis = new FileInputStream(src);FileOutputStream fos = new FileOutputStream(desc);int len = 0 ;byte[] bytes = new byte[1024];while((len = fis.read(bytes))!=-1){fos.write(bytes,0,len);}fos.close();fis.close();}/** 方法,实现文件复制* 1. 字节流读写单个字节*/public static void copy_1(File src,File desc)throws IOException{FileInputStream fis = new FileInputStream(src);FileOutputStream fos = new FileOutputStream(desc);int len = 0 ;while((len = fis.read())!=-1){fos.write(len);}fos.close();fis.close();}
}
推荐用缓冲流!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
字符缓冲流
/** 使用缓冲区流对象,复制文本文件* 数据源 BufferedReader+FileReader 读取* 数据目的 BufferedWriter+FileWriter 写入* 读取文本行, 读一行,写一行,写换行*/
public class Copy_1 {public static void main(String[] args) throws IOException{BufferedReader bfr = new BufferedReader(new FileReader("c:\\w.log")); BufferedWriter bfw = new BufferedWriter(new FileWriter("d:\\w.log"));//读取文本行, 读一行,写一行,写换行String line = null;while((line = bfr.readLine())!=null){bfw.write(line);bfw.newLine();bfw.flush();}bfw.close();bfr.close();}
}
/** 打印流实现文本复制* 读取数据源 BufferedReader+File 读取文本行* 写入数据目的 PrintWriter+println 自动刷新*/
public class PrintWriterDemo1 {public static void main(String[] args) throws IOException{BufferedReader bfr = new BufferedReader(new FileReader("c:\\a.txt"));PrintWriter pw = new PrintWriter(new FileWriter("d:\\a.txt"),true);//比BufferedWriter还要省事String line = null;while((line = bfr.readLine())!=null){pw.println(line);}pw.close();bfr.close();}
}