English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Code Example of Java IO Stream Splitting a File into Multiple Subfiles

File splitting and merging is a common requirement, for example: when uploading large files, you can first split them into small blocks, upload them to the server, and then merge them. Many high-end distributed file systems (such as Google's GFS, Taobao's TFS) also split or merge files by block.

Let's take a look at the basic idea:

If there is a large file, after specifying the split size (for example: by1M cutting)

step 1:

First, calculate the final number of split small files N based on the size of the original file and the split size

step 2:

Create these N small files on the disk

step 3:

Start multiple threads (thread count = number of split files), in each thread, use the seek function of RandomAccessFile to position the read pointer to the beginning of each segment in the original file, then read the specified size forward (that is: the size of the split block), and finally write to the corresponding split file. Because of the parallel processing of multiple threads, each writes to its own small file, which is relatively fast in terms of speed.

Nachfolgender Code teilt eine Datei in mehrere Unterdateien auf, wobei jede Datei eine Größe von}}100K

package testIO;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Arrays;
public class subStream {
	public static void main(String[] args) {
		//Lesen Sie zunächst die Quelldatei in den Speicher 
		int eachSize=100*1024;
		File srcFile =new File("F:",/test/test.txt");
		//Erstellen Sie ein Dateiobjekt 
		splitFile(srcFile,eachSize);
	}
	public static void splitFile(File srcFile,int eachSize){
		//Prüfen Sie, ob die Datei den Anforderungen für die Aufteilung entspricht 
		if(srcFile.length()==0){
			throw new RuntimeException("Die Datei entspricht nicht den Anforderungen für die Aufteilung");
		}
		byte[] fileContent= new byte[(int) srcFile.length()];
		try {
			//Lesen Sie den Dateiinhalt in den Speicher 
			FileInputStream fis=new FileInputStream(srcFile);
			fis.read(fileContent);
			fis.close();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		//Berechnen Sie, wie viele Teile der Datei gesplittet werden müssen 
		int fileNumber;
		if(fileContent.length%eachSize==0){
			fileNumber = fileContent.length/eachSize;
		} else{
			fileNumber = fileContent.length/eachSize+1;
		}
		for (int i=0;i<fileNumber;i++){
			String fileName = srcFile.getName()+"-"+i+".txt";
			File fi = new File(srcFile.getParent(), fileName);
			//在当前文件路径下创建拆分的文件 
			byte[] eachContent;
			//将源文件内容复制到拆分的文件中 
			if(i!=fileNumber-1){
				eachContent = Arrays.copyOfRange(fileContent, eachSize*i, eachSize*(i+1));
			} else{
				eachContent = Arrays.copyOfRange(fileContent, eachSize*i, fileContent.length);
			}
			try {
				FileOutputStream fos = new FileOutputStream(fi);
				fos.write(eachContent);
				fos.close();
				System.out.printf("输出子文件 %s,其大小是 %d,每个的大小是%d\n",fi.getAbsoluteFile(),fi.length(),eachContent.length);
			}
			catch (Exception e) {
				// 待处理:处理异常 
				e.printStackTrace();
			}
		}
	}
}

总结

以上就是本文关于java IO流将一个文件拆分为多个子文件代码示例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

声明:本文内容来自网络,版权归原作者所有。内容由互联网用户自发贡献并自行上传,本网站不拥有所有权,未进行人工编辑处理,也不承担相关法律责任。如果您发现涉嫌版权的内容,欢迎发送邮件至:notice#oldtoolbag.com(在发送邮件时,请将#替换为@进行举报,并提供相关证据。一经查实,本站将立即删除涉嫌侵权的内容。)

You May Also Like