数据拷贝过程中U盘异常拔出

news/2024/11/28 10:58:36/
这两天在做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/akon_vm/article/details/7429245


http://www.ppmy.cn/news/113923.html

相关文章

联想服务器linux系统raid驱动,ThinkSystem服务器RAID 530/930系列阵列卡驱动及安装RHEL7.3要点说明...

知识点分析: 本文介绍在配置为RAID 530/930系列阵列卡的 ThinkSystem ST550 ThinkSystem ST558 ThinkSystem SR530 ThinkSystem SR550 ThinkSystem SR630 ThinkSystem SR650 ThinkSystem SR850 ThinkSystem SR950 ThinkSystem SD530 ThinkSystem SN550 ThinkSystem SN850 以上服…

NXP(Freescale) QorIQ T2080 u-boot与linux内核移植

作者 QQ群&#xff1a;852283276 微信&#xff1a;arm80x86 微信公众号&#xff1a;青儿创客基地 B站&#xff1a;主页 https://space.bilibili.com/208826118 参考 T2080RDB-PC uboot 引导 Linux 内核启动 Freescale Layerscape 嵌入式系统构建笔记 系统内存空间分配 CCSR&…

u-boot 之配置分析 (2)

Makefile简要分析所有这些目录的编译连接都是由顶层目录的makefile来确定的。 1.在makefile中有: unconfig: @rm -f $(obj)include/config.h $(obj)include/config.mk \ $(obj)board/*/config.tmp $(obj)board/*/*/config.tmp %是个通配符,make xxx_config都是这个目标.目标…

磺酸基吲哚菁绿活化脂ICG-Sulfo-OSu,ICG NHS ester,CAS:1622335-40-3,ICG-PEG-OSu

ICG-Sulfo-OSu,ICG NHS ester,磺酸基吲哚菁绿活化脂 中文名&#xff1a;磺酸基吲哚菁绿活化脂 英文名&#xff1a;ICG-Sulfo-OSu 产品描述 ICG-Sulfo-OSu产品的光谱学相关参数为Ex&#xff08;nm&#xff09;780/ Em&#xff08;nm&#xff09;800&#xff1b;其对应分子量大…

u-boot - environment - CRC32

Overview 这里简单提一下environment中使用的CRC32算法&#xff0c;可以作为其他项目计算CRC32时的源码参考。 Source Codes Location include/u-boot/crc.h lib/crc32.c这里仅用到如下两个接口&#xff1a; uint32_t crc32 (uint32_t, const unsigned char *, uint); uint…

Codeforces-930A Connected Components

传送门 n个点的树&#xff0c;以点1为根&#xff0c;告诉你经过每一秒&#xff0c;某顶点上的苹果会到达哪个顶点&#xff0c;且已知一个顶点每同时出现两个苹果就会碰撞消失&#xff08;如2得0&#xff0c;如3得1&#xff09;&#xff0c;求有多少个苹果能到达顶点1 bfs求点深…

《jdk8u源码分析》jvm.dll接口列表

通过命令行工具 VS2012 ARM Cross Tools Command Prompt 执行命令&#xff1a; dumpbin /exports "C:\Tools\Java\jre1.8.0_191\bin\server\jvm.dll">>D:\jvmdll.txtjvmdll.txt Microsoft (R) COFF/PE Dumper Version 11.00.51106.1 Copyright (C) Microsoft…

DELL戴尔服务器Windows Server 2008/2012 操作系统安装指导-U盘安装

个人整理的DELL服务器相关的常用网址 DELL官网服务支持 可以下载相关文档驱动 http://www.dell.com/support/home/cn/zh/cndhs1 DELL服务器R730产品介绍手册 http://www.dell.com/support/manuals/cn/zh/cndhs1/poweredge-r730/r730_ompublication/dell-poweredge-r730-%E7%B…