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();
}
}
}
}
【java】 IO读写文件示例
最新推荐文章于 2024-08-17 09:11:50 发布