如何使用spring集成逐行读取文件

时间:2014-12-16 20:32:23

标签: spring-integration

我是Spring Integration的新手。我需要逐行读取文本文件并使用spring集成打印每一行。任何人都可以有一个例子或任何想法吗?

2 个答案:

答案 0 :(得分:0)

框架当前没有现成的组件,除了文件尾部通道适配器,但需要在文件末尾停止。

您可以编写一个简单的POJO来一次读取一行并使用<inbound-channel-adapter/>调用它。

或者,POJO可以返回一个遍历行的Iterator<splitter/>将调用迭代器,直到消耗掉所有行。

编辑:

<int-file:inbound-channel-adapter directory="/tmp/test" 
        id="filesIn" channel="toSplitter">
    <int:poller fixed-delay="5000" />
</int-file:inbound-channel-adapter>

<int:splitter input-channel="toSplitter" output-channel="logger"
    ref="fileSplitter" method="split" />

<int:logging-channel-adapter id="logger" level="WARN"/>

<bean id="fileSplitter" class="foo.FileSplitter" />

我为FileSplitter in this gist添加了代码。

答案 1 :(得分:0)

您可以使用此代码

xml code = fileCopyApplicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:int="http://www.springframework.org/schema/integration"
    xmlns:file="http://www.springframework.org/schema/integration/file"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/integration
            http://www.springframework.org/schema/integration/spring-integration.xsd
            http://www.springframework.org/schema/integration/file
            http://www.springframework.org/schema/integration/file/spring-integration-file.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context.xsd">

    <context:property-placeholder />

    <file:inbound-channel-adapter id="filesIn"
        directory="E:/usmandata/logs/input/" filter="onlyPropertyFiles"
        auto-startup="true">
        <int:poller id="poller" fixed-delay="500" />
    </file:inbound-channel-adapter>

    <file:inbound-channel-adapter id="filesIn1"
        directory="E:/usmandata/logs/input/" filter="onlyPropertyFiles"
        auto-startup="true">
        <int:poller id="poller" fixed-delay="500" />
    </file:inbound-channel-adapter>


    <int:service-activator input-channel="filesIn"
         ref="handler" />

    <bean id="handler" class="com.javarticles.spring.integration.file.FileHandler" />
    <bean id="onlyPropertyFiles"
        class="org.springframework.integration.file.config.FileListFilterFactoryBean"
        p:filenamePattern="*.log" />
</beans>

FileHandler.java =

package com.javarticles.spring.integration.file;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class FileHandler {
    public void handleFile(File input) throws IOException {
       // System.out.println("Copying file: " + input.getAbsolutePath());


        RandomAccessFile file = new RandomAccessFile(input,"r");

        FileChannel channel = file.getChannel();

        //System.out.println("File size is: " + channel.size());

        ByteBuffer buffer = ByteBuffer.allocate((int) channel.size());

        channel.read(buffer);

        buffer.flip();//Restore buffer to position 0 to read it

        System.out.println("Reading content and printing ... ");

        for (int i = 0; i < channel.size(); i++) {
            System.out.print((char) buffer.get());
        }

        channel.close();
        file.close();
    }
}

SpringIntegrationFileCopyExample.java

package com.javarticles.spring.integration.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringIntegrationFileCopyExample {

    public static void main(String[] args) throws InterruptedException, IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                "fileCopyApplicationContext.xml");

    }

}