FileInputStream fileInputStream = null;
try{
fileInputStream = new FileInputStream(mTempFile);
bufferedInputStream = new BufferedInputStream(fileInputStream);
byte[] buffer = new byte[2048];
int size = 0;
DataProcessor tempDataProcessor;
while((size = bufferedInputStream.read(buffer)) >= 0){
tempProcessor.save(buffer,size,plainTextRandomAccessFile):
}
catch(IOExeption | COMException e){
LogUtil.error(TAG, "IOException or COMException");
}
//初始化随机文件读写
randomAccessFile = new RandomAccessFile(mFile, "rw");
tempRandomAccessFile = new RandomAccessFile(mTempFile, "rw");
if(!TextUtils.isEmpty(mPlainTextFilePath)){
if(mPlainTextFile == null ) {
mPlainTextFile = new File(mPlainTextFilePath);
}
plainTextRandomAccessFile = new RandomAccessFile(mPlainTextFile, "rw");
}
try{
randomAccessFile.seek(toPlayFileLength);
tempRandomAccessFile.seek(mDownloadLen);
} catch(IOException ex){
closeAllRandomFiles();
notifyError(DownloadListener.ERROR_INTERNAL);
return false;
}
读取文件,并输出到另一个文件
1. 判断文件是否存在
if(!FileUtils.isFileExists(file)) {
return new byte[0];
}
BufferedInputStream bufferedInputStream = null;
ByteArrayOutputStream out = null;
FileInputStream fileInputStream = null;
try{
fileInputStream = new FileInputStream(file);
bufferedInputStream = new BufferedinputStream(fileInputStream);
out = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int count = 0;
int readSize;
while((readSize = bufferedInputStream.read(buffer)) >= 0){
count += readSize;
if(count >= MAX_SIZE) {
return;
}
out.write(buffer, 0, readSize);
}
return out.toByteArray();
}catch(IOException ex){
LogUtil.error(tag, "unzip failed"):
} finally {
CloseUtils.close(fileInputStream);
CloseUtils.close(bufferedInputStream);
CloseUtils.close(out);
}
return new byte[0];
}