Java uses IO stream to realize the segmentation and merging of large files
File segmentation should be a more practical function. For example, if you have a 3G file that needs to be copied from one computer to another, but your storage device (such as an SD card) is only 1G. At this time, you can cut the file into 3 1G files, copy them separately, and finally merge the three files, which solves the problem. For example, you have a file of hundreds of M to upload to FTP, but this FTP limits your single file to not exceed 10M. At this time, you can also use file segmentation to solve the problem. Since it is divided, we need to merge when we use it again. Today we will implement the ability of file splitting and merging through Java code.
Now, we will demonstrate by demonstrating a file from my machine. The file directory is: E:/eclipse-jee-juno-win32.zip (Today I will have fun with the eclipse that everyone hates):
The following figure shows the situation before the segmentation:
The situation after division is:
How to split files in Java:
//The method of file splitting (pass the file path to be split and the number of copies to be split in the method) private static void splitFileDemo(File src, int m) { if(src.isFile()) { //Get the total length of the file long l = src.length(); //Get the file name String fileName = src.getName().substring(0, src.getName().indexOf(".")); //Get the file suffix String endName = src.getName().substring(src.getName().lastIndexOf(".")); System.out.println(endName); InputStream in = null; try { in = new FileInputStream(src); for(int i = 1; i <= m; i++) { StringBuffer sb = new StringBuffer(); sb.append(src.getParent()).append(fileName) .append("_data").append(i).append(endName); System.out.println(sb.toString()); File file2 = new File(sb.toString()); //Create the output stream for writing files OutputStream out = new FileOutputStream(file2); int len = -1; byte[] bytes = new byte[10*1024*1024]; while((len = in.read(bytes))!=-1) { out.write(bytes, 0, len); if(file2.length() > (l / m)) { break; } } out.close(); } } catch (Exception e) { e.printStackTrace(); } finally { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } }} Java merge files:
//Method of file merging (pass in the file path to merge) private static void joinFileDemo(String... src) { for(int i = 0; i < src.length; i++) { File file = new File(src[i]); String fileName = file.getName().substring(0, file.getName().indexOf("_")); String endName = file.getName().substring(file.getName().lastIndexOf(".")); StringBuffer sb = new StringBuffer(); sb.append(file.getParent()).append("//").append(fileName) .append(endName); System.out.println(sb.toString()); try { //Read the input stream of small files InputStream in = new FileInputStream(file); //Write the output stream of large files File file2 = new File(sb.toString()); OutputStream out = new FileOutputStream(file2,true); int len = -1; byte[] bytes = new byte[10*1024*1024]; while((len = in.read(bytes))!=-1) { out.write(bytes, 0, len); } out.close(); in.close(); } catch (Exception e) { e.printStackTrace(); } } System.out.println("File merge is completed!");}I thought it was quite complicated before writing, but after writing it, I thought it was just that. You can practice your skills.
Thank you for reading, I hope it can help you. Thank you for your support for this site!