import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class ReadFile {
private static List<String> filePath = new ArrayList<String>();
public static void main(String[] args) {
String str = "";
traverseFolder2("D:\\work\\aimMaster");
for(int i=0;i<filePath.size();i++) {
System.out.println(filePath.get(i));
try {
str += readFile(new File(filePath.get(i)));
} catch (IOException e) {
e.printStackTrace();
}
BufferedWriter out;
try {
out = new BufferedWriter(new FileWriter("d:\\code.txt"));
out.write(str);
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static List<String> traverseFolder2(String path) {
File file = new File(path);
if (file.exists()) {
File[] files = file.listFiles();
if (null == files || files.length == 0) {
// System.out.println("文件夹是空的!");
} else {
for (File file2 : files) {
if (file2.isDirectory()) {
// System.out.println("文件夹:" + file2.getAbsolutePath());
traverseFolder2(file2.getAbsolutePath());
} else {
String path2 = file2.getAbsolutePath();
if(path2.endsWith(".java")) {
filePath.add(path2);
// System.out.println(path2);
}
}
}
}
} else {
System.out.println("文件不存在!");
}
return filePath;
}
public static String readFile(File path) throws IOException{
InputStream is = new FileInputStream(path);
byte[] bytes = new byte[is.available()];
int len = is.read(bytes);
String str = null;
while(len!=-1){
str = new String(bytes, 0, len);
len = is.read(bytes);
}
is.close();
return str;
}
}