基于字节流和字符流实现的文件复制
import java.io.*;import java.io.File;/** * @author 极致书生 基于字节流实现的文件的拷贝 * */public class ByteCopy { public static void main(String[] args) { File f_s = new File("g:/wjl/jizhishusheng.txt"); // 要拷贝的文件对象 File f_d = new File("D:/yxm"); // 要拷贝到的路径 if (!f_d.exists()) { // 判断要拷贝到的路径是否存在,不存在就创建 f_d.mkdirs(); } File f_dd = new File(f_d, "7s.txt"); FileInputStream fis = null; FileOutputStream fos = null; try { fis = new FileInputStream(f_s); fos = new FileOutputStream(f_dd); byte[] byteTemp = new byte[1024]; while(fis.read(byteTemp) != -1){ int size =byteTemp.length; fos.write(byteTemp,0,size); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (fis != null) { fos.close(); } if (fos != null) { fis.close(); } } catch (IOException e) { e.printStackTrace(); } } }}package com.neusoft.mytest.myproj;import java.io.*;import java.io.File;/** * @author 极致书生 基于字符流实现的文件的拷贝 * */public class CharCopy { public static void main(String[] args) { File f_s = new File("g:/wjl/jizhishusheng.txt"); // 要拷贝的文件对象 File f_d = new File("D:/yxm"); // 要拷贝到的路径 if (!f_d.exists()) { // 判断要拷贝到的路径是否存在,不存在就创建 f_d.mkdirs(); } File f_dd = new File(f_d, "7s.txt"); FileReader fis = null; FileWriter fos = null; try { fis = new FileReader(f_s); fos = new FileWriter(f_dd); char[] charTemp = new char[1]; while (fis.read(charTemp) != -1) { int size = charTemp.length; fos.write(charTemp, 0, size); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (fis != null) { fos.close(); } if (fos != null) { fis.close(); } } catch (IOException e) { e.printStackTrace(); } } }}posted on 2016-02-19 21:05 1130136248 阅读(169) 评论(0) 收藏 举报
浙公网安备 33010602011771号