Greetings , I get huge number of records from database and write into a file.I was wondering what the best way to write huge files. (1Gb - 10Gb).
Currently I am using BufferedWriter
BufferedWriter mbrWriter=new BufferedWriter(new FileWriter(memberCSV));
while(done){
//do writings
}
mbrWriter.close();
If you really insist using Java for this, then the best way would be to write immediately as soon as the data comes in and thus not to collect all the data from ResultSet
into Java's memory first. You would need at least that much of free memory in Java otherwise.
Thus, do e.g.
while (resultSet.next()) {
writer.write(resultSet.getString("columnname"));
// ...
}
That said, most decent DB's ships with builtin export-to-CSV capabilities which are undoubtely way more efficient than you could ever do in Java. You didn't mention which one you're using, but if it was for example MySQL, you could have used the LOAD DATA INFILE
for this. Just refer the DB-specific documentation. Hope this gives new insights.