import java.io.BufferedReader;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class SumTextUtil {
    public static String newFile = "";
    public static String filePath = "";
    public static String code = "UTF-8";
    public static void sumText(String textName, int beginIndex, int endIndex) {
        File file = new File(newFile);
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            if (file.exists()) {
                file.createNewFile();
            }
            bw = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(file), code));
            for (int i = beginIndex; i <= endIndex; i++) {
                String fileName = textName + "00000" + i;
                fileName = fileName.substring(fileName.length() - 5) + ".txt";
                String fileTempPath = filePath + fileName;
                File file2 = new File(fileTempPath);
                if (!file2.exists()) {
                    continue;
                }
                br = new BufferedReader(new InputStreamReader(
                        new FileInputStream(file2)));
                String top = "";
                while ((top = br.readLine()) != null) {
                    bw.write(top);
                    bw.write("\r");
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // release the resource
            try {
                if (null != br) {
                    br.close();
                }
                if (null != bw) {
                    bw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        newFile = "D:\\Stanny\\sum.txt";
        filePath = "D:\\Stanny\\219139\\";
        String textName = "";
        int beginIndex = 1;
        int endIndex = 400;
        sumText(textName, beginIndex, endIndex);
    }
}