公司要申请项目著作权,需要将源码整合到一起打印出来。写的一段整合的代码,跟大家分享下:
源码:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.util.Properties;
public class Merge {
static String folderPath = "D:/src/";
public void Merge(){
InputStream in = this.getClass().getClassLoader().getResourceAsStream("folderpath.properties");
Properties p = new Properties();
try {
p.load(in);
folderPath = p.getProperty("folderpath");
System.out.println(folderPath);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void go() {
System.out.println("开始读取文件夹中文档 》》》");
File file = new File(folderPath);
System.out.println(folderPath);
analysisDir(file);
System.out.println("合并数据完成《《《");
}
public static Boolean isDir(File file) {
if (file.isDirectory())
return true;
return false;
}
public static void analysisDir(File file) {
if (isDir(file)) {
File[] files = file.listFiles();
for (File tempFile : files) {
analysisDir(tempFile);
}
} else {
String fileName = file.getName();
System.out.println(fileName);
if (fileName.indexOf(".java")>0) {
File unionFile = new File(folderPath + "union.txt");
String line = "";
FileInputStream fis;
try {
fis = new FileInputStream(file);
//设置编码,以防乱码
InputStreamReader brs = new InputStreamReader(fis,"utf-8");
BufferedReader br = new BufferedReader(brs);
//仍然乱码,在设置输出编码
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(unionFile,true),"utf-8");
//FileWriter writer = new FileWriter(unionFile, true);
BufferedWriter bw = new BufferedWriter(writer);
while ((line = br.readLine()) != null) {
System.out.println(line);
bw.write(line);
bw.newLine();
//不要忘记flush().
bw.flush();
//System.setOut(new PrintStream(new FileOutputStream(folderPath + "output.txt")));
}
//brs.close();
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}