最近要将一个工程的所有源代码导入到一个文件中,要如下图效果。
于是写了如下代码:最后用Notepad上色,paste到word中即可。
import java.io.*;
public class ExportCode {
static FileWriter fw;
static BufferedWriter writer;
static String headTitle = "=======================================";
static String endline="\n";
public ExportCode() {}
public ExportCode(String outputPath)
{
String os=System.getProperties().getProperty("os.name");
if(os.startsWith("win")||os.startsWith("Win"))endline="\r\n";
try {
// 设置成尾部追加方式
fw = new FileWriter(outputPath, true);
writer = new BufferedWriter(fw);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @param path 绝对路径
* @param filename 要读的文件名
*/
public void WriteToMyFile(String path, String filename) {
if (!filename.endsWith(".java") )
return;
try {
writer.write(endline+headTitle+endline);
writer.write("『"+filename+"』");
writer.write(endline+headTitle+endline);
BufferedReader br = new BufferedReader(new FileReader(path));
String buf = br.readLine();
while (buf != null) {
writer.write(buf + endline);
buf = br.readLine();
}
// 输出到文件
writer.flush();
if (br != null)br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//递归遍历当前文件夹下的所有文件
public void showAllSubFile(String path) {
File f = new File(path);
String[] list = f.list();
for (String s : list) {
// System.out.println(s);
File subf = new File(f.getPath() + File.separator + s);
// 如果当前s所代表的是文件夹
if (subf.isDirectory())
showAllSubFile(subf.getPath());
else {
WriteToMyFile(subf.getPath(), s);
}
}
}
public static void main(String[] args) {
System.out.println("Export start....");
/*输入文件夹路径*/
String inpath="C:\\Users\\sq\\Desktop\\test";
/*输出文件的路径*/
String outpathString="C:\\Users\\sq\\Desktop\\output.java";
new ExportCode(outpathString).showAllSubFile(inpath);
System.out.println("Export Complete.");
// 最后关掉输出流
try {
if (writer != null)
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}