title: Java create .docx file from byte array
body: <p>I read a .docx file to byte array, but when I tried to save it again to .docx file, the file couldn't open.</p>
  
  <p>Can anyone explain why?</p>
  
  <pre><code>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);
      }
  }
  </code></pre>
  
  <p>And here is readFile method</p>
  
  <pre><code>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;
  }
  </code></pre>