InputStream writes a large number of NUL files, and inputstream writes nul files.
The most common problems for file writing are chinese character code and the like. Today is not a garbled problem. It is a large number of empty characters that are invisible when opened in notepad. If NodePad ++ is enabled, NUL is displayed.
Question points:
At first, I thought it was a coding problem. I tried several codes and found the problem still. It should be because an exception occurred while writing the file. Please refer to the following two sections of code:
// Problematic code
Try {FileOutputStream fos = new FileOutputStream (filePath); byte [] B = new byte [1024]; while (in. read (B ))! =-1) {fos. write (B); // This problem occurs because the length of 1024 bytes is not set during write, resulting in a large number of null characters written in the last few 1024 bytes.} in. close (); fos. close ();} catch (Exception e) {// TODO Auto-generated catch block e. printStackTrace ();}
// Modified code
Try {FileOutputStream fos = new FileOutputStream (filePath); byte [] B = new byte [1024]; int len =-1; while (len = in. read (B ))! =-1) {fos. write (B, 0, len); // increase the start length and write the actual read length each time. Therefore, no null characters are displayed. } In. close (); fos. close ();} catch (Exception e) {// TODO Auto-generated catch block e. printStackTrace ();}