多线程文件的拷贝,使用RandomAccessFile进行文件的合并与分割

在这里插入图片描述
使用RandomAccessFile类来进行文件的拷贝;我们平时创建的IO流处理文件(FileWriter)(FileReader)等等都必须是从文件的头部开始读写文件,不能从中间开始,
但是RandomAccessFile就可以很好的解决这个问题。因为它可以指定位置seek()来读。操作场景一般是多线程下载一个大文件!
RandomAccessFile类中常用的方法:
void seek(long pos) 设计文件指针的偏移,从该文件的开头测量,发生下一次读取或写入。
String readLine()从此文件读取下一行文本。

此题的方法:

1.将文件设置成五个(平均)大小的文件,首先规定启动五个线程,设置每个线程操作文件的起始位置和结束位置。
2.在每个线程当中进行文件的拷贝,
文件拷贝的过程与方法:

public void run() {
     
		try {
     
			RandomAccessFile R = new RandomAccessFile(FirstFile,"rw");
			RandomAccessFile F = new RandomAccessFile(SecondFile,"rw");
			//指针移到指定的位置。
			R.seek(StartIndex);
			F.seek(StartIndex);
			int Index=StartIndex;
			int count;
			byte t[]=new byte[1024];
			while((count=R.read(t))!=-1) {
     
				Index=Index+count;
				F.write(t,0,count);
				if(Index>EndIndex) {
     
					break;
				}
				
			}

代码如下:

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

//把文件进行复制
public class CopyFile implements Runnable {
     
	private String FirstFile;
	private String SecondFile;
	private int StartIndex;
	private int EndIndex;
	CopyFile(String FirstFile,String SecondFile,int StartIndex,int EndIndex){
      
		this.FirstFile=FirstFile;
		this.SecondFile=SecondFile;
		this.StartIndex=StartIndex;
		this.EndIndex=EndIndex;
	}
	public void run() {
     
		try {
     
			RandomAccessFile R = new RandomAccessFile(FirstFile,"rw");
			RandomAccessFile F = new RandomAccessFile(SecondFile,"rw");
			R.seek(StartIndex);
			F.seek(StartIndex);
			int Index=StartIndex;
			int count;
			byte t[]=new byte[1024];
			while((count=R.read(t))!=-1) {
     
				Index=Index+count;
				F.write(t,0,count);
				if(Index>EndIndex) {
     
					break;
				}
				
			}
		} catch (FileNotFoundException e) {
     
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
     
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		
	}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

public class FileFenGe {
     
	private String FirstFile;
	private String SecondFile;
	private int FileLength;
	FileFenGe(){
     };
	public void FenGe(String FirstFile, String SecondFile) throws IOException {
     
		File file=new File(FirstFile);
		int FileLength=(int)file.length();
		int count=5;
		int size=FileLength/5;
		RandomAccessFile F = new RandomAccessFile(FirstFile,"rw");
		RandomAccessFile S = new RandomAccessFile(SecondFile,"rw");
		for(int i=0;i<count-1;i++) {
     
			F.seek(i*size);
			S.seek(i*size);
				System.out.println("第"+(i+1)+"个线程启动");
				new Thread(new CopyFile(FirstFile,SecondFile, i*size , (i+1)*size) ).start();
			}
		       System.out.println("第5个线程启动");
		       new Thread(new CopyFile(FirstFile,SecondFile, count*size , FileLength) ).start();
		}
		
	}

import java.io.IOException;

public class Test {
     
	public static void main(String[] args) throws IOException {
     
		FileFenGe fis=new FileFenGe();
		fis.FenGe("D://java//cut//music.mp3", "D://java//cuts//music1.mp3")

	}
}

使用RandomAccessFIle进行文件的合并:

package 文件合并;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

public class Test {
     
	public static void main(String args[]) throws IOException {
     
		File file =new File("D://java//newdata.txt");
		file.createNewFile();
		RandomAccessFile out=new RandomAccessFile(file,"rw");
		for(int i=1;i<=6;i++) {
     
			String value="D://java//data"+i+".txt";
			RandomAccessFile in=new RandomAccessFile(value,"rw");
 			String count=in.readLine();
 			while(count!=null) {
     
 				out.writeChars(count);
 				count=in.readLine();
 			}
		}
		
	}
}

文件的分割:

package 文件分割;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

public class Test {
     
	public static void main(String args[]) throws IOException {
     
		File file=new File("D://java//data.txt");
		RandomAccessFile in=new RandomAccessFile(file,"rw");
		int length=(int)file.length();
		int everycount=length/5;
		 byte b[]=new byte[everycount];
		 int count=in.read(b);
		 int i=1;
		while(count!=-1) {
     
			RandomAccessFile out =new RandomAccessFile("D://java//data"+i+".txt","rw");
			i++;
			out.write(b,0,count);
			count=in.read(b);
		}
	}

}

你可能感兴趣的:(Java学习)