title: How to check whether the file is binary?
body: <p>I wrote the following method to see whether particular file contains ASCII text characters only or control characters in addition to that. Could you glance at this code, suggest improvements and point out oversights?</p>
  
  <p>The logic is as follows: "If first 500 bytes of a file contain 5 or more Control characters - report it as binary file"</p>
  
  <p>thank you.</p>
  
  <pre><code>public boolean isAsciiText(String fileName) throws IOException {
  
      InputStream in = new FileInputStream(fileName);
      byte[] bytes = new byte[500];
  
      in.read(bytes, 0, bytes.length);
      int x = 0;
      short bin = 0;
  
      for (byte thisByte : bytes) {
          char it = (char) thisByte;
          if (!Character.isWhitespace(it) &amp;&amp; Character.isISOControl(it)) {
  
              bin++;
          }
          if (bin &gt;= 5) {
              return false;
          }
          x++;
      }
      in.close();
      return true;
  }
  </code></pre>