package it.BufferedWriterDemo;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
/*
* d:\\class\\*.java复制到f:\\world\\*.txt
*/
public class CopyOfCopyFileDemo2 {
public static void main(String[] args) {
File file1 = new File("d:\\class文件");
File[] file2 = file1.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
File file5 = new File(dir, name);
boolean flag = file5.isFile();
boolean flag1 = name.endsWith(".java");
return flag && flag1;
}
});
File file3 = new File("f:\\world");
if (!(file3.exists())) {
file3.mkdir();
}
for (File file : file2) {
String name = file.getName();
File file4 = new File(file3, name);
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new FileReader(file));
bw = new BufferedWriter(new FileWriter(file4));
String line = null;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
bw.flush();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
File[] file6 = file3.listFiles();
for (File fil : file6) {
String s = fil.getName();
String news = s.replace(".java", ".txt");
File file7 = new File(file3, news);
fil.renameTo(file7);
}
}
}
|
|