java系列之:FileInputStream读取文件ByteArrayOutputStream输出文件内容

主要实现的功能:

  • FileInputStream读取配置文件
  • ByteArrayOutputStream输出文件内容
  • toByteArray创建一个新分配的 byte 数组,大小是此输出流的当前大小,并且缓冲区的有效内容已复制到该数组中

一、配置文件

# source, channel and sink statement
agent-tetestkafka.sources = source1
agent-tetestkafka.channels = channel1
agent-tetestkafka.sinks = sink1

# link source to sink via channel
agent-tetestkafka.sources.source1.channels = channel1
agent-tetestkafka.sinks.sink1.channel = channel1

二、详细实现代码

import java.io.*;

public class commonTest {
    public static void main(String[] args) {
        InputStream input = null;
        ByteArrayOutputStream bytestream = null;
        byte[] data = null;

        String properties = "/data/resources/flume/flume.conf";
        try{
            input = new FileInputStream(properties);
            bytestream = new ByteArrayOutputStream();
            int ch;
            while((ch = input.read()) != -1){
                bytestream.write(ch);
            }
            data = bytestream.toByteArray();
            System.out.println(new String(data));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try{
                bytestream.close();
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

输出如下所示:

# source, channel and sink statement
agent-tetestkafka.sources = source1
agent-tetestkafka.channels = channel1
agent-tetestkafka.sinks = sink1

# link source to sink via channel
agent-tetestkafka.sources.source1.channels = channel1
agent-tetestkafka.sinks.sink1.channel = channel1
Logo

Kafka开源项目指南提供详尽教程,助开发者掌握其架构、配置和使用,实现高效数据流管理和实时处理。它高性能、可扩展,适合日志收集和实时数据处理,通过持久化保障数据安全,是企业大数据生态系统的核心。

更多推荐