package _03;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;
//面向对象的思想封装
public class _3_26SplitFile {
//1.源头
private File src;
//2.目的地
private String destDir;
//3.所有分割后的文件路径
private List<String> pathList;
//4.分成多少块
private long size;
//5.分成块数的大小
private long blockSize;
public _3_26SplitFile(File file,String destDir){
this.src = file;
this.destDir = destDir;
this.pathList = new ArrayList<String>();
init();
}
private void init(){
long length =src.length();
//以1024字节为一块
int blockSize = 1024;
int size = (int) Math.ceil(length*1.0/blockSize);
for(int i=0;i<=size-1;i++){
this.pathList.add(destDir+"i"+"-"+this.src.getName());
}
}
//视频后期太乱 思想是对的 但是整个没有进行封装
public static void split(int i,int beginPos,int actualSize) throws IOException {
RandomAccessFile ra = new RandomAccessFile(new File("src/_03/_3_01File.java"),"r");
//读取指定长度
ra.seek(beginPos);
byte[] flush = new byte[1024];
int len =-1;
System.out.println(i); //表示分割的块数
while((len = ra.read(flush))!=-1){
if(actualSize>len){
String msg = new String(flush,0,len);
System.out.println(msg);
actualSize = - len;
}else{
String msg = new String(flush,0,actualSize);
System.out.println(msg);
break;
}
}
ra.close();
}
}
//文件的合并 相当与在文件后面追加
public void merge(String destPath) throws IOException {
OutputStream os = new BufferedOutputStream(new FileOutputStream(destPath,true)); //true表示追加
//输出流
for(int i=0;i<pathList.size();i++){
InputStream is = new BufferedInputStream(new FileInputStream(pathList.get(i)));
//拷贝
byte[] flush = new byte[1024];
int len = -1;
while((len= is.read(flush))!=-1){
os.write(flush,0,len);
}
os.flush();
os.close();
is.close();
}
}
// SequenceInputStream 表示逻辑级联
Vector<InputStream> ve = new Vector<>();
SequenceInputStream si =null;
// si = new SequenceInputStream(ve.elements())
package _03;
import sun.misc.IOUtils;
import java.net.MalformedURLException;
import java.net.URL;
public class _3_26CommonsIO {
public static void main(String[] args) throws MalformedURLException {
//CommonsIO 类似于我们的 FileUtils
//多个jar包打包在一起 就是组件了
//CommonsIO 需要从Apache官网上下载
// long len = FileUtils.sizeOf(new File("src/li.java"));
//
// String msg = FileUtils.readFiletoString(new File("emp.txt"),"UTF-8");
// System.out.print(msg);
//
// byte[] datas = FileUtils.readFileToByteArray(new File("emp.txt"));
// System.out.println(datas.length);
//
// //逐行读取
// List<String> msgs = FileUtil.readLine(new File("emp.txt"),"UTF-8");
//
// //复制文件
// FileUtil.copyFile(new File("example.png"),new File("p_example.png"));
// //复制文件到目录
// FileUtil.copyFileTODirectory(new File(example.png),new File("lib"));
//
// //复制目录成为当前目录的子目录
// FileUtil.copyDirectoryToDirectory(new File(),new File());
//
// //将一个目录的文件复制到另一个目录
// FileUtil.copyDirectory(new File(),new File());
//
//
// //拷贝URL的内容
// FileUtil.copyURLTOFile(new URL(),new File());
// String msg = IOUtils.toString(new URL("http://www.baidu.com"),"UTF-8");
}
}