将文件剪切成多个文件(Java)

时间:2016-09-05 13:00:21

标签: java

我将Java Camel用于我的项目(Talend)。 我需要你的帮助来解决这个问题。我有这个文件:

EEASTDE                                                                                                                                                                                    
ELASPDF
ELASQML

我如何以这种方式切入java(在2个文件中)?

EEASTDE                                                                                                                                                                                   
ELASPDF
ELASQHG

public class bean_test implements Processor{


private static final String MDS_ENDPOINT_NAME = "MDSEndpoint";
private static final String EEASTD_HEADER = "EEASTDE";
private static final int EEASTD_HEADER_START_POSITION = 0;
private static final int EEASTD_HEADER_END_POSITION = 7;
private static final String START_OF_FILE_TAG = "D";
private static final String END_OF_FILE_TAG = "F";

@Override
public void process(Exchange exchange) throws Exception {

    ProducerTemplate producerTemplate = exchange.getContext().createProducerTemplate();
    String MDSEndpoint = exchange.getIn().getHeader(MDS_ENDPOINT_NAME, String.class);
    InputStream is = new ByteArrayInputStream(exchange.getIn().getBody(String.class).getBytes());
    extractFromMDS(producerTemplate, is, MDSEndpoint, new HashMap<String, Object>(exchange.getIn().getHeaders()));

}

private void extractFromMDS(ProducerTemplate producerTemplate, InputStream content, String MDSEndpoint, Map<String, Object> headers){
    BufferedReader br = new BufferedReader(new InputStreamReader(content));
    String line;
    try {
        while((line = br.readLine()) != null){
            if(!line.startsWith(START_OF_FILE_TAG) && !line.startsWith(END_OF_FILE_TAG)){
                StringBuilder stringBuilder = null;
                if(line.substring(EEASTD_HEADER_START_POSITION, EEASTD_HEADER_END_POSITION).equals(EEASTD_HEADER)){
                    stringBuilder = new StringBuilder();
                    stringBuilder.append("\n"+(line));

                }
            producerTemplate.sendBodyAndHeaders(MDSEndpoint, stringBuilder.toString(), headers);
        }
    }
    } catch (IOException e) {
        e.printStackTrace();
    }
    finally{
        try {
            if(br != null)br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    }
}

我必须根据头部EEASTDE剪切文件​​。这是一个例子,我可以多次使用标题EEASTDE(3,4,5 ...)。 我想到了HashMap,即arraylist,但我不知道如何编写代码,因为我没有将EEASTDE作为唯一键表示

这是我的测试代码,但它不起作用:

CHelper

有你的想法吗?

提前谢谢。

娜塔莎。

0 个答案:

没有答案