/** * @time 2014-8-15 下午05:00:58 * @author new * @function 通过bufferedReader和bufferedWriter 拷贝文件 * */ package com.neusoft.copyfile; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; /** * */ public class CopyFileByBuffer { /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { String oldFileName="hs_err_pid3420.log"; String newFileName="copy_"+oldFileName; CopyFileByBuffer cfBuffer=new CopyFileByBuffer(); cfBuffer.CopyFile(oldFileName, newFileName); System.out.println("结束"); } /** * @function 通过bufferedReader和bufferedWriter 拷贝文件 * @param oldFileName * @param newFileName * @throws IOException */ public void CopyFile(String oldFileName,String newFileName) throws IOException{ //读文件 hs_err_pid3420.log File oldFile=new File(oldFileName); //写文件 File newFile=new File(newFileName); FileReader fReader=new FileReader(oldFile); BufferedReader bReader =new BufferedReader(fReader); FileWriter newFWrite=new FileWriter(newFile); BufferedWriter bWriter=new BufferedWriter(newFWrite); String s=null; while ((s=bReader.readLine())!=null) { System.out.println(s); bWriter.write(s); bWriter.newLine(); bWriter.flush(); } if (bWriter!=null) { bWriter.close(); } if (bReader!=null) { bReader.close(); } System.out.println("复制文件成功"); } }
只要修改文件路径为你的文件路径,就可以实现复制功能了
时间: 2024-09-29 23:03:24