花开不停 花开不停
首页
  • 追忆杂谈
  • 书信往来
  • 旅行记录
  • 文定之喜
  • 黄金屋-颜如玉
  • 程序化广告
  • 禅茶一味
  • 随写编年
  • 家人物语
  • 追忆青春
  • 大千世界
  • Shell
  • Java
  • Spark
  • Hadoop
  • ClickHouse
  • MySQL
  • PostgreSQL
  • MongoDB
  • 调度器
  • Zookeeper
  • Kafka
  • Flume
  • 学习周刊
关于
  • 分类
  • 标签
  • 归档
开往 (opens new window)

花开不停

此心光明,亦复何言
首页
  • 追忆杂谈
  • 书信往来
  • 旅行记录
  • 文定之喜
  • 黄金屋-颜如玉
  • 程序化广告
  • 禅茶一味
  • 随写编年
  • 家人物语
  • 追忆青春
  • 大千世界
  • Shell
  • Java
  • Spark
  • Hadoop
  • ClickHouse
  • MySQL
  • PostgreSQL
  • MongoDB
  • 调度器
  • Zookeeper
  • Kafka
  • Flume
  • 学习周刊
关于
  • 分类
  • 标签
  • 归档
开往 (opens new window)
  • Shell编程

  • Java编程笔记

    • 开发技巧

      • go日常开发代码片段
      • 根据FileName获取 要求格式的子File列表对象
      • 计算两个坐标之间的距离
      • 获取配置文件信息
      • Java加密解密
      • RocksDB应用
      • Java IO读写文件
        • 1. 依赖
        • 2. InputStream
        • 2. OutputStream
          • 2.1.
  • Spark

  • Hadoop

  • ClickHouse

  • MySQL

  • PostgreSQL

  • MongoDB

  • 调度器

  • Zookeeper

  • Kafka

  • Flume

  • 编程世界
  • Java编程笔记
  • 开发技巧
花开不停
2024-09-05
目录

Java IO读写文件原创

# 1. 依赖

    <dependencies>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>${commons.lang3.version}</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>${commons.io.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-compress</artifactId>
            <version>${commons.compress.version}</version>
        </dependency>
    </dependencies>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# 2. InputStream

private static void readExecuteData(File inputFile) throws IOException {
    //读gz压缩文件
    GzipCompressorInputStream inputStream = new GzipCompressorInputStream(new FileInputStream(inputFile));
    //读普通文件
    //BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(inputFile));
    try (LineIterator iterator = IOUtils.lineIterator(inputStream, StandardCharsets.UTF_8)) {
        while (iterator.hasNext()) {
            String line = iterator.nextLine();
            System.out.println(line);
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12

# 2. OutputStream

# 2.1.

    
    static Map<String, OutputStream> outputStreamMap = new ConcurrentHashMap<>();

    private synchronized void write(String path, String filePath, List<String> records, HashMap<String, Long> outputRowRecord, HashMap<String, Long> outputSizeRecord) throws Exception {
        IOUtils.writeLines(records, null, this.getOutputStream(filePath), StandardCharsets.UTF_8);
    }

    private OutputStream getOutputStream(String path) throws Exception {
        OutputStream outputStream;
        if (!outputStreamMap.containsKey(path)) {
            outputStream = new BufferedOutputStream(FileUtils.openOutputStream(new File(path), true));
            outputStreamMap.put(path, outputStream);
        } else {
            outputStream = outputStreamMap.get(path);
        }
        return outputStream;
    }

    public void closeOutputStreams() {
        outputStreamMap.forEach((path, outputStream) -> {
            try {
                outputStream.close();
            } catch (IOException e) {
                logger.error(e.getMessage(), e);
            }
        });
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

← RocksDB应用 Spark On Yarn 部署→

最近更新
01
2025-05-26当我意识到我变得自私、暴躁、情绪不受控制 原创
05-26
02
clickhouse版本升级的语法变动21.8.9.1至23.8.9.1 原创
04-22
03
2025-03-28拍婚纱照 原创
04-02
更多文章>
Theme by Vdoing | Copyright © 2023-2025 | 京ICP备2023013437号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式