package IO;
import java.io.*;
public class FileCopy
{
public static void main(String[] args) throws Exception
{
File srcDir = new File("java");
if(!(srcDir.isDirectory() && srcDir.exists()))
{
throw new Exception("目录不存在");
}
//String[] list,File[] listFiles方法可以接收一个FilenameFilter参数,
//FilenameFilter接口里包含了一个accept(File dir,String name)方法,
//该方法依次对指定的File的所有的子目录或者文件进行迭代,如果方法返回true或者false /*File[] files = srcDir.listFiles(
new FilenameFilter()
{
public boolean accept(File dir, String name)
{
return name.endsWith(".java");
}
}
);*/ File[] files = srcDir.listFiles((dir, name) -> name.endsWith(".java"));
File destDir = new File("jad");
if(!destDir.exists())
{
destDir.mkdir();
}
for (File file : files)
{
String destFileName = file.getName().replaceAll("\\.java$", ".jad");
File destFile = new File(destDir, destFileName);
try(FileInputStream fis = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream(destFile))
{
copy(fis,fos);
}
}
}
public static void copy(InputStream ips, OutputStream ops)throws IOException
{
byte[] bbuf = new byte[1024];
int hasRead = 0;
while((hasRead = ips.read(bbuf)) > 0)
{
ops.write(bbuf, 0, hasRead);
}
}
}