InputStream writes a large number of NUL files, and inputstream writes nul files.

Source: Internet
Author: User

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 ();}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.