public class CopyFile {
public static void main(String[] args){
File file = new File("c:\\java\\java\\copys");
File file2 = new File("c:\\java\\java\\jap ");
if (!file2.exists()) {
file2.mkdirs();
}
File[] files = file.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return new File(dir, name).isFile() && name.endsWith(".txt");
}
});
for (File f : files) {
// String name = f.getName();
String newName = f.getName().replace(".txt", ".java");
File file3 = new File("c:\\java\\java\\copys ", newName);
f.renameTo(file3);
File file4 = new File(file2, newName);
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new FileReader(f));
bw = new BufferedWriter(new FileWriter(file4));
String line = null;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
bw.flush();
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
|
|