I am trying to build a servlet that parses form input and creates a .csv file from them, but the bufferedWriter object truncates a lot of characters for no (apparent to me) reason.
String filepath = getServletContext().getRealPath("\\") + "temp";
String filename = "csv"+dateFormat.format(date)+".csv";
File file = new File(filepath + filename);
file.createNewFile();
BufferedWriter fwrite = new BufferedWriter(new FileWriter(file));
for(int i=0; i<list.size(); i++) {
String[] dataEntry = list.get(i);
for (int j=0; j<dataEntry.length;j++)
fwrite.write("test1-2");
//fwrite.append(dataEntry[j]+";");
fwrite.newLine();
}
fwrite.close();
URI fileUri = file.toURI();
stream = response.getOutputStream();
response.setContentType("text/csv");
response.addHeader("Content-Disposition", "attachment; filename="
+ filename);
URLConnection urlConn = fileUri.toURL().openConnection();
response.setContentLength((int) urlConn.getContentLength());
buf = new BufferedInputStream(urlConn.getInputStream());
while (buf.read() != -1)
stream.write(buf.read());
} finally {
if (stream != null)
stream.close();
if (buf != null)
buf.close();
}
}
Sorry if the code is a bit slapdash. My current output when writing the "test1-2" string for each entry is
et-ts12et-ts12et-ts12ÿ
any further comments on the code itself would be appreciated I'm just experimenting with stuff i find on the net, I have no actual best practices points of reference.