package com.heima.www.test5;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.InputStream;
import java.io.OutputStream;
public class FileReadAndWriteDemo {
public void fileDemo() {
String regex="\\.java";
try {
File file = new File("d:");
if (!(file.exists() && file.isDirectory()))
try {
throw new Exception("目录不存在");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
File[] files = file.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.endsWith(".java");
}
});
System.out.println(files.length);
File destDir = new File("c:/jad");
if (!destDir.exists())
destDir.mkdir();
for (File f : files) {
FileInputStream fis = new FileInputStream(f);
// String destFileName = f.getName().replaceAll("\\.java{1}quot;",
// ".jad");
String destFileName = f.getName().replaceAll(regex, ".jad");
FileOutputStream fos = new FileOutputStream(new File(destDir,
destFileName));
copy(fis, fos);
fis.close();
fos.close();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void copy(InputStream ips, OutputStream ops)
throws Exception {
int len = 0;
byte[] buf = new byte[1024];
while ((len = ips.read(buf)) != -1) {
ops.write(buf, 0, len);
}
}
}
|
|