I have to fetch 500K rows from the database and write that data into the file , Means perform the I/O operation . I have done for two steps .
- Write each row one by one into the file .
- Make the chunk of those rows .Append those rows in StringBuffer and then print it . This one will be better but is there any way that File I/O can make the buffer on it’s own without using StringBuffer . As this String Buffer is taking more time (Internally it use Arrays.copyOf)
Please suggest me on this
Please find the code attached with this
public void fetchDataWithChunkSpace(Connection con)throws Exception
{
String query ="select * from lis.testxml ";
long startTime=new Date().getMinutes();
PreparedStatement stmt = con.prepareStatement(query);
ResultSet rs = stmt.executeQuery();
PrintWriter pw = new PrintWriter(new File("c:\\test_chunkSpace.xml"));
pw.write("<?xml version='1.0' encoding='UTF-8'?>");
StringBuffer sb = new StringBuffer();
String t=null;
long count =0;
while(rs.next())
{ count++;
t = rs.getString("xmltest");
sb = sb.append(t);
if(count%100==0)
{
pw.write(sb.toString());
sb = new StringBuffer();
System.out.println(count);
}
}
pw.write("\n");
pw.write("</EndTag>");
pw.close();
stmt.close();
con.close();
long endTime=new Date().getMinutes();
System.out.println("chunkSpace ---> " + (endTime-startTime));
}
Look at BufferedOutputStream or BufferedWriter.
Also consider that StringBuffer is thread-safe class with synchronyzed methods unlike StringBuilder.
Update.
Try this code: