Repository navigation
Expand file tree
/
Copy pathFileSplitter.java
More file actions
94 lines (78 loc) · 3.07 KB
/
Copy pathFileSplitter.java
File metadata and controls
94 lines (78 loc) · 3.07 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package com.example;
import java.io.RandomAccessFile;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
public class FileSplitter {
public static void main(String[] args) {
String fPath = "C:\\tmp\\test-file-new.txt";
try {
split(fPath);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void split(final String fPath) throws Exception {
RandomAccessFile raf = new RandomAccessFile(fPath, "r");
long length = raf.length();
raf.close();
final long byteSize = length/3;
long remainder = length%3;
System.out.println("File Length: " + length + ", Byte Size: " + byteSize + ", R: " + remainder);
long startOffset = 0L;
for(int loop = 0; loop < 3; loop++) {
final long sOffset = startOffset;
//final long eOffset = startOffset + byteSize;
Runnable runnable = new Runnable() {
public void run() {
FileSplitter fileSplitter = new FileSplitter();
try {
Runtime runtime = Runtime.getRuntime();
String tName = Thread.currentThread().getName();
System.out.println("[" + tName + "]Total Mem: " + (runtime.totalMemory()/1000) + ", Free Mem: " + (runtime.freeMemory()/1000));
fileSplitter.readChunk(sOffset, byteSize, fPath);
System.out.println("[" + tName + "]Total Mem: " + (runtime.totalMemory()/1000) + ", Free Mem: " + (runtime.freeMemory()/1000));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
Thread executor = new Thread(runnable, "THREAD-" + loop);
executor.start();
startOffset = sOffset + byteSize;
}
Runtime runtime = Runtime.getRuntime();
System.out.println("DONE. Total Mem: " + (runtime.totalMemory()/1000) + ", Free Mem: " + (runtime.freeMemory()/1000));
}
public void readChunk(long start, long byteSize, String filePath) throws Exception {
System.out.println("Data: " + start + "-" + byteSize);
byte[] byteData = new byte[(int) byteSize];
RandomAccessFile raf = new RandomAccessFile(filePath, "r");
raf.seek(start);
Runtime runtime = Runtime.getRuntime();
String tName = Thread.currentThread().getName();
System.out.println("[" + tName + "]Total Mem: " + (runtime.totalMemory()/1000) + ", Free Mem: " + (runtime.freeMemory()/1000));
/*boolean readNext = true;
String line = null;
long bytesRead = 0L;
StringBuilder sBuilder = new StringBuilder();*/
/*while (readNext && (line = raf.readLine()) != null ) {
sBuilder.append(line);
sBuilder.append(System.lineSeparator());
bytesRead += line.length();
if(bytesRead >= byteSize) {
readNext = false;
}
}*/
//System.out.println("Bytes Read: " + bytesRead);
raf.readFully(byteData);
raf.close();
//Files.lines(null).
Path parent = Paths.get(filePath, "").getParent();
Files.write(parent.resolve("file-" + start + ".txt"), byteData, StandardOpenOption.CREATE);
System.out.println("[" + tName + "]Total Mem: " + (runtime.totalMemory()/1000) + ", Free Mem: " + (runtime.freeMemory()/1000));
}
}