Java create .docx file from byte array -
i read .docx file byte array, when tried save again .docx file, file couldn't open.
can explain why?
public static void main(string[] args) throws ioexception { byte[] data = readfile("test.docx"); system.out.println(data.length); try (fileoutputstream fos = new fileoutputstream("testcopy.docx")) { fos.write(data); } } and here readfile method
public static byte[] readfile(string filepath) throws filenotfoundexception, ioexception { file file = new file(filepath); byte[] bytes = new byte[(int) file.length()]; try (datainputstream datainputstream = new datainputstream(new bufferedinputstream(new fileinputstream(filepath)))) { datainputstream.readfully(bytes); } return bytes; }
you should difference between datainputstream , fileinputstream.
try using fileinputstream instead of datainputstream , see how works.
try (fileinputstream fileinputstream = new fileinputstream(filepath)) { fileinputstream.read(bytes); }
Comments
Post a Comment