title: Why is FileInputStream read() method wrongly reading question mark (ascii: 63) when put into infinite loop?
body: <p>There are some similar questions on the site but all have been used in different scenario. So, I'm asking it here:</p>
  
  <pre><code>package Assign6B;
  
  import java.io.FileInputStream;
  import java.io.FileOutputStream;
  import java.io.IOException;
  
  public class FileOpsDemo {
      public static void main(String[] args) throws IOException 
      {
  
          FileInputStream inputFile = null;
          FileOutputStream outputFile = null;
  
          try
          {
              inputFile = new FileInputStream("s:/inputFile.txt");
              outputFile = new FileOutputStream("s:/outputFile.txt");
              char c;
              while(( c = (char) inputFile.read()) != -1)
              {
                  System.out.println((char)c);
                  outputFile.write(c);
              }
  
              System.out.println("File transfer complete!");
          }
  
          finally
          {
              if (inputFile != null)
                  inputFile.close();
  
              if (outputFile != null)
                  outputFile.close();
          }
      }
  }
  </code></pre>
  
  <p>This is my code. In the while loop condition, first I had set it to typecast the int output by read() into a char. The result was that it went into an infinite loop with all characters being converted to '?' (ascii: 63). Then I realized my mistake of char conversion and changed it.</p>
  
  <p>But when I changed my while condition to "=-2" (without the char conversion) (this condition will never occur and hence put it into infinite loop). Here too, even if there is no char conversion the first (say, 10) valid characters of the file are still being converted into '?'. (After it reaches EOF, all invalid chars become '?' - I'm assuming this is given).</p>
  
  <p>Why is this happening? At least the valid characters of the file should be read properly until it encounters the EOF and starts feeding on invalid chars!</p>