3

我有这个错误

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

1 回答 1

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();
}
于 2013-07-15T10:04:16.113 回答