题目:
将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();
}
}
}
}
}
------解决方案--------------------
帮你写了个,看看吧
- Java code
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); } } }}