Convert the path to a file object, and then calculate how many blocks will be divided:
File file = filePath.toFile();
int howManyParts = (int) Math.ceil(file.length() / (double)filePieceSize);
Initialize the input / output stream, output error information, and return false,Get current directory:
DataInputStream fileReader = null;
try {
fileReader = new DataInputStream(new FileInputStream(file));
} catch (FileNotFoundException e) {
e.printStackTrace(); System.out.println("File not found!"); return false;
}
DataOutputStream fileWriter;
Path dir = filePath.getParent();
Next, the file is read and output to each part In the file:
int readLength = -1;
long total = 0;
try {
for (int i = 1; i <= howManyParts ; i++){ //New file part i Path temp = Files.createFile(dir.resolve(filePath.getFileName() + ".part" + i)); //Build output stream fileWriter = new DataOutputStream(new FileOutputStream(temp.toFile())); //Read file and output while ( (readLength = fileReader.read(buffer)) != -1){ fileWriter.write(buffer,0,readLength); fileWriter.flush(); total += readLength; if (total == filePieceSize){ total = 0; break; } } //The file of part i has been output. Close the stream fileWriter.close(); } //After reading, close the input stream fileReader.close();
} catch (IOException e) {
e.printStackTrace(); System.out.println("IO Wrong!"); return false;
}
The function has been implemented. Next, test(Because of the film Fury There are 14 G. . It's too big.. Let's change it): ![](https://user-gold-cdn.xitu.io/2018/11/20/16730faeea03cc54?imageView2/0/w/1280/h/960/format/webp/ignore-error/1) I'm a big brother. Episode 5 has 729 M,It can be divided into about 12 part All right.
public static void main(String[] args) throws IOException {
double before = System.currentTimeMillis(); Path bigboss = Paths.get("D:\\Video\\I'm a big brother\\I'm a big brother.Kyou.kara.Ore.wa.Ep05.Chi_Jap.HDTVrip.1280X720.mp4"); FileSlice.slice(bigboss,-1); double after = System.currentTimeMillis(); System.out.println("Split file I'm the big brother.Kyou.kara.Ore.wa.Ep05.Chi_Jap.HDTVrip.1280X720.mp4," + Files.size(bigboss) + "Bytes, total time" + (after - before) + "ms" );
}
Operation results:
I'm a big brother Kyou. kara. Ore.wa. Ep05. Chi_ Jap. HDTVrip. 1280X720. Mp4765321889 bytes, total time 16335.0ms
![](https://user-gold-cdn.xitu.io/2018/11/20/16730faeea2cd9f6?imageView2/0/w/1280/h/960/format/webp/ignore-error/1) The speed is still very slow.. Next time, it's still implemented by multithreading, and then test the speed. In the case of single thread, an ordinary 40 minute Japanese drama costs 15-30s About, if mkv It's been a long time since the format of the movie.. But the limit should not be CPU The speed of execution, but on the hard disk IO If it is an ordinary hard disk, even multithreading should not speed up much.. ### File splicing This is very simple, contrary to segmentation OK. Complete code directly:
public static boolean glue(Path filePath, int howManyParts){
if (!Files.exists(filePath)){ return false; } //Get original file name String filename = getOriginalFileName(filePath.getFileName().toString()); if (filename == null){ System.out.println("afferent part Error parsing file name!"); return false; } //Initialize buffer byte [] buffer = new byte[1024 * 8]; //Gets the path to the file store Path dir = filePath.getParent(); try { DataInputStream fileReader = null; //Create original file Files.createFile(dir.resolve(filename)); //Build original file output stream DataOutputStream fileWriter = new DataOutputStream(new FileOutputStream(dir.resolve(filename).toFile())); int readLength = -1; for (int i = 1; i <= howManyParts ; i++){ //Get part i file path Path temp = dir.resolve(filename + ".part" + i); //Build input stream fileReader = new DataInputStream(new FileInputStream(temp.toFile())); //Read file and output while ( (readLength = fileReader.read(buffer)) != -1){ fileWriter.write(buffer,0,readLength); fileWriter.flush(); } //The file of part i has been read in. Close the stream fileReader.close(); } //After writing, close the output stream fileWriter.close(); } catch (IOException e) { e.printStackTrace(); System.out.println("IO Wrong!"); return false;
epilogue
Xiaobian is also very touched. If he has been in small and medium-sized companies and has not been in contact with large-scale Internet architecture design, it may be difficult to reach the technical and cognitive height of senior architects only by reading books. Learning from powerful people is the most effective way to reduce time exploration and energy waste.
The industry we choose has to continue to learn and eat youth food.
Although you may often see that programmers earn hundreds of thousands a year, after all, not most of them have the aura of famous schools or large enterprises such as Alibaba and Huawei. Older people are more likely to be laid off.
Share a wave of learning materials compiled by Xiaobian!
Give it to every little partner who wants to learn Java to improve himself. If you want information, you can click here for free
The technical and cognitive level of a first-class architect. Learning from powerful people is the most effective way to reduce time exploration and energy waste.
The industry we choose has to continue to learn and eat youth food.
Although you may often see that programmers earn hundreds of thousands a year, after all, not most of them have the aura of famous schools or large enterprises such as Alibaba and Huawei. Older people are more likely to be laid off.
Share a wave of learning materials compiled by Xiaobian!
Give it to every little partner who wants to learn Java to improve himself. If you want information, you can click here for free
[external chain picture transferring... (img-9iTXoqRU-1628410926040)]
This article ends here. My favorite friends can help me with some praise and comments. Thank you for your support!