版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/myloveprogrmming/article/details/82871117
1.分割:
package a;
import java.io.*;
//用三个线程把文件切割成三段
class PartOne implements Runnable{
File f;
int size;
PartOne(File f,int size)
{
this.f=f;
this.size=size;
}
public void run()
{
BufferedInputStream bin = null;;
BufferedOutputStream bout=null;
try {
bin=new BufferedInputStream(new FileInputStream(f));
bout=new BufferedOutputStream(new FileOutputStream("d:\\part1.rmvb"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(int i=1;i<=size/3;i++)
{
try {
bout.write(bin.read());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
bout.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
bin.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class PartTwo implements Runnable{
File f;
int size;
PartTwo(File f,int size)
{
this.f=f;
this.size=size;
}
public void run()
{
BufferedInputStream bin = null;;
BufferedOutputStream bout=null;
try {
bin=new BufferedInputStream(new FileInputStream(f));
bout=new BufferedOutputStream(new FileOutputStream("d:\\part2.rmvb"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
bin.skip(size/3);
for(int i=1;i<=size/3;i++)
{
try {
bout.write(bin.read());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
bout.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
bin.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class PartThree implements Runnable{
File f;
int size;
PartThree(File f,int size)
{
this.f=f;
this.size=size;
}
public void run()
{
BufferedInputStream bin = null;;
BufferedOutputStream bout=null;
try {
bin=new BufferedInputStream(new FileInputStream(f));
bout=new BufferedOutputStream(new FileOutputStream("d:\\part3.rmvb"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
bin.skip((size/3)*2);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
int len=size-(size/3)*2;
for(int i=1;i<=len;i++)
{
try {
bout.write(bin.read());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
bout.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
bin.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class Homework1 {
public static void main(String[] args) throws IOException, IOException {
File f=new File("E:\\迅雷下载\\伯德小姐.rmvb");
int fsize=new FileInputStream(f).available();
Thread t1=new Thread(new PartOne(f,fsize));
t1.start();
Thread t2=new Thread(new PartTwo(f,fsize));
t2.start();
Thread t3=new Thread(new PartThree(f,fsize));
t3.start();
}
}
2.拼接(还原)
package a;
import java.io.*;
public class HomeTest {
public static void main(String[] args) throws IOException {
BufferedInputStream bin1=new BufferedInputStream(new FileInputStream("d:\\part1.rmvb"));
BufferedInputStream bin2=new BufferedInputStream(new FileInputStream("d:\\part2.rmvb"));
BufferedInputStream bin3=new BufferedInputStream(new FileInputStream("d:\\part3.rmvb"));
BufferedOutputStream bout=new BufferedOutputStream(new FileOutputStream("d:\\haha.rmvb"));
byte[] b=new byte[1024];
int len=0;
while((len=bin1.read(b))!=-1) {
bout.write(b,0,len);
}
while((len=bin2.read(b))!=-1) {
bout.write(b,0,len);
}
while((len=bin3.read(b))!=-1) {
bout.write(b,0,len);
}
bout.close();
bin3.close();
bin2.close();
bin1.close();
}
}