【java】 IO读写文件示例

package com.test.nb.utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import com.google.common.base.Strings;

public final class FileUtils {

    private FileUtils() {
    }

    public static void main(String[] args) throws Exception{
        readGzTxt();
    }

    public static void readGzTxt()throws Exception{

        final String sourceFilePath = "/Users/test/Downloads/TRADE_YXYRD_20180103.txt.gz1";
        final String TxnIdsFilePath  = "/Users/test/Downloads/txnIds.txt";
        final String targetFilePath = "/Users/test/Downloads/transfer46List.txt";

        if (!new File(TxnIdsFilePath).exists()) {
            System.out.println("读取txnIds.txt文件不存在!!!");
            return;
        }
        System.out.println("读取txnIds.txt文件成功!");

        System.out.println("读取sourceFilePath文件");
        if (!new File(sourceFilePath).exists()) {
            System.out.println("读取sourceFilePath文件不存在!!!");
            return;
        }
        System.out.println("读取sourceFilePath文件成功!");

      if (new File(targetFilePath).exists()) {
            new File(targetFilePath).delete();
        }
        new File(targetFilePath).createNewFile();
        System.out.println("创建targetFilePath文件成功!");
        System.out.println(Strings.repeat("*", 80));


        Set<String> txnIdsSet = new HashSet<String>();
        BufferedReader txnIdReader = new BufferedReader(new FileReader(TxnIdsFilePath));
        BufferedReader reader = new BufferedReader(new FileReader(sourceFilePath));
        BufferedWriter writer = new BufferedWriter(new FileWriter(targetFilePath));
        try {
            String txnId  = null;
            while ( (txnId = txnIdReader.readLine()) != null ) {
                txnIdsSet.add(txnId);
            }
            System.out.println("读取txnIds.txt文件,txnIdsSet size:" + txnIdsSet.size());

            String lineContent = null;
            String lineStr = null;
            while ( (lineContent = reader.readLine()) != null) {
                lineStr = lineContent;
                String[] data = lineContent.split("&;");
                if (txnIdsSet.contains(data[0])) {
                    System.out.println("写入文件:" + lineStr);
                    writer.write(lineStr);
                    writer.newLine();
                }
            }

            writer.flush();
            System.out.println("执行完毕!");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                writer.close();
                reader.close();
                txnIdReader.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

门主冬七

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值