看后面的图
package IOTest;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
/**
*
* 文件分割下载
*
*/
public class SplitFile {
private File source; //文件源头
private String desFile; //文件保存的目的地文件夹
private List<String> destPath; //所有分割后的文件所保存的路径
private int eachBlockSize; //每一块的大小
private int sizeNum; //总共多少块
public SplitFile(String path, String desFile, int eachBlockSize)
{
this.source = new File(path);
this.desFile = desFile;
this.eachBlockSize = eachBlockSize;
this.destPath = new ArrayList<String>();
init();
}
private void init() //初始化
{
long len = this.source.length(); //文件总长度
this.sizeNum = (int)Math.ceil(len*1.0 / eachBlockSize); //计算总块数,注意小数点
for(int i = 0;i < sizeNum; i++)
{
this.destPath.add(this.desFile + "/" + i + "-" + this.source.getName()); //完成所有分割后的文件所保存的路径
}
}
public void split() throws IOException //分割文件,首先计算每一块文件的起始位置
{
long len = this.source.length();
int beginPos = 0; //起始位置
int actualSize = (int)(eachBlockSize > len ? len:eachBlockSize);
for(int i = 0;i < sizeNum;i++)
{
beginPos = i * eachBlockSize;
if(i == sizeNum - 1) //到了最后一块
actualSize = (int)len;
else
{
actualSize = eachBlockSize;
len -= actualSize; //剩余还没读取的量
}
splitDetail(i,beginPos,actualSize);
}
}
private void splitDetail(int i, int beginPos, int actualSize) throws IOException {
RandomAccessFile randomAccessFile1 = new RandomAccessFile(this.source,"r");
RandomAccessFile randomAccessFile2 = new RandomAccessFile(this.destPath.get(i),"rw");
randomAccessFile1.seek(beginPos); //读取
byte[] flush = new byte[1024]; //缓冲容器
int len = -1; //接收长度
while((len = randomAccessFile1.read(flush)) != -1) //read方法返回读取的数据量
{
if(actualSize > len) //若剩下的数据量大于这一次接收的量
{
randomAccessFile2.write(flush,0,len);
actualSize -= len;
}
else
{
randomAccessFile2.write(flush,0,actualSize);
break;
}
}
randomAccessFile1.close();
randomAccessFile2.close();
}
public void merge(String desFile) throws IOException {
OutputStream out = new BufferedOutputStream(new FileOutputStream(desFile,true));
for(int i=0;i < this.destPath.size();i++)
{
InputStream in = new BufferedInputStream(new FileInputStream(destPath.get(i)));
byte[] flush = new byte[1024];
int len = -1;
while((len=in.read(flush)) != -1)
{
out.write(flush,0,len);
}
out.flush();
in.close();
}
out.close();
}
public static void main(String[] args) throws IOException {
SplitFile splitFile = new SplitFile("src/IOTest/SplitFile.java","src",1024);
splitFile.split();
splitFile.merge("outer.java");
}
}
为什么0号文件与其他3个文件都不同?而且运行完以后IOTEST里的主类直接红线了,提示Cannot resolve method......