-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileOperations.java
More file actions
134 lines (123 loc) · 3.64 KB
/
FileOperations.java
File metadata and controls
134 lines (123 loc) · 3.64 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package map_reduce_demo;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.*;
public class FileOperations {
public static void separateFile(String file, int num){
File ffile = new File(file);
long lon = ffile.length()/((int)num) +1L;
int index = file.lastIndexOf(".");
String fname = file.substring(0, index);
String extend = file.substring(index+1);
try{
RandomAccessFile raf1 = new RandomAccessFile(file,"r");
byte[] bytes = new byte[1024*4];
byte[] lbyte = new byte[1];
byte[] encode = {(byte) 0xfe,(byte) 0xff};
byte[] newline = {0x00,0x0d,0x00,0x0a};
char key,value,l;
raf1.read(encode);
key = raf1.readChar();
value = raf1.readChar();
l = raf1.readChar();
LinkedList<Byte> newline_windows = new LinkedList<Byte>();
newline_windows.add(newline[0]);
newline_windows.add(newline[1]);
newline_windows.add(newline[2]);
newline_windows.add(newline[3]);
int len = -1;
for(int i = 0; i < num; i++){
String name = fname + i + "."+ extend;
File file2 = new File(name);
RandomAccessFile raf2 = new RandomAccessFile(file2,"rw");
raf2.write(encode);
raf2.writeChar(key);
raf2.writeChar(value);
raf2.writeChar(l);
while((len = raf1.read(bytes)) != -1){
raf2.write(bytes, 0, len);
if(raf2.length() > lon){
LinkedList<Byte> link = new LinkedList<Byte>();
do{
if(raf1.read(lbyte)==-1)
break;
link.add(lbyte[0]);
if(link.size()>4)
link.removeFirst();
raf2.write(lbyte);
}while(!link.equals(newline_windows));
break;
}
}
raf2.close();
}
raf1.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static boolean mergeFiles(String[] fpaths, String resultPath) {
if (fpaths == null || fpaths.length < 1 ) {
return false;
}
if (fpaths.length == 1) {
return new File(fpaths[0]).renameTo(new File(resultPath));
}
File[] files = new File[fpaths.length];
for (int i = 0; i < fpaths.length; i ++) {
files[i] = new File(fpaths[i]);
if ( !files[i].exists() || !files[i].isFile()) {
return false;
}
}
File resultFile = new File(resultPath);
byte[] k = {(byte) 0xfe,(byte) 0xff};
char[] c = new char[3];
try {
RandomAccessFile raf1 = new RandomAccessFile(resultFile,"rw");
raf1.write(k);
byte[] bytes = new byte[1024*4];
for(int i = 0; i < fpaths.length; i++){
RandomAccessFile raf2 = new RandomAccessFile(files[i],"r");
if(i != 0){
raf2.read(k);
c[0] = raf2.readChar();
c[1] = raf2.readChar();
c[2] = raf2.readChar();
}else{
raf2.read(k);
}
int len;
while((len = raf2.read(bytes)) != -1){
raf1.write(bytes,0 , len);
}
raf2.close();
}
raf1.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*
for (int i = 0; i < fpaths.length; i ++) {
files[i].delete();
}*/
return true;
}
public static void main(String[] args){
separateFile("test.txt",10);
String[] fpath = new String[10];
for(int i = 0;i<10;i++){
fpath[i] = "test" + i + ".txt";
}
mergeFiles(fpath,"test_merge.txt");
}
}