这算是断点续传的雏形么?

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;

/*

 * 多线程复制本地文件

 *写完才发现就这几行简单的代码,竟用了我好几个小时;

 * 呜呜,不知道为什么图片的复制要在
 * 连续运行第二次时才能复制,所以复制时生成的
 * 临时文件就没有写删除代码了;
 */
public class CopyOfCopyOfTest 
{
static File f3 = new File("d:\\a\\a.txt");     //本地要复制的文本、音频、图片等;
public static void main(String[] args) throws IOException, InterruptedException 
{
download();       
copy();
}

public static void download() throws FileNotFoundException, InterruptedException
{
long contentLength = f3.length(); //本地要复制的文件的长度;
long[] startPos = new long[3];    //每个线程开始复制文件的位置;(设置了三个子线程)
long endPos = 0;                  //每个线程结束复制文件的位置;
ChildThread11[] thread = new ChildThread11[3];
for(int i=0; i<3; i++)
{
startPos[i] += contentLength*i/3;
if(i==2)
{
endPos = contentLength;
}
else
{
endPos = contentLength*(i+1)/3 - 1;
}
thread[i] = new ChildThread11(f3,  i, startPos[i], endPos);
thread[i].start();
}
}

//将几个子线程复制的文件合并成一个完整的文件,即copy后的文件;
public static void copy( ) throws IOException
{
File file = new File(f3.getParent());   //取源文件的父路径;
File[] lists = file.listFiles();
File fileCopy = new File(file + File.separator + "copy" + f3.getName()); //copy的文件名;
InputStream is = null;
OutputStream os = new FileOutputStream(fileCopy);
for(int i=0; i<3; i++)
{
for(File x:lists)
{
if(x.isFile() && x.getName().matches("^.*" + "_" + i + "$")) //检查是否刚创建的临时文件;
{
is = new FileInputStream(x);
byte[] b = new byte[(int) x.length()];
is.read(b);
os.write(b);
x.delete();
}
}
}
is.close();
os.close();
}
}


class ChildThread11 extends Thread
{
private long startPosition;//线程开始复制文件的位置;
private long endPosition;  //线程结束复制文件的位置;
private int id;   //第id个子线程;
private File tempFile = null; //第id个子线程的临时文件;
private File file = null;  //源文件;

public ChildThread11(File file, int id, long startPosition, long endPosition)
{
this.id = id;
this.startPosition = startPosition;
this.endPosition = endPosition;
this.file = file;
tempFile = new File(file.getPath() + "_" + id);  //临时文件名;
if(!tempFile.exists())
{
try 
{
tempFile.createNewFile(); //创建临时文件;
} catch (IOException e) 
{
e.printStackTrace();
}
}
}
@Override
public void run()
{
System.out.println("Thread" + id + "run……");
        RandomAccessFile rafIn = null;
        RandomAccessFile rafOut = null;
try 
{
            rafIn = new RandomAccessFile(file, "rw");
rafOut = new RandomAccessFile(tempFile, "rw");
rafIn.seek(startPosition);   //调到第id个子线程开始复制的位置;
} catch (FileNotFoundException e) 
{
e.printStackTrace();
} catch (IOException e) 
{
e.printStackTrace();
}
while(startPosition <= endPosition)
{
if(id ==2 && startPosition == endPosition)
break;
System.out.println("Thread " + id + " run……" + " s=" + startPosition + " e=" + endPosition);
try {
rafOut.write(rafIn.read());
startPosition ++; //指针位置,每读一个字节就增加一次;
} catch (FileNotFoundException e) 
{
e.printStackTrace();
} catch (IOException e) 
{
e.printStackTrace();
}
}
try 
{
rafIn.close();
rafOut.close(); //关闭流;
} catch (IOException e) 
{
e.printStackTrace();
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值