Jer*_*emy 2 java collections stringwriter bufferedwriter java-8
我在其他语言中发现了这个问题,但还没有在java应用程序中找到解决这个问题的方法.
我有一个.txt包含数百万条记录的大文件.每条记录都是/n分隔的.基本上它是来自表的单列数据.目标是从输入文件中读取数据并对其进行分区.然后将分区数据写入新文件.例如,一个包含200万条记录的文件将成为200个文件,每个文件有10,000条记录(最后一个文件包含<10,000个.)
我成功地读取和分区数据.我成功创建了第一个文件,并且正确命名.
问题是只创建了一个文件,它是空的.代码编译和运行没有错误或异常.
我的代码如下:
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();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
输入文件示例:
230449
235659
295377
329921
348526
359836
361447
384723
396202
571490
Run Code Online (Sandbox Code Playgroud)
先感谢您.
你不需要你的所有那些额外的编写器,并且没有调用写入文件(fileWriter)的文件.用这个替换你的:
for (String chunk : chunks) {
fileWriter.write(chunk);
}
Run Code Online (Sandbox Code Playgroud)
提示:只需在finally块中调用fileWriter.close()一次.close方法会自动刷新writer(不需要调用fileWriter.flush()).
| 归档时间: |
|
| 查看次数: |
908 次 |
| 最近记录: |