io流读取多文件

该博客展示了如何在Java中利用Spring框架和KafkaTemplate读取文件内容,通过递归遍历目录读取所有文件,并将内容发送到Kafka主题。之后,这些消息被接收并写入Elasticsearch(ES)进行存储。Loginfo实体类和接口的提及表明存在数据持久化的操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.写一个测试类

readFile

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring.xml")
public class LogsRead {
    @Autowired
    KafkaTemplate kafkaTemplate;

    @Test
    public void  loadFile() throws InterruptedException, IOException {
       String path="d:/logs/";
       readFile(new File(path));
       Thread.sleep(100000);
        System.out.println("读取结果");
    }

    private void readFile(File file) throws IOException {
        //获取所有文件
        File[] files = file.listFiles();
        if (files!=null&&files.length>0){
            for (File file1 : files) {
                if (file1.isFile()){
                    //读取文件
                    System.out.println("读取文件的名字是:"+file1.getName());
                    BufferedReader reader = new BufferedReader(new FileReader(file1));
                    String str="";
                    while ((str=reader.readLine())!=null){
                        //发送给kafka
                        kafkaTemplate.send("test","import",str);
                    }
                }else {
                    this.readFile(file1);
                }
            }
        }
    }


}

2.将文件写入es

写一个loginfo的实体类

 写一个接口

 将消息接收 写入到es

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值