Java将字符串列表写入文件,但该文件为空
我已经在其他语言中发现了这个问题,但是还没有在java应用程序中找到解决这个问题的方法Java将字符串列表写入文件,但该文件为空,java,collections,java-8,bufferedwriter,stringwriter,Java,Collections,Java 8,Bufferedwriter,Stringwriter,我已经在其他语言中发现了这个问题,但是还没有在java应用程序中找到解决这个问题的方法 我有一个包含数百万条记录的大型.txt文件。每个记录都有/n分隔符。基本上,它是表中的一列数据。目标是从输入文件中读取数据并对其进行分区。然后将分区数据写入一个新文件。例如,一个有200万条记录的文件将变成200个文件,每个文件有10000条记录(最后一个文件包含aStringWriter不是用于写入字符串,而是用于将写入字符串您的For中不需要所有额外的写入程序,而应该写入的写入程序(fileWriter)
我有一个包含数百万条记录的大型
.txt
文件。每个记录都有/n
分隔符。基本上,它是表中的一列数据。目标是从输入文件中读取数据并对其进行分区。然后将分区数据写入一个新文件。例如,一个有200万条记录的文件将变成200个文件,每个文件有10000条记录(最后一个文件包含aStringWriter
不是用于写入字符串,而是用于将写入字符串您的For中不需要所有额外的写入程序,而应该写入的写入程序(fileWriter)未调用到该文件的。
将的替换为以下内容:
for (String chunk : chunks) {
fileWriter.write(chunk);
}
提示:只要在finally块内调用fileWriter.close()。close方法将自动为您刷新writer(无需调用fileWriter.flush())。您可以使用
Path file = Paths.get(filename);
Files.write(file, chunks, Charset.forName("UTF-8"));
并且,您应该在循环之前放置count=0,否则它将始终为0
总的来说是这样的:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
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;
// 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(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 = 10;
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 the file is 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;
Path file = Paths.get(filename);
Files.write(file, chunks, Charset.forName("UTF-8"));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// Closing the resources
System.out.println("Finished");
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
导入java.io.BufferedReader;
导入java.io.FileReader;
导入java.io.IOException;
导入java.nio.charset.charset;
导入java.nio.file.Files;
导入java.nio.file.Path;
导入java.nio.file.path;
导入java.util.ArrayList;
导入java.util.Collection;
导入java.util.List;
导入java.util.concurrent.AtomicInteger;
导入java.util.stream.collector;
公共类ChunkTextFile{
私有静态最终字符串inputFilename=“inputFile.txt”;
公共静态void main(字符串[]args){
BufferedReader reader=null;
//创建ArrayList对象以保存输入文件的行
列表行=新的ArrayList();
试一试{
//创建BufferedReader对象以读取输入文件
reader=new BufferedReader(new FileReader(inputFilename));
//逐个读取输入文件的所有行并将它们添加到ArrayList中
字符串currentLine=reader.readLine();
while(currentLine!=null){
行。添加(当前行);
currentLine=reader.readLine();
}
//文件读取结束。
//将ArrayList划分为一组较小的列表
最终AtomicInteger计数器=新的AtomicInteger(0);
最终整数大小=10;
集合已分区=行。流()
.collect(Collectors.groupingBy(it->counter.getAndIncrement()/size)).values();
//打印分区。每个分区将写入一个文件。
//测试确认分区工作正常。
partitioned.forEach(System.out::println);
//遍历集合并为列表对象创建一个文件。
//测试确认文件已创建并正确命名。
整数计数=0;
for(列表块:分区){
//准备新的递增文件名。
String outputFile=“批处理项目文件”;
字符串txt=“.txt”;
计数++;
字符串文件名=outputFile+count+txt;
Path file=Path.get(文件名);
file.write(文件、块、字符集.forName(“UTF-8”);
}
}捕获(IOE异常){
e、 printStackTrace();
}最后{
//关闭资源
系统输出打印项次(“完成”);
试一试{
if(读卡器!=null){
reader.close();
}
}捕获(IOE异常){
e、 printStackTrace();
}
}
}
}
我接受上述答案,因为它解决了我的问题,但我想为找到此问题和答案的任何人扩展它。为了使创建的文件与输入文件的格式相同(以换行符分隔),我使用接受的答案更改了代码,并添加了System.lineSeparator()
最终的解决方案是这样的
fileWriter.write(chunk+System.lineSeparator());
再次感谢您的快速回复
这是工作版本。我建议注释掉或删除partitioned.forEach(System.out::println);
以提高性能
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;
// 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.
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 the file is 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) {
// Prepare list of strings to be written to new file.
// Write each item number to file.
fileWriter.write(chunk + System.lineSeparator());
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// Closing the resources
System.out.println("Finished");
try {
if (reader != null) {
reader.close();
}
if (fileWriter != null) {
fileWriter.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
导入java.io.BufferedReader;
导入java.io.BufferedWriter;
导入java.io.FileReader;
导入java.io.FileWriter;
导入java.io.IOException;
导入java.io.StringWriter;
导入java.util.ArrayList;
导入java.util.Collection;
导入java.util.List;
导入java.util.concurrent.AtomicInteger;
导入java.util.stream.collector;
公共类ChunkTextFile{
私有静态最终字符串inputFilename=“inputFile.txt”;
公共静态void main(字符串[]args){
BufferedReader reader=null;
BufferedWriter fileWriter=null;
//创建ArrayList对象以保存输入文件的行
列表行=新的ArrayList();
试一试{
//创建BufferedReader对象以读取输入文件
reader=新的BufferedReader(新的文件读取器(“src”+“/”+inputFilename));
//逐个读取输入文件的所有行并将它们添加到ArrayList中
字符串currentLine=reader.readLine();
while(currentLine!=null){
行。添加(当前行);
currentLine=reader.readLine();
}
//文件读取结束。
最终AtomicInteger计数器=新的AtomicInteger(0);
最终整数大小=10000;
集合已分区=行。流()
.collect(Collectors.groupingBy(it->counter.getAndIncrement()/size)).values();
//打印分区。每个分区将写入一个文件。
//测试确认分区工作正常。
partitioned.forEach(System.out::println);
//遍历集合并为列表对象创建一个文件。
//测试确认文件已创建并正确命名。
整数计数=0;
对于(列表块:p
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
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;
// 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(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 = 10;
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 the file is 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;
Path file = Paths.get(filename);
Files.write(file, chunks, Charset.forName("UTF-8"));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// Closing the resources
System.out.println("Finished");
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
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;
// 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.
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 the file is 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) {
// Prepare list of strings to be written to new file.
// Write each item number to file.
fileWriter.write(chunk + System.lineSeparator());
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// Closing the resources
System.out.println("Finished");
try {
if (reader != null) {
reader.close();
}
if (fileWriter != null) {
fileWriter.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
fileWriter = new BufferedWriter(new FileWriter("src" + "//" + outputFile));
fileWriter = new BufferedWriter(new FileWriter(filename));
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class ChunkTextFile {
private static final String inputFilename = "inputFile.txt";
public static void main(String[] args) {
final int size = 10000;
try(BufferedReader reader=Files.newBufferedReader(Paths.get("src", inputFilename))) {
String line = reader.readLine();
for(int count = 0; line != null; count++) {
try(BufferedWriter writer = Files.newBufferedWriter(
Paths.get("batched_items_file_" + count + ".txt"))) {
for(int i = 0; i < size && line != null; i++) {
writer.write(line);
writer.newLine();
line = reader.readLine();
}
}
}
}
catch(IOException ex) {
ex.printStackTrace();
}
}
}