您的位置:首页 > 编程语言 > Java开发

文件处理续存操作

2017-12-27 16:44 211 查看
/**
* 拷贝文件,文件存在可以从后面追加文件数据,可用于文件操作失败续存操作
*
* @param fileDir
*            被拷贝文件位置
* @param copyFileDir
*            拷贝到的目标文件
* @param xuchuan
*            文件存在是否从文件后面添加
* @throws IOException
*/
public static void copyFile(String fileDir, String copyFileDir, boolean xuchuan) throws IOException {
// 原始文件
File fIn = new File(fileDir);
RandomAccessFile in = new RandomAccessFile(fIn, "r");// 以只读写入方式打开
// 拷贝文件
File fOut = new File(copyFileDir);
RandomAccessFile randomFile = new RandomAccessFile(fOut, "rw");// 以读写入方式打开
// 一次读多个字节
if (fOut.exists() && xuchuan) {
long fileLong = 0;
fileLong = fOut.length();
// 读取移动指针到指定位置
in.seek(fileLong);
// 写入移动指针到指定位置
randomFile.seek(fileLong);
}
// 一次读多个字节
byte[] tempbytes = new byte[1024];
int byteread = 0;
while ((byteread = in.read(tempbytes)) != -1) {
// 写文件
randomFile.write(tempbytes, 0, byteread);
}
randomFile.close();
in.close();
}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  文件 java