复制代码
- package cn.itcast.day05;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.io.FilenameFilter;
- import java.io.PrintWriter;
- public class CopyDemo {
- public static void main(String[] args) {
- String srcDir=args[0];
- String desDir=args[1];
- copyFile(srcDir,desDir);
- }
- public static void copyFile(String src,String des){
- File srcFile=new File(src);
- String[] arr=srcFile.list(new FilenameFilter(){
- public boolean accept(File dir,String name){
- return name.endsWith(".java");
- }
- });
- for(int i=0;i<arr.length;i++){
- String desName=arr[i].substring(0, arr[i].lastIndexOf('.'))+".txt";
- readAndWrite(src+arr[i],des+desName);
- }
- }
- public static void readAndWrite(String in,String out){
- BufferedReader bufr=null;
- PrintWriter pw=null;
- try{
- bufr=new BufferedReader(new FileReader(in));
- pw=new PrintWriter(out);
- String line=null;
- while((line=bufr.readLine())!=null){
- pw.println(line);
- pw.flush();
- }
- }
- catch(Exception e){
- throw new RuntimeException("文件读取写入失败");
- }
- finally{
- if(pw!=null)
- pw.close();
- if(bufr!=null)
- try{
- bufr.close();
- }
- catch(Exception e){
- throw new RuntimeException("读取流关闭失败");
- }
- }
- }
- }
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |