我在其他语言中发现了这个问题,但还没有在 java 应用程序中找到解决这个问题的方法。
我有一个.txt
包含数百万条记录的大文件。每条记录都是/n
分隔的。基本上它是表中的一列数据。目标是从输入文件中读取数据并对其进行分区。然后将分区数据写入新文件。例如,一个有 200 万条记录的文件将变成 200 个文件,每个文件有 10,000 条记录(最后一个文件包含 <10,000 条记录)。
我正在成功读取和分区数据。我成功地创建了第一个文件并且它被正确命名。
问题是只创建了 1 个文件并且它是空的。代码按原样编译和运行,没有错误或异常。
我的代码如下:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
public class ChunkTextFile {
private static final String inputFilename = "inputFile.txt";
public static void main(String[] args) {
BufferedReader reader = null;
BufferedWriter fileWriter = null;
BufferedWriter lineWriter = null;
StringWriter stringWriter = null;
// Create an ArrayList object to hold the lines of input file
List<String> lines = new ArrayList<String>();
try {
// Creating BufferedReader object to read the input file
reader = new BufferedReader(new FileReader("src" + "//" + inputFilename));
// Reading all the lines of input file one by one and adding them into ArrayList
String currentLine = reader.readLine();
while (currentLine != null) {
lines.add(currentLine);
currentLine = reader.readLine();
}
// End of file read.
//Partition ArrayList into a collection of smaller Lists<String>
final AtomicInteger counter = new AtomicInteger(0);
final int size = 10000;
Collection<List<String>> partitioned = lines.stream()
.collect(Collectors.groupingBy(it -> counter.getAndIncrement() / size)).values();
//Printing partitions. Each partition will be written to a file.
//Testing confirms the partitioning works correctly.
partitioned.forEach(System.out::println);
//Iterate through the Collections and create a file for List<String> object.
//Testing confirms that multiple files are created and properly named.
Integer count = 0;
for (List<String> chunks : partitioned) {
// Prepare new incremented file name.
String outputFile = "batched_items_file_";
String txt = ".txt";
count++;
String filename = outputFile + count + txt;
// Write file to directory.
fileWriter = new BufferedWriter(new FileWriter("src" + "//" + outputFile));
fileWriter = new BufferedWriter(new FileWriter(filename));
//Iterate through the List of Strings and write each String to the file.
//Writing is not successful. Only 1 file is created and it is empty.
for (String chunk : chunks) {
stringWriter = new StringWriter();
lineWriter = new BufferedWriter(stringWriter);
// Prepare list of strings to be written to new file.
// Write each item number to file.
lineWriter.write(chunk);
lineWriter.flush();
}
lineWriter.close(); // <- flush the BufferedWriter
fileWriter.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// Closing the resources
System.out.println("Finished");
try {
if (reader != null) {
reader.close();
}
if (fileWriter != null) {
fileWriter.close();
}
if (stringWriter != null) {
stringWriter.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
输入文件示例:
230449
235659
295377
329921
348526
359836
361447
384723
396202
571490
先感谢您。