package com.pac.day06;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
/*
* 将d://java文件下的.java文件,保存到D://jad文件夹,并改名为.jad
* */
public class Jad2Java {
public static void main(String[] args) throws IOException {
File srcDir = new File("D:\\java1");
if(!(srcDir.isDirectory()||srcDir.exists())){
try {
throw new FileNotFoundException("文件不存在");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
//遍历该目录下的所有以.java结尾的文件,FilenameFilter实现一个文件过滤器
File file[] = srcDir.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.endsWith(".java");
}
});
for(int i = 0 ; i < file.length ;i++){
System.out.println(file[i]);
}
//获取要copy的文件的名字,并在指定目录下创建该文件的名,但是必须将后缀替换
for(int i = 0 ; i < file.length ;i++){
//读取文件的流
FileInputStream in = new FileInputStream(file[i]);
File dir = new File("D:/jad");
if(!(dir.exists() || dir.isDirectory())){
dir.mkdir();
}
String fileName = file[i].getName().replaceAll("\\.java$", ".jad");
FileOutputStream out = new FileOutputStream(new File(dir,fileName));
copy(in,out);
in.close();
out.close();
}
//copy文件到jad文件夹下
}
private static void copy(FileInputStream in, FileOutputStream out) throws IOException {
int off = 0;
int len = 1024;
byte[] buff = new byte[1024];
while((len=in.read(buff,off,len)) != -1){
out.write(buff, off, len);
}
}
}