Java读取文件夹下的文件并将该文件夹下指定的文本文件合并

        // 文件输出路径
        String oF = CLASSIFICATION_PATH + ABNORMAL;

        // 获取文件夹下所有的文件名
        File f = new File(CLASSIFICATION_PATH);
        List fileList = Arrays.asList(f.list());

        // 存放存在的文件名
        int n = 20;
        int i = 0;
        String [] iF = new String[n];

        // 放入字符串数组中
        if (fileList.contains(ABNORMAL_6)) {
            iF[i]=String.valueOf(CLASSIFICATION_PATH + ABNORMAL_6);
            i++;
        }

        if (fileList.contains(ABNORMAL_5)) {
            iF[i]=String.valueOf(CLASSIFICATION_PATH + ABNORMAL_5);
            i++;
        }

        if (fileList.contains(ABNORMAL_4)) {
            iF[i]=String.valueOf(CLASSIFICATION_PATH + ABNORMAL_4);
            i++;
        }

        if (fileList.contains(ABNORMAL_3)) {
            iF[i]=String.valueOf(CLASSIFICATION_PATH + ABNORMAL_3);
            i++;
        }

        if (fileList.contains(ABNORMAL_2)) {
            iF[i]=String.valueOf(CLASSIFICATION_PATH + ABNORMAL_2);
            i++;
        }

        if (fileList.contains(ABNORMAL_1)) {
            iF[i]=String.valueOf(CLASSIFICATION_PATH + ABNORMAL_1);
            i++;
        }

        FileOutputStream output = new FileOutputStream(new File(oF));
        WritableByteChannel targetChannel = output.getChannel();

        try {
            for (int j = 0; j < iF.length; j++) {
                FileInputStream input = new FileInputStream(iF[j]);
                FileChannel inputChannel = input.getChannel();

                inputChannel.transferTo(0, inputChannel.size(), targetChannel);

                inputChannel.close();
                input.close();
            }
        } catch (Exception e) {
            System.out.println();
        }

        targetChannel.close();
        output.close();
    }

你可能感兴趣的:(Java,JAVA,文本文件合并)