I'm having trouble generating files (both text and binary). The text files are not coming out with the correct encoding, and the binaries are corrupted. I'm using java 5 and Struts.
The code to generate the files is
final byte[] array = (byte[]) hashMap.get("BINARY_FILE");
if (array != null && array.length > 0) {
bis = new BufferedInputStream(new ByteArrayInputStream(array));
}
file = new File(tempDir.getAbsolutePath() + "\" + path + fileName);
final FileOutputStream fos = new FileOutputStream(file);
if (!file.exists()) {
file.createNewFile();
}
baos = new ByteArrayOutputStream();
int c;
if (size >= SIZE_LIMIT) {
final BufferedOutputStream bos = new BufferedOutputStream(fos);
for (int i = 0; i < size; i++) {
bos.write(i);
}
bos.flush();
bos.close();
} else {
while ((c = bis.read()) != -1) {
fos.write(array);
}
}
fos.flush();
fos.close();