How to split a file into pieces using Java?

I got a strange question. I need to know how I can break a given file into small pieces using Java (any type of file). Then I can put these rows in the number of CDs, pendrives and pick up. I tried to use this method

But, as most users have commented, what I'm trying is impossible to achieve in this way. So, I decided to ask a new question to get the correct method for splitting files.

When I break a file into pieces (I think 30 pieces), there should be a way to put them back together and create the source file. Please, help.

+7
source share
2 answers

Read the parts of the bytes into a byte array and save them in new files when the buffer is full or is the end of the file.

For example (the code is not perfect, but it should help to understand the process)

class FileSplit { public static void splitFile(File f) throws IOException { int partCounter = 1;//I like to name parts from 001, 002, 003, ... //you can change it to 0 if you want 000, 001, ... int sizeOfFiles = 1024 * 1024;// 1MB byte[] buffer = new byte[sizeOfFiles]; String fileName = f.getName(); //try-with-resources to ensure closing stream try (FileInputStream fis = new FileInputStream(f); BufferedInputStream bis = new BufferedInputStream(fis)) { int bytesAmount = 0; while ((bytesAmount = bis.read(buffer)) > 0) { //write each chunk of data into separate file with different number in name String filePartName = String.format("%s.%03d", fileName, partCounter++); File newFile = new File(f.getParent(), filePartName); try (FileOutputStream out = new FileOutputStream(newFile)) { out.write(buffer, 0, bytesAmount); } } } } public static void main(String[] args) throws IOException { splitFile(new File("D:\\destination\\myFile.mp4")); } } 

myFile.mp4 size = 12.7 MB

After the split, I had 13 files

  • myFile.mp4.001 - myFile.mp4.012 with a size of 1 MB
  • myFile.mp4.013 with a size of 806 KB

If you want to combine these files, you can use

 public static void mergeFiles(List<File> files, File into) throws IOException { try (FileOutputStream fos = new FileOutputStream(into); BufferedOutputStream mergingStream = new BufferedOutputStream(fos)) { for (File f : files) { Files.copy(f.toPath(), mergingStream); } } } 

You can also create some additional ways to make your life easier. For example, a method that will create a list of files containing individual parts based on the name (and location) of one of these files.

 public static List<File> listOfFilesToMerge(File oneOfFiles) { String tmpName = oneOfFiles.getName();//{name}.{number} String destFileName = tmpName.substring(0, tmpName.lastIndexOf('.'));//remove .{number} File[] files = oneOfFiles.getParentFile().listFiles( (File dir, String name) -> name.matches(destFileName + "[.]\\d+")); Arrays.sort(files);//ensuring order 001, 002, ..., 010, ... return Arrays.asList(files); } 

Using this method, we can overload the mergeFiles method to use only one of the File oneOfFiles files instead of the entire List<File> list (we will create this list based on one of the files)

 public static void mergeFiles(File oneOfFiles, File into) throws IOException { mergeFiles(listOfFilesToMerge(oneOfFiles), into); } 

You can also overload these methods to use String instead of File (if necessary, we will wrap each line in the file)

 public static List<File> listOfFilesToMerge(String oneOfFiles) { return listOfFilesToMerge(new File(oneOfFiles)); } public static void mergeFiles(String oneOfFiles, String into) throws IOException{ mergeFiles(new File(oneOfFiles), new File(into)); } 
+25
source

Reading and writing streams as raw byte[] . Avoid text streams and readers.

In your last question, you apparently split the files according to the "string". To reproduce this behavior, just use the fixed byte[] size for reading. Check out the warnings in the comments for your last question to check how many bytes are being read. A byte[2^16] not necessarily populated with one reading.

+3
source

All Articles