import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.StringWriter; import java.nio.channels.FileChannel; import java.util.Iterator; import java.util.List; import java.util.Map; import org.apache.commons.io.FileUtils; import org.apache.commons.lang.StringUtils; import com.ist.common.properties.SysConfig; /** * 文件合并 * * * */ public class ServerTemplate extends Template { /** 跨平台的换行符 */ public static final String LINE_SEPARATOR = System.getProperty("line.separator"); /** * 合并文件 * @Method mergeFiles * @Author lbh * @Date 2014-12-11 下午02:59:54 * @param outFile 路径+输出文件名 * @param files 输入文件列表 */ public boolean mergeFiles(String outFile, List<String> files) { boolean mergeResult = true; FileChannel fileOutChannel = null; //声明通道 FileReader fileReader = null; BufferedReader bufferedReader = null ; try{ if(files == null || files.size() < 1){ logger.info("Make sure there have outnumber 2 files "); //System.out.println("Make sure there have outnumber 2 files "); mergeResult = false; } else if(files.size() == 1){//只有一个文件,那么只要改名即可 File file = new File(files.get(0).toString()); file.renameTo(new File(outFile)); } else{ fileOutChannel = new FileOutputStream(outFile).getChannel(); //实例化输出通道,此时将 创建文件。 //循环所有文件 /*Iterator it = fileMap.keySet().iterator(); int i = 0; //计数 while(it.hasNext()){*/ for(int i=0; i<files.size();i++){ //System.out.println(files.get(i)); File file = new File(files.get(i).toString()); if(!file.exists()){//容错检查 logger.info("Make sure every file is existed"); //System.out.println("file:"+ files.get(i) +" is not existed"); } fileReader = new FileReader(file); if(!fileReader.ready()){//容错检查 logger.info("Make sure every file is existed"); //System.out.println("file "+ files.get(i) +" is not already to read"); } bufferedReader = new BufferedReader(fileReader); /*InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(files[i]),"UTF-8");//此为stream方式读文件,做备用。 BufferedReader bufferedReader = new BufferedReader(inputStreamReader);*/ //开始处理文件 if(bufferedReader != null){ if(i == 0){ //如果是首个文件,那么除去最后一行保留前两行再写入 mergeResult = handleFile("fist", bufferedReader, outFile); } else if(i == files.size() - 1){ //如果是最后一个文件,那么去除前两行 ,保留最后一行 mergeResult = handleFile("last", bufferedReader, outFile); } else{ //其他则同时去除前两行和最后一行 mergeResult = handleFile("normal", bufferedReader, outFile); } //System.out.println("file:"+ files.get(i) +" merge sate is -- "+mergeResult); //logger.info("file:"+files.get(i)+" merge sate is -- "+mergeResult); } //删除该文件 file.delete(); } } } catch (IOException e){ e.printStackTrace(); return mergeResult; } //关闭 if(fileOutChannel !=null){ try{bufferedReader.close();}catch (IOException ignore) {return mergeResult;} } if(fileReader !=null){ try{fileReader.close();}catch (IOException ignore) {return mergeResult;} } if(bufferedReader !=null){ try{bufferedReader.close();}catch (IOException ignore) {return mergeResult;} } return mergeResult; } /** * 处理单个文件,读取后立即写入, type:fist--首个文件;last--末个文件;以及其他文件 * @Method handleFile * @Author lbh * @Date 2014-12-11 上午09:49:19 */ public static boolean handleFile(String type, BufferedReader bufferedReader, String outFile){ boolean handleResult = false; //是否成功 FileWriter fileWriter = null; String readString = ""; //存放读取数据的变量 String strA,strB,strC; //临时变量 try{ fileWriter = new FileWriter(outFile,true); if("fist".equals(type)){ //首文件的处理 strA = ""; strB = ""; strC = ""; int fistI = 0;//计数 while((strC = bufferedReader.readLine()) != null){ //从第一行开始循环读 strA = strB; strB = strC; readString = strA + LINE_SEPARATOR;//最后一行的strB被丢弃 //输出 if(fistI > 0){//避免第一行空行 fileWriter.write(readString); } fistI ++; } handleResult = true; } if("last".equals(type)){ //末文件的处理 strA = ""; bufferedReader.readLine(); bufferedReader.readLine(); //执行两次,已读取并丢弃了前两行。 while((strA = bufferedReader.readLine()) != null){ //开始循环读 readString = strA + LINE_SEPARATOR; //输出 fileWriter.write(readString); } handleResult = true; } else { //其他文件的处理 strA = ""; strB = ""; strC = ""; bufferedReader.readLine(); bufferedReader.readLine(); //执行两次,已读取并丢弃了前两行。 long i = 0; while((strC = bufferedReader.readLine()) != null){ //开始循环读 strA = strB; strB = strC; readString = strA + LINE_SEPARATOR;//最后一行的strB被丢弃 //输出 if(i > 0){//避免第一行空行 fileWriter.write(readString); } i ++; } handleResult = true; } } catch (IOException e) { e.printStackTrace(); return handleResult; } finally{ try{ bufferedReader.close(); fileWriter.close(); } catch (IOException ignore) {return handleResult;} } return handleResult; } public static void main(String[] args) throws Exception{ /*System.out.println("开始"); List<String> ssss = new ArrayList<String>(); ssss.add("E:\\111\\xmlTest\\a.xml"); ssss.add("E:\\111\\xmlTest\\b.xml"); ssss.add("E:\\111\\xmlTest\\c.xml"); ServerTemplate st = new ServerTemplate(); st.mergeFiles("E:\\111\\xmlTest\\stream2.xml", ssss); System.out.println("结束");*/ } }
时间: 2024-11-07 16:59:53