天易43------实现文件的截取
来源:互联网 发布:淘宝一个月能赚多少 编辑:程序博客网 时间:2024/06/01 22:05
一:代码
package com.okhttp3.util;
import android.os.Environment;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
/**
* @author : zhousf
* @description : 文件复制工具类:可指定长度复制文件
* @date : 2017/4/6.
*/
public class FileUtil {
/**
* 保存文件的路径
*/
private final static String saveFileDir = Environment.getExternalStorageDirectory().getPath()+"/okHttp_upload/";
/**
* 根据长度截取文件并返回字节数组
* @param from 复制起始点
* @param to 复制终点
* @param file 复制的文件
* @return 截取后的文件
*/
public static byte[] copyFileToByte(long from, long to, File file){
if(to == 0){
to = file.length();
}
if(from > file.length()){
throw new IllegalArgumentException("from is over size of the file:"+file.getPath());
}
long length = to - from;
byte[] result = new byte[(int)length];
RandomAccessFile accessFile = null;
try {
accessFile = new RandomAccessFile(file,"rw");
accessFile.seek(from);
int readSize = accessFile.read(result);
if (readSize == -1) {
return null;
} else if (readSize == length) {
return result;
} else {
byte[] tmpByte = new byte[readSize];
System.arraycopy(result, 0, tmpByte, 0, readSize);
return tmpByte;
}
} catch (Exception e){
e.printStackTrace();
} finally {
if (accessFile != null) {
try {
accessFile.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
return null;
}
/**
* 根据长度截取文件并返回字节数组
* @param from 复制起始点
* @param to 复制终点
* @param filePath 复制的文件的地址
* @return 截取后的文件地址
*/
public static String copyFile(long from, long to, String filePath){
File file = new File(filePath);
if(file.exists()){
return copyFile(from,to,file);
}
return null;
}
/**
* 根据长度截取文件并返回字节数组
* @param from 复制起始点
* @param to 复制终点
* @param file 复制的文件
* @return 截取后的文件地址
*/
public static String copyFile(long from, long to, File file){
byte[] result = copyFileToByte(from,to,file);
if(result != null){
String name = file.getPath().substring(file.getPath().lastIndexOf("/") + 1,file.getPath().length());//文件名
mkDirNotExists(saveFileDir);
return getFile(result,saveFileDir,name).getPath();
}
return null;
}
public static boolean mkDirNotExists(String dir) {
File file = new File(dir);
return file.exists() || file.mkdirs();
}
//根据byte数组,生成文件
public static File getFile(byte[] bfile, String filePath,String fileName) {
BufferedOutputStream bos = null;
FileOutputStream fos = null;
File file = null;
try {
File dir = new File(filePath);
if(!dir.exists()&&dir.isDirectory()){//判断文件目录是否存在
dir.mkdirs();
}
file = new File(filePath+"\\"+fileName);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(bfile);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bos != null) {
bos.close();
}
if (fos != null) {
fos.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
return file;
}
}
二:调用方法
copyFile(long from, long to, String filePath)
1 0
- 天易43------实现文件的截取
- 文件截取的实现
- 利用VC实现AVI文件的图像截取
- 利用VC实现AVI文件的图像截取
- 利用VC实现AVI文件的图像截取
- tail命令实现截取掉文件开始的一部分字节
- JAVA截取文件的后缀
- 文件的截取:truncate方法
- jquery 截取字符串的实现
- 截取屏幕的实现方法
- QT截取屏幕的实现
- 屏幕截取功能的实现
- QT截取屏幕的实现
- NY12图像的截取实现
- JS实现字符串的截取
- 文件截取
- Qt 截取文件路径后的后缀名
- js截取文件上传的文件名称
- lambda表达式之进化
- 异步网络爬虫的Python实现(3)
- $parse和$eval
- Android观察者模式的简单实现demo
- 大规模优化算法--LBFGS算法
- 天易43------实现文件的截取
- git基本命令大全
- 安卓选择通讯录联系人和获取全部联系人
- 机器学习Python实现之线性模型
- 事件引入和本质
- js,jq监听键盘的组合键事件
- 序列化和反序列化
- 异步网络爬虫的Python实现(4)
- Linux(CentOS)下配置安装Tomcat并配置JDK环境