输入流I / O上的EBADF(错误文件编号)

时间:2013-07-15 09:55:03

标签: android io fileinputstream

我有这个错误

  

java.io.IOException:读取失败:EBADF(错误的文件编号)

在这行代码上:

 while ((input = fis.read(buffer)) != -1) {

您认为这条线上的EBADF错误文件编号错误是什么?关于如何解决它的任何想法。我只是想要一个工作流来从SD读取PDF文件并再次使用不同的名称读回SD卡。

堆栈跟踪

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)

代码的其余部分

        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) { // <<-- STACK TRACE ERROR
                    fos.write(buffer, 0, input);
                    Log.i("<<<<<>>>>", "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();
        }
    }
}

1 个答案:

答案 0 :(得分:3)

while ((input = fis.read(buffer)) != -1) { // <<-- STACK TRACE ERROR
    fos.write(buffer, 0, input);

要么必须是},对吧?或者fis.close将被执行,并且会发生错误。

Log.i("<<<<<>>>>", "fos2 is is; " + buffer[7]);

try {
    fos.flush();
    fos.close();
    fis.close();
} 
catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}