数据拷贝过程中U盘异常拔出
来源:互联网 发布:淘宝onlyanna整容前后 编辑:程序博客网 时间:2024/05/22 17:26
这两天在做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 catch try { if(raf!= null){ raf.close(); } if(fis!= null){ fis.close(); } } catch (Exception e) { e.printStackTrace(); } }
附上RandomAccessFile的使用详解链接:
http://blog.csdn.net/akon_vm/article/details/7429245
1 0
- 数据拷贝过程中U盘异常拔出
- 在.NET中探测U盘的插入/拔出
- 在.NET中探测U盘的插入/拔出
- 在.NET中探测U盘的插入/拔出
- U盘插入拔出检测
- 有效禁止u盘拷贝电脑中数据具体操作
- 如何检测U盘插入拔出?
- c# 检测优盘插入,U盘拔出!
- C# 监控U盘插入与拔出
- U盘插入与拔出识别
- C#检测U盘插入拔出
- DELPHI U盘插入拔出提示
- Qt检测U盘插入拔出Demo
- 判断-U盘拔出和插入-获取它盘符
- Win32窗体工程--监控U盘插入和拔出(1)
- Win32窗体工程--监控U盘插入和拔出
- U盘加载,卸载,拔出,插入,WM_DEVICECHANGE,WndProc,DBT_DEVICEARRIVAL,DBT_DEVICEREMOVECOMPLETE
- vb.net 实现u盘插入,拔出的识别
- 判断设备通知是否开启
- 工作准备
- Android定时广播和定时服务两种实现方式
- 卸载应用程序
- TCP/IP 之传输层
- 数据拷贝过程中U盘异常拔出
- ubuntu小型web服务器:apache2+svn+php+tomcat+mysql+备份恢复
- 插入排序
- Node.js是如何处理模块的
- 观察者模式。简单demo
- Basic Theory of Physically-Based Rendering
- Oracle-18-select语句初步&SQL中用算术表达式&别名的使用&连接运算符%distinct&where子句
- sql SELECT 增加行号
- 学习笔记(一) mysql + kbengine-0.8.2+U3D_demo详细搭建过程个人记录