Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.RandomAccessFile;
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- *
- * @author ram
- */
- public class split
- {
- public static void main(String[] args) throws Exception
- {
- String fileName = "tifName.tif";
- String fil = "name";
- String type = ".tif";
- File tifFile = new File("C:\\Users\\s\\folder\\car_folder\\" + fileName);
- RandomAccessFile raf = new RandomAccessFile(tifFile, "r");
- long numSplits = 2; //from user input, extract it from args
- long sourceSize = raf.length();
- long bytesPerSplit = sourceSize/numSplits ;
- long remainingBytes = sourceSize % numSplits;
- long maxReadBufferSize = 1 * 1610612736; //1500mb
- for(int destIx = 1; destIx <= numSplits; destIx++) {
- BufferedOutputStream bw = new BufferedOutputStream(new FileOutputStream(fil + "_" + destIx + type));
- if(bytesPerSplit > maxReadBufferSize) {
- long numReads = bytesPerSplit/maxReadBufferSize;
- long numRemainingRead = bytesPerSplit % maxReadBufferSize;
- for(int i=0; i<numReads; i++) {
- readWrite(raf, bw, maxReadBufferSize);
- }
- if(numRemainingRead > 0) {
- readWrite(raf, bw, numRemainingRead);
- }
- }
- else {
- readWrite(raf, bw, bytesPerSplit);
- }
- bw.close();
- }
- if(remainingBytes > 0) {
- BufferedOutputStream bw = new BufferedOutputStream(new FileOutputStream("split." + numSplits+1));
- readWrite(raf, bw, remainingBytes);
- bw.close();
- }
- raf.close();
- }
- static void readWrite(RandomAccessFile raf, BufferedOutputStream bw, long numBytes) throws IOException {
- byte[] buf = new byte[(int) numBytes];
- int val = raf.read(buf);
- if(val != -1) {
- bw.write(buf);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement