本文共 1511 字,大约阅读时间需要 5 分钟。
private static void fileCopy1(File from, File to){ InputStream in = null; OutputStream out = null; try{ //定义高效字节流 in = new BufferedInputStream(new FileInputStream(from)); out = new BufferedOutputStream(new FileOutputStream(to)); //传输 int b = 0; while((b = in.read())!=-1){ out.write(b); } }catch(Exception e){ e.printStackTrace(); }finally{ try{ assert in != null; in.close(); assert out != null; out.close(); }catch(Exception e){ e.printStackTrace(); }finally{ in = null; out = null; } } } // 使用字符流复制,只能复制文本文件 private static void fileCopy2(File from,File to){ Reader r = null; Writer w= null; try{ r = new BufferedReader(new FileReader(from)); w = new BufferedWriter(new FileWriter(to)); int b = 0; while((b = r.read())!=-1){ w.write(b); } }catch(Exception e){ e.printStackTrace(); }finally{ try{ assert r != null; r.close(); assert w != null; w.close(); }catch(Exception e){ e.printStackTrace(); }finally{ r = null; w = null; } } }
转载地址:http://ubmmi.baihongyu.com/