这两天在做U盘文件复制这个简单的小功能,测试中发现如果拷贝到一般时强行拔出U盘会导致整个应用崩溃。
功能代码:
/*** 复制文件 <功能说明>* * @param @param oldFile* @param @param newfile* @param @param id*/public void copyfile(File oldFile, File newfile, int id) {FileInputStream fis = null; RandomAccessFile raf = null;try {fis = new FileInputStream(oldFile);raf = new RandomAccessFile(newfile.getPath(), "rwd");raf.setLength(0);//清除旧文件byte[] buf = new byte[3 * 1024 * 1024];int readline = 0;while ((readline = fis.read(buf)) != -1) {raf.write(buf, 0, readline);}raf.close();fis.close();} catch (IOException e) {e.printStackTrace();}}
先上崩溃日志:
04-14 14:16:15.840 W/Vold ( 81): subsystem found in netlink event
04-14 14:16:15.840 D/Vold ( 81): scsi_device, 2
04-14 14:16:15.890 W/System.err( 1036): java.io.IOException: write failed: EIO (I/O error)
-----------------------------------
04-14 13:59:26.840 E/MediaScannerJNI( 377): An error occurred while scanning directory '/mnt/usb_storage'.
04-14 13:59:26.850 I/System.out( 414): 777777777777777flog:0
04-14 13:59:26.910 D/dalvikvm( 485): GC_CONCURRENT freed 366K, 10% free 6407K/7047K, paused 16ms+22ms, total 56ms
04-14 13:59:26.930 D/MediaScannerService( 377): done scanning volume external
04-14 13:59:26.950 E/ProcessKiller( 81): Process com.xxxx (414) has open file /mnt/usb_storage/mnt/sdcard/recorderfiles/1 - 复制 (17).jpg
04-14 13:59:27.200 E/ProcessKiller( 81): Process com.xxxx (414) has open file /mnt/usb_storage/mnt/sdcard/recorderfiles/1 - 复制 (17).jpg
04-14 13:59:27.450 E/ProcessKiller( 81): Process com.xxxx (414) has open file /mnt/usb_storage/mnt/sdcard/recorderfiles/1 - 复制 (17).jpg
04-14 13:59:27.630 E/ProcessKiller( 81): Process com.xxxx (414) has open file /mnt/usb_storage/mnt/sdcard/recorderfiles/1 - 复制 (17).jpg
04-14 13:59:27.780 E/ProcessKiller( 81): Process com.xxxx (414) has open file /mnt/usb_storage/mnt/sdcard/recorderfiles/1 - 复制 (17).jpg
04-14 13:59:27.930 E/ProcessKiller( 81): Process com.xxxx (414) has open file /mnt/usb_storage/mnt/sdcard/recorderfiles/1 - 复制 (17).jpg
04-14 13:59:28.080 E/ProcessKiller( 81): Process com.xxxx (414) has open file /mnt/usb_storage/mnt/sdcard/recorderfiles/1 - 复制 (17).jpg
04-14 13:59:28.240 E/ProcessKiller( 81): Process com.xxxx (414) has open file /mnt/usb_storage/mnt/sdcard/recorderfiles/1 - 复制 (17).jpg
04-14 13:59:28.350 W/Vold ( 81): Failed to unmount /mnt/usb_storage (Device or resource busy, retries 140, action 0)
04-14 13:59:28.420 E/ProcessKiller( 81): Process com.xxxx (414) has open file /mnt/usb_storage/mnt/sdcard/recorderfiles/1 - 复制 (17).jpg
04-14 13:59:28.560 E/ProcessKiller( 81): Process com.xxxx (414) has open file /mnt/usb_storage/mnt/sdcard/recorderfiles/1 - 复制 (17).jpg
04-14 13:59:28.710 E/ProcessKiller( 81): Process com.xxxx (414) has open file /mnt/usb_storage/mnt/sdcard/recorderfiles/1 - 复制 (17).jpg
04-14 13:59:28.860 E/ProcessKiller( 81): Process com.xxxx (414) has open file /mnt/usb_storage/mnt/sdcard/recorderfiles/1 - 复制 (17).jpg
04-14 13:59:29.030 E/ProcessKiller( 81): Process com.xxxx (414) has open file /mnt/usb_storage/mnt/sdcard/recorderfiles/1 - 复制 (17).jpg
04-14 13:59:29.260 E/ProcessKiller( 81): Process com.xxxx(414) has open file /mnt/usb_storage/mnt/sdcard/recorderfiles/1 - 复制 (17).jpg
04-14 13:59:29.460 E/ProcessKiller( 81): Process comxxxx (414) has open file /mnt/usb_storage/mnt/sdcard/recorderfiles/1 - 复制 (17).jpg
04-14 13:59:29.630 E/ProcessKiller( 81): Process comxxxx (414) has open file /mnt/usb_storage/mnt/sdcard/recorderfiles/1 - 复制 (17).jpg
04-14 13:59:29.810 E/ProcessKiller( 81): Process comxxxx (414) has open file /mnt/usb_storage/mnt/sdcard/recorderfiles/1 - 复制 (17).jpg
04-14 13:59:29.850 I/MainActivity( 414): >>>LoadStatusTask, doInBackground
04-14 13:59:29.990 E/ProcessKiller( 81): Process com.xxxx (414) has open file /mnt/usb_storage/mnt/sdcard/recorderfiles/1 - 复制 (17).jpg
04-14 13:59:30.080 W/Vold ( 81): Failed to unmount /mnt/usb_storage (Device or resource busy, retries 130, action 1)
04-14 13:59:30.140 E/ProcessKiller( 81): Process com.xxxx (414) has open file /mnt/usb_storage/mnt/sdcard/recorderfiles/1 - 复制 (17).jpg
binder: release 414:613 transaction 169656 out, still active
04-14 13:59:30.140 W/ProcessKiller( 81): Sending SIGHUP to process 414
04-14 13:59:30.180 I/WindowState( 264): WIN DEATH: Window{423c87c0 com.lidma.test/com.lidma.test.MainActivity paused=false}
查询日志发现先是I/O error,然后 会判断进程所打开的文件,重试几次无效后会把进程kill掉。既然问题是文件没关掉导致的,我就在捕获到IOEexception后关闭文件,问题暂时解决。
/*** 复制文件 <功能说明>* * @param @param oldFile* @param @param newfile* @param @param id*/public void copyfile(File oldFile, File newfile, int id) {FileInputStream fis = null; RandomAccessFile raf = null;try {fis = new FileInputStream(oldFile);raf = new RandomAccessFile(newfile.getPath(), "rwd");raf.setLength(0);//清除旧文件byte[] buf = new byte[3 * 1024 * 1024];int readline = 0;while ((readline = fis.read(buf)) != -1) {raf.write(buf, 0, readline);}} catch (IOException e) {e.printStackTrace();}//之前IO错误后就直接跳到catch,没有执行file.close的操作,现在把他们单独搞个try catchtry {if(raf!= null){raf.close();}if(fis!= null){fis.close();}} catch (Exception e) {e.printStackTrace();}}
附上RandomAccessFile的使用详解链接:
http://blog. csdn.net/ako n_vm/article /details/742 9245