ジェレミー:
私は他の言語でこの質問を見つけましたが、Javaアプリケーションで、この問題への解決策を見つけるためにまだ持っています。
私は、大規模な持っている.txt
数百万レコードを持つファイルを。各レコードはされる/n
区切り。基本的には、テーブルからのデータの単一の列です。目標は、入力ファイルからデータを読み込み、それを分割することです。その後、新しいファイルに分割されたデータを書き込みます。例えば、200万レコードを持つファイルは、(10,000 <含む最後のファイルで。)10,000レコードそれぞれに200個のファイルになります
私は正常に読み、データを分割しています。私が成功した最初のファイルを作成していますし、それが適切に命名されています。
問題が作成された唯一の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
前もって感謝します。
アンドレ・パリ:
あなたは、あなたのためにすべてのそれらの余分な作家を必要としないと、ファイルへの書き込み(てFileWriter)になっライターが呼び出されていません。あなたのためには、このいずれかで置き換えます。
for (String chunk : chunks) {
fileWriter.write(chunk);
}
ヒント:ただコールfileWriter.close()finallyブロックの内側に一度。closeメソッドが自動的にあなたのためのライターをフラッシュします(fileWriter.flushを呼び出す必要はありません())。