题目:
将d:java目录下的所有.java文件复制到d:jad目录下,并将原文件改成.jad格式的怎么写啊?
我只会把c盘的某个文件复制到d盘的、怎么写上面的?
例如下面的把c盘的下的t文件下的a.txt复制到d盘
public static void main(String []args){
FileReader fr=null;
  FileWriter fw=null;
  char[] ch=new char[1024];
  int n=0;
  
  try {
   fr=new FileReader("c:\\t\\a.txt");
   fw=new FileWriter("D:\\a.txt");
   while((n=fr.read(ch))!=-1)
    fw.write(ch,0,n);
    fw.flush();
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }finally{     if(fw!=null){
      try {
       fw.close();
       } catch (IOException e) {
         e.printStackTrace();
       }      }   if(fr!=null){
    try {
     fr.close();
     } catch (IOException e) {
      e.printStackTrace();
     }
    }
   }  }
}

解决方案 »

  1.   

    一样的吧   你取文件名是.java的复制呗  最后把名字改一下就成了
      

  2.   

    在LZ的代码上追加文件改名
    public static void main(String []args){
        FileReader fr=null;
        FileWriter fw=null;
        char[] ch=new char[1024];
        int n=0;
      
        try {
            fr=new FileReader("c:\\t\\a.txt");
            fw=new FileWriter("D:\\a.txt");
            while((n=fr.read(ch))!=-1)
                fw.write(ch,0,n);
            fw.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                if (fw!=null) fw.close();
                if (fr!=null) fr.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }    //以上是LZ的复制文件代码
         //文件改名
        File f = new File("D:\\a.jad");
        if (f.exsits()) f.delete();
        new File("D:\\a.txt").renameTo(f);
    }
      

  3.   

    帮你写了个,看看吧package com.test;import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;public class Test { public static void main(String[] args) {
    FileReader fr = null;
    FileWriter fw = null;
    char[] ch = new char[1024];
    int n = 0;
    List<File> files = getFiles("d:\\java");
    for(File file : files){
    try {
    File f = new File("D:\\java1\\" + file.getName());
    fr = new FileReader(file);
    fw = new FileWriter(f);
    while ((n = fr.read(ch)) != -1)
    fw.write(ch, 0, n);
    fw.flush();
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    if (fw != null) {
    try {
    fw.close();
    } catch (IOException e) {
    e.printStackTrace();
    }

    }
    if (fr != null) {
    try {
    fr.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    String fileName = file.getAbsolutePath();
    File fl = new File(fileName.substring(0, fileName.indexOf(".")) + ".jad");
    file.renameTo(fl);
    }
    }
    private static List<File> getFiles(String path) {
    List<File> files = new ArrayList<File>();
    File file = new File(path);
    getAllFiles(file, files);
    return files;
    } private static void getAllFiles(File file, List<File> files) {
    if (file.isFile()) {
    files.add(file);
    } else {
    File[] fs = file.listFiles();
    for (File f : fs) {
    getAllFiles(f, files);
    }
    }
    }
    }