本文介绍了EBADF(错误的文件数)上的InputStream的I / O的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的错误
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!