gpt4 book ai didi

Java将字符串列表写入文件,但文件为空

转载 作者:行者123 更新时间:2023-12-01 14:10:50 24 4
gpt4 key购买 nike

我在其他语言中发现了这个问题,但还没有在 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

提前谢谢你。

最佳答案

您的 for 中不需要所有这些额外的写入器,并且不会调用应该写入 (fileWriter) 到文件的写入器。用这个替换你的 for:

for (String chunk : chunks) {
fileWriter.write(chunk);
}

提示:只需在 finally block 中调用 fileWriter.close() 一次。 close 方法会自动为您刷新 writer(无需调用 fileWriter.flush())。

关于Java将字符串列表写入文件,但文件为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54830852/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com