EBADF(错误的文件数)上的InputStream的I / O [英] EBADF (Bad file number) on inputstream I/O

查看:1065
本文介绍了EBADF(错误的文件数)上的InputStream的I / O的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的错误


  

java.io.IOException异常:读取失败:EBADF(错误的文件数)


在这条线code的:

 ,而((输入= fis.read(缓冲))!= -1){

你认为什么会导致在这一行此EBADF错误的文件编号错误?关于如何解决它的任何想法。我只是想要一个工作流从SD PDF文件阅读和阅读放回SD卡上再次与一个不同的名称。

堆栈跟踪

  07-15 18:47:38.952:W / System.err的(2220):java.io.IOException异常:读取失败:EBADF(错误的文件数)
07-15 18:47:38.962:W / System.err的(2220):在libcore.io.IoBridge.read(IoBridge.java:432)
07-15 18:47:38.962:W / System.err的(2220):在Java.io.FileInputStream.read(FileInputStream.java:179)
07-15 18:47:38.962:W / System.err的(2220):在java.io.InputStream.read(InputStream.java:163)
07-15 18:47:38.962:W / System.err的(2220):在com.example.fileionottext.MainActivity.pdfInAndOut(MainActivity.java:95)
07-15 18:47:38.962:W / System.err的(2220):在com.example.fileionottext.MainActivity $ 1.onClick(MainActivity.java:47)
07-15 18:47:38.962:W / System.err的(2220):在android.view.View.performClick(View.java:3531)
07-15 18:47:38.962:W / System.err的(2220):在android.view.View $ PerformClick.run(View.java:14224)
07-15 18:47:38.962:W / System.err的(2220):在android.os.Handler.handleCallback(Handler.java:605)
07-15 18:47:38.962:W / System.err的(2220):在android.os.Handler.dispatchMessage(Handler.java:92)
07-15 18:47:38.962:W / System.err的(2220):在android.os.Looper.loop(Looper.java:137)
07-15 18:47:38.962:W / System.err的(2220):在android.app.ActivityThread.main(ActivityThread.java:4699)
07-15 18:47:38.962:W / System.err的(2220):在java.lang.reflect.Method.invokeNative(本机方法)
07-15 18:47:38.962:W / System.err的(2220):在java.lang.reflect.Method.invoke(Method.java:511)
07-15 18:47:38.962:W / System.err的(2220):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:790)

在code的其余部分。

 公共无效pdfInAndOut()抛出FileNotFoundException异常,IOException异常
        {
            尝试{
                FOS的FileOutputStream = NULL;
                档案文件2;                文件2 =新的文件(到/ mnt / SD卡/ replacementPdf.pdf);                //字符串文件名=testerPDFCopy.pdf;
                尝试{
                    如果(!file2.exists()){
                        file2.createNewFile();
                    }
                    FOS =新的FileOutputStream(文件2);
                }
                赶上(例外E1){
                    // TODO自动生成catch块
                    e1.printStackTrace();
                }
                FIS的FileInputStream = NULL;
                字节[]缓冲区=新的字节[888192]。                文件FILEIN;
                FILEIN =新的文件(到/ mnt / SD卡/ tempPdf.pdf);                FIS =新的FileInputStream(FILEIN);                INT输入= 0;                而((输入= fis.read(缓冲))!= -1){//<< - 堆栈跟踪错误
                    fos.write(缓冲液,0,输入);
                    Log.i(&所述;&所述;&所述;&所述;&所述;>>>>中,fos2是是;+缓冲区[7]);
                    尝试{
                        fos.flush();
                        fos.close();
                        fis.close();
                    }
                    赶上(IOException异常五){
                        // TODO自动生成catch块
                        e.printStackTrace();
                    }
                }
            }
            textViewOne.setText(将String.valueOf(输入));
        }
        赶上(例外五){
            // TODO自动生成catch块
            e.printStackTrace();
        }
    }
}


解决方案

 ,而((输入= fis.read(缓冲))!= -1){//<< - 堆栈跟踪错误
    fos.write(缓冲液,0,输入);

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

  Log.i(&所述;&所述;&所述;&所述;&所述;>>>>中,fos2是是;+缓冲区[7]);尝试{
    fos.flush();
    fos.close();
    fis.close();
}
赶上(IOException异常五){
    // TODO自动生成catch块
    e.printStackTrace();
}

I have this error

java.io.IOException: read failed: EBADF (Bad file number)

on this line of code:

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

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.

Stack trace

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)

The rest of the 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) { // <<-- 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();
        }
    }
}

解决方案

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

Either must be a }, right? Or the fis.close will be executed, and an error will occur.

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

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

这篇关于EBADF(错误的文件数)上的InputStream的I / O的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆