title: EBADF (Bad file number) on inputstream I/O
body: <p>I have this error</p>
  
  <blockquote>
    <p>java.io.IOException: read failed: EBADF (Bad file number)</p>
  </blockquote>
  
  <p>on this line of code:</p>
  
  <pre><code> while ((input = fis.read(buffer)) != -1) {
  </code></pre>
  
  <p>What do you think would cause this EBADF bad file number error on this line? Any ideas on how to fix it. I just wanted a working stream to read in the PDF file from the SD and read put back on the SD card again with a different name.</p>
  
  <p>Stack trace</p>
  
  <pre><code>07-15 18:47:38.952: W/System.err(2220): java.io.IOException: read failed: EBADF (Bad file number)
  07-15 18:47:38.962: W/System.err(2220): at libcore.io.IoBridge.read(IoBridge.java:432)
  07-15 18:47:38.962: W/System.err(2220): at Java.io.FileInputStream.read(FileInputStream.java:179)
  07-15 18:47:38.962: W/System.err(2220):     at java.io.InputStream.read(InputStream.java:163)
  07-15 18:47:38.962: W/System.err(2220):     at com.example.fileionottext.MainActivity.pdfInAndOut(MainActivity.java:95)
  07-15 18:47:38.962: W/System.err(2220):     at com.example.fileionottext.MainActivity$1.onClick(MainActivity.java:47)
  07-15 18:47:38.962: W/System.err(2220):     at android.view.View.performClick(View.java:3531)
  07-15 18:47:38.962: W/System.err(2220):     at android.view.View$PerformClick.run(View.java:14224)
  07-15 18:47:38.962: W/System.err(2220):     at android.os.Handler.handleCallback(Handler.java:605)
  07-15 18:47:38.962: W/System.err(2220):     at android.os.Handler.dispatchMessage(Handler.java:92)
  07-15 18:47:38.962: W/System.err(2220):     at android.os.Looper.loop(Looper.java:137)
  07-15 18:47:38.962: W/System.err(2220):     at android.app.ActivityThread.main(ActivityThread.java:4699)
  07-15 18:47:38.962: W/System.err(2220):     at java.lang.reflect.Method.invokeNative(Native Method)
  07-15 18:47:38.962: W/System.err(2220):     at java.lang.reflect.Method.invoke(Method.java:511)
  07-15 18:47:38.962: W/System.err(2220):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
  </code></pre>
  
  <p>The rest of the code</p>
  
  <pre><code>        public void pdfInAndOut() throws FileNotFoundException, IOException
          {
              try {
                  FileOutputStream fos = null;
                  File file2;
  
                  file2 = new File("/mnt/sdcard/replacementPdf.pdf");
  
                  // String fileName = "testerPDFCopy.pdf";
                  try {
                      if (!file2.exists()) {
                          file2.createNewFile();
                      }
                      fos = new FileOutputStream(file2);
                  }
                  catch (Exception e1) {
                      // TODO Auto-generated catch block
                      e1.printStackTrace();
                  }
                  FileInputStream fis = null;
                  byte[] buffer = new byte[888192];
  
                  File fileIn;
                  fileIn = new File("/mnt/sdcard/tempPdf.pdf");
  
                  fis = new FileInputStream(fileIn);
  
                  int input = 0;
  
                  while ((input = fis.read(buffer)) != -1) { // &lt;&lt;-- STACK TRACE ERROR
                      fos.write(buffer, 0, input);
                      Log.i("&lt;&lt;&lt;&lt;&lt;&gt;&gt;&gt;&gt;", "fos2 is is; " + buffer[7]);
                      try {
                          fos.flush();
                          fos.close();
                          fis.close();
                      }
                      catch (IOException e) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                      }
                  }
              }
              textViewOne.setText(String.valueOf(input));
          }
          catch (Exception e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
  }
  </code></pre>