I have a code that creates xml.
public void createXML(InputStream in, String fileName) throws IOException {
baos = new ByteArrayOutputStream();
byte[] buf = new byte[BUF_SIZE];
int readNum = 0;
Writer writer = new BufferedWriter(new OutputStreamWriter(FileUtil.getOutputStream(fileName, FileUtil.HDD)));
writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
writer.write("\t<" + imageFileName + ">\r\n");
writer.write("\t\t");
try {
while ((readNum = in.read(buf)) >= 0) {
baos.write(buf, 0, readNum);
writer.write(baos.toString());
baos.reset();
}
}
finally {
if (in != null) {
in.close();
baos.close();
}
}
writer.write("\r\n\t<" + imageFileName + ">");
writer.close();
baos = null;
buf = null;
}
I want to create this xml into multiple parts (maximum of 500kb each). How can I do this? Is there any way for me to determine that the created file is already 500kb and write the remaining data to a different file?
I used this but the image after decoding the base64 string, the image produced is corrupted on the portion where it was cut.
for(int i = 1; i <= numberOfFiles; i++){
baos = new ByteArrayOutputStream();
String filePartName = fileName + ".part" + i + ".xml";
Writer writer = new BufferedWriter(new OutputStreamWriter(FileUtil.getOutputStream(filePartName, FileUtil.HDD)));
writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
writer.write("\t<" + filePartName + ">\r\n");
writer.write("\t\t");
int size = 0;
while ((readNum = in.read(buf)) >= 0 && size < 512000) {
baos.write(buf, 0, readNum);
size = size + readNum;
writer.write(baos.toString());
baos.reset();
}
writer.write("\r\n\t<" + filePartName + ">");
writer.close();
baos.close();
}
}
in.close();
If you keep a sum total of readNum
that is in
while ((readNum = in.read(buf)) >= 0)
then you can test for its value and create new files when ever you want.