-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSplit.java
More file actions
72 lines (69 loc) · 1.84 KB
/
Copy pathSplit.java
File metadata and controls
72 lines (69 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
public class Split{
public static void main(String[] args){
if(args.length!=2){
System.err.println("usage: java Split pathname partsize");
return;
}
File srcFile = new File(args[0]);
if(!srcFile.exists()){
System.err.println("File does not exist.");
return;
}
if(srcFile.isDirectory()){
System.err.println("The specified file is a directory.");
return;
}
long srcSize = srcFile.length(); // unit byte.
long partSize = 0;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try{
partSize = (long)(Float.parseFloat(args[1]) * 1024 * 1024); // unit megabyte.
int quotient = (int)(srcSize/partSize);
int partCount = srcSize % partSize==0 ? quotient : quotient+1;
int partOffset = 0;
File partFile = new File(srcFile.getName() + "_part_" + partOffset);
int read;
int partFileSize = 0;
bis = new BufferedInputStream(new FileInputStream(srcFile));
bos = new BufferedOutputStream(new FileOutputStream(partFile));
while((read=bis.read())!=-1){
if(partFileSize == partSize){
partOffset++;
partFile = new File(srcFile.getName() + "_part_" + partOffset);
partFileSize = 0;
bos.flush();
bos.close();
bos = new BufferedOutputStream(new FileOutputStream(partFile));
}
bos.write(read);
partFileSize ++;
}
}catch(NumberFormatException nfe){
nfe.printStackTrace();
}catch(IOException ioe){
ioe.printStackTrace();
}finally{
if(bis!=null){
try{
bis.close();
}catch(IOException ioe){
ioe.printStackTrace();
}
}
if(bos!=null){
try{
bos.close();
}catch(IOException ioe){
ioe.printStackTrace();
}
}
}
}
}