导航菜单
首页 >  javaioFileNotFoundException  > 如何处理报错java

如何处理报错java

代码示例

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

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

File extDir = Environment.getExternalStorageDirectory();String filename = "file.txt";File fullFilename = new File(extDir, filename);try {if(!fullFilename.exists()){fullFilename.createNewFile();fullFilename.setWritable(Boolean.TRUE);// 写文件操作}else{// 读文件操作}} catch (IOException e) {e.printStackTrace();}

示例:

java.io.FileNotFoundException: /proc/mtprof/status: open failed: ENOENT (No such file or directory)at libcore.io.IoBridge.open(IoBridge.java:496)at java.io.FileInputStream.(FileInputStream.java:76)at java.io.FileInputStream.(FileInputStream.java:103)at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1494)at android.os.Handler.dispatchMessage(Handler.java:111)at android.os.Looper.loop(Looper.java:194)at android.app.ActivityThread.main(ActivityThread.java:5692)at java.lang.reflect.Method.invoke(Native Method)at java.lang.reflect.Method.invoke(Method.java:372)at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)at libcore.io.Posix.open(Native Method)at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)at libcore.io.IoBridge.open(IoBridge.java:482)... 10 more

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

10-20 09:43:08.073820820 E WifiTrafficPoller: TRAFFIC_STATS_POLL true Token 1138 num clients 1210-20 09:43:08.074820820 E WifiTrafficPoller: packet count Tx=237196 Rx=49375610-20 09:43:08.365820842 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 crashing10-20 09:43:08.401 23079 23079 E ActivityThread: mtprof entry can not be found10-20 09:43:08.401 23079 23079 E ActivityThread: java.io.FileNotFoundException: /proc/mtprof/status: open failed: ENOENT (No such file or directory)10-20 09:43:08.401 23079 23079 E ActivityThread: at libcore.io.IoBridge.open(IoBridge.java:496)10-20 09:43:08.401 23079 23079 E ActivityThread: at java.io.FileInputStream.(FileInputStream.java:76)10-20 09:43:08.401 23079 23079 E ActivityThread: at java.io.FileInputStream.(FileInputStream.java:103)10-20 09:43:08.401 23079 23079 E ActivityThread: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1494)10-20 09:43:08.401 23079 23079 E ActivityThread: at android.os.Handler.dispatchMessage(Handler.java:111)10-20 09:43:08.401 23079 23079 E ActivityThread: at android.os.Looper.loop(Looper.java:194)10-20 09:43:08.401 23079 23079 E ActivityThread: at android.app.ActivityThread.main(ActivityThread.java:5692)10-20 09:43:08.401 23079 23079 E ActivityThread: at java.lang.reflect.Method.invoke(Native Method)10-20 09:43:08.401 23079 23079 E ActivityThread: at java.lang.reflect.Method.invoke(Method.java:372)10-20 09:43:08.401 23079 23079 E ActivityThread: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)10-20 09:43:08.401 23079 23079 E ActivityThread: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)10-20 09:43:08.401 23079 23079 E ActivityThread: Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)10-20 09:43:08.401 23079 23079 E ActivityThread: at libcore.io.Posix.open(Native Method)10-20 09:43:08.401 23079 23079 E ActivityThread: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)10-20 09:43:08.401 23079 23079 E ActivityThread: at libcore.io.IoBridge.open(IoBridge.java:482)10-20 09:43:08.401 23079 23079 E ActivityThread: ... 10 more10-20 09:43:08.415 23079 23079 W Launcher: setApplicationContext called twice! 10-20 09:43:08.422 23079 23079 E Fatal exception: :java.lang.RuntimeException: Unable to start activity ComponentInfo{}: java.io.IOException: broken file descriptor10-20 09:43:08.422 23079 23079 W System.err: java.lang.RuntimeException: Unable to start activity ComponentInfo{}: java.io.IOException: broken file descriptor10-20 09:43:08.422 23079 23079 W System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2581)10-20 09:43:08.422 23079 23079 W System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2656)10-20 09:43:08.422 23079 23079 W System.err: at android.app.ActivityThread.access$800(ActivityThread.java:178)10-20 09:43:08.423 23079 23079 W System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1512)10-20 09:43:08.423 23079 23079 W System.err: at android.os.Handler.dispatchMessage(Handler.java:111)10-20 09:43:08.423 23079 23079 W System.err: at android.os.Looper.loop(Looper.java:194)10-20 09:43:08.423 23079 23079 W System.err: at android.app.ActivityThread.main(ActivityThread.java:5692)10-20 09:43:08.423 23079 23079 W System.err: at java.lang.reflect.Method.invoke(Native Method)10-20 09:43:08.423 23079 23079 W System.err: at java.lang.reflect.Method.invoke(Method.java:372)10-20 09:43:08.423 23079 23079 W System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)10-20 09:43:08.423 23079 23079 W System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)10-20 09:43:08.423 23079 23079 W System.err: Caused by: java.io.IOException: broken file descriptor10-20 09:43:08.423 23079 23079 W System.err: at android.graphics.BitmapFactory.nativeDecodeFileDescriptor(Native Method)10-20 09:43:08.423 23079 23079 W System.err: at android.graphics.BitmapFactory.decodeFileDescriptor(BitmapFactory.java:714)10-20 09:43:08.423 23079 23079 W System.err: at android.app.WallpaperManager$Globals.getCurrentWallpaperLocked(WallpaperManager.java:308)10-20 09:43:08.423 23079 23079 W System.err: at android.app.WallpaperManager$Globals.peekWallpaperBitmap(WallpaperManager.java:270)10-20 09:43:08.423 23079 23079 W System.err: at android.app.WallpaperManager.getDrawable(WallpaperManager.java:413)

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

相关推荐: