全部产品

java_io_FileNotFoundException

更新时间:2017-06-07 13:26:11

问题分析:

文件未找到或无权限时抛出此类异常。

解决方案文件未找到或无权限时,请确认文件路径文件存在,添加所需访问权限,确保文件存在的情况下进行读写,并增加try catch块做异常处理。

代码示例:

增加读写外部存储上的文件权限:

  1. <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
  2. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

创建文件,增加读写权限并catch exception。

  1. File extDir = Environment.getExternalStorageDirectory();
  2. String filename = "file.txt";
  3. File fullFilename = new File(extDir, filename);
  4. try {
  5. if(!fullFilename.exists()){
  6. fullFilename.createNewFile();
  7. fullFilename.setWritable(Boolean.TRUE);
  8. // 写文件操作
  9. }
  10. else{
  11. // 读文件操作
  12. }
  13. } catch (IOException e) {
  14. e.printStackTrace();
  15. }

示例:

  1. java.io.FileNotFoundException: /proc/mtprof/status: open failed: ENOENT (No such file or directory)
  2. at libcore.io.IoBridge.open(IoBridge.java:496)
  3. at java.io.FileInputStream.<init>(FileInputStream.java:76)
  4. at java.io.FileInputStream.<init>(FileInputStream.java:103)
  5. at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1494)
  6. at android.os.Handler.dispatchMessage(Handler.java:111)
  7. at android.os.Looper.loop(Looper.java:194)
  8. at android.app.ActivityThread.main(ActivityThread.java:5692)
  9. at java.lang.reflect.Method.invoke(Native Method)
  10. at java.lang.reflect.Method.invoke(Method.java:372)
  11. at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
  12. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
  13. Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
  14. at libcore.io.Posix.open(Native Method)
  15. at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
  16. at libcore.io.IoBridge.open(IoBridge.java:482)
  17. ... 10 more

异常导致应用无法启动。日志如下:

  1. 10-20 09:43:08.073 820 820 E WifiTrafficPoller: TRAFFIC_STATS_POLL true Token 1138 num clients 12
  2. 10-20 09:43:08.074 820 820 E WifiTrafficPoller: packet count Tx=237196 Rx=493756
  3. 10-20 09:43:08.365 820 842 W BroadcastQueue: Skipping deliver [background] BroadcastRecord{9a089ad u-1 android.net.conn.CONNECTIVITY_CHANGE} to ReceiverList{2c1b84e2 23189 (unknown name)/2000/u-1 remote:2663fdd7}: process crashing
  4. 10-20 09:43:08.401 23079 23079 E ActivityThread: mtprof entry can not be found
  5. 10-20 09:43:08.401 23079 23079 E ActivityThread: java.io.FileNotFoundException: /proc/mtprof/status: open failed: ENOENT (No such file or directory)
  6. 10-20 09:43:08.401 23079 23079 E ActivityThread: at libcore.io.IoBridge.open(IoBridge.java:496)
  7. 10-20 09:43:08.401 23079 23079 E ActivityThread: at java.io.FileInputStream.<init>(FileInputStream.java:76)
  8. 10-20 09:43:08.401 23079 23079 E ActivityThread: at java.io.FileInputStream.<init>(FileInputStream.java:103)
  9. 10-20 09:43:08.401 23079 23079 E ActivityThread: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1494)
  10. 10-20 09:43:08.401 23079 23079 E ActivityThread: at android.os.Handler.dispatchMessage(Handler.java:111)
  11. 10-20 09:43:08.401 23079 23079 E ActivityThread: at android.os.Looper.loop(Looper.java:194)
  12. 10-20 09:43:08.401 23079 23079 E ActivityThread: at android.app.ActivityThread.main(ActivityThread.java:5692)
  13. 10-20 09:43:08.401 23079 23079 E ActivityThread: at java.lang.reflect.Method.invoke(Native Method)
  14. 10-20 09:43:08.401 23079 23079 E ActivityThread: at java.lang.reflect.Method.invoke(Method.java:372)
  15. 10-20 09:43:08.401 23079 23079 E ActivityThread: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
  16. 10-20 09:43:08.401 23079 23079 E ActivityThread: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
  17. 10-20 09:43:08.401 23079 23079 E ActivityThread: Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
  18. 10-20 09:43:08.401 23079 23079 E ActivityThread: at libcore.io.Posix.open(Native Method)
  19. 10-20 09:43:08.401 23079 23079 E ActivityThread: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
  20. 10-20 09:43:08.401 23079 23079 E ActivityThread: at libcore.io.IoBridge.open(IoBridge.java:482)
  21. 10-20 09:43:08.401 23079 23079 E ActivityThread: ... 10 more
  22. 10-20 09:43:08.415 23079 23079 W Launcher: setApplicationContext called twice!
  23. 10-20 09:43:08.422 23079 23079 E Fatal exception: :java.lang.RuntimeException: Unable to start activity ComponentInfo{}: java.io.IOException: broken file descriptor
  24. 10-20 09:43:08.422 23079 23079 W System.err: java.lang.RuntimeException: Unable to start activity ComponentInfo{}: java.io.IOException: broken file descriptor
  25. 10-20 09:43:08.422 23079 23079 W System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2581)
  26. 10-20 09:43:08.422 23079 23079 W System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2656)
  27. 10-20 09:43:08.422 23079 23079 W System.err: at android.app.ActivityThread.access$800(ActivityThread.java:178)
  28. 10-20 09:43:08.423 23079 23079 W System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1512)
  29. 10-20 09:43:08.423 23079 23079 W System.err: at android.os.Handler.dispatchMessage(Handler.java:111)
  30. 10-20 09:43:08.423 23079 23079 W System.err: at android.os.Looper.loop(Looper.java:194)
  31. 10-20 09:43:08.423 23079 23079 W System.err: at android.app.ActivityThread.main(ActivityThread.java:5692)
  32. 10-20 09:43:08.423 23079 23079 W System.err: at java.lang.reflect.Method.invoke(Native Method)
  33. 10-20 09:43:08.423 23079 23079 W System.err: at java.lang.reflect.Method.invoke(Method.java:372)
  34. 10-20 09:43:08.423 23079 23079 W System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
  35. 10-20 09:43:08.423 23079 23079 W System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
  36. 10-20 09:43:08.423 23079 23079 W System.err: Caused by: java.io.IOException: broken file descriptor
  37. 10-20 09:43:08.423 23079 23079 W System.err: at android.graphics.BitmapFactory.nativeDecodeFileDescriptor(Native Method)
  38. 10-20 09:43:08.423 23079 23079 W System.err: at android.graphics.BitmapFactory.decodeFileDescriptor(BitmapFactory.java:714)
  39. 10-20 09:43:08.423 23079 23079 W System.err: at android.app.WallpaperManager$Globals.getCurrentWallpaperLocked(WallpaperManager.java:308)
  40. 10-20 09:43:08.423 23079 23079 W System.err: at android.app.WallpaperManager$Globals.peekWallpaperBitmap(WallpaperManager.java:270)
  41. 10-20 09:43:08.423 23079 23079 W System.err: at android.app.WallpaperManager.getDrawable(WallpaperManager.java:413)

更换手机桌面应用布局界面时调用native方法decodeFileDescriptor读取文件/proc/stprof/status抛出了异常未捕获导致进程崩溃,建议按照示例一判断文件是否存在并对异常进行捕获处理。

参考文献:

  1. https://developer.android.com/guide/topics/data/data-storage.html#filesExternal

  2. http://www.cnblogs.com/feisky/archive/2011/01/05/1926177.html