import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
public class FileUtil {
/**
* @Title: saveByRandomAccessFile
* @Description: 使用RandomAccessFile写入文件
* @param outPath 存入的目标地址,包括文件名称扩展名
* @param tempFile 源文件
* @throws IOException
*/
public static void saveByRandomAccessFile(String outPath, File tempFile)
throws IOException {
RandomAccessFile raFile = null;
BufferedInputStream inputStream = null;
try {
File dirFile = new File(outPath);
if (!dirFile.getParentFile().exists()) {
dirFile.getParentFile().mkdirs();
}
// 以读写的方式打开目标文件
raFile = new RandomAccessFile(dirFile, "rw");
raFile.seek(raFile.length());
inputStream = new BufferedInputStream(new FileInputStream(tempFile));
byte[] buf = new byte[1024];
int length = 0;
while ((length = inputStream.read(buf)) != -1) {
raFile.write(buf, 0, length);
}
} catch (Exception e) {
throw new IOException(e.getMessage());
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
if (raFile != null) {
raFile.close();
}
} catch (Exception e) {
throw new IOException(e.getMessage());
}
}
}
}