-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeFiles.java
More file actions
138 lines (116 loc) · 3.69 KB
/
Copy pathMergeFiles.java
File metadata and controls
138 lines (116 loc) · 3.69 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
135
136
137
138
package dummypkg;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
class SharedInteger {
private boolean available = false;
public File processingFile;
public Byte vectorBytes;
SharedInteger() {
}
public synchronized Byte get() {
while (available == false) {
try {
wait();
} catch (InterruptedException e) {
}
}
available = false;
notify();
return vectorBytes;
}
public synchronized void put(Byte value) {
while (available == true) {
try {
wait();
} catch (InterruptedException e) {
}
}
vectorBytes = value;
available = true;
notify();
}
}
class Producer extends Thread {
private SharedInteger sharedInteger;
public Producer(SharedInteger c) {
sharedInteger = c;
}
public void run() {
FileInputStream fis = null;
Vector<Byte> vectorBytes = new Vector<Byte>();
try {
fis = new FileInputStream(sharedInteger.processingFile);
while (fis.available() != 0) {
sharedInteger.put((byte) fis.read());
//vectorBytes.add((byte) fis.read());
}
System.out.println(Thread.currentThread().getName());
} catch (Exception e) {
}
}
}
class Consumer extends Thread {
private SharedInteger sharedInteger;
private FileOutputStream fos;
public Consumer(SharedInteger c) {
sharedInteger = c;
}
public void run() {
File newFile = sharedInteger.processingFile;
System.out.println(newFile.getParentFile());
try {
fos = new FileOutputStream(newFile.getParentFile()+"1\\out.txt", true);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Byte v = sharedInteger.get();
while(v!=null) {
try {
if (null != v) {
writeToAFile(v);
}
} catch (IOException e) { // TODO Auto-generated catch block
e.printStackTrace();
}
v=sharedInteger.get();
}
}
public void writeToAFile(Byte contents) throws IOException {
//for (int i = 0; i < contents.size(); i++) {
System.out.println(Thread.currentThread());
fos.write(contents);
fos.flush();
//}
}
}
public class MergeFiles {
public static void main(String[] args) throws FileNotFoundException {
File folder = new File("C:\\Users\\lenovo\\Desktop\\vector\\in_file");
File[] fileList = folder.listFiles();
int countOfFiles = fileList.length;
SharedInteger c;
List<Producer> pList = new ArrayList<Producer>();
List<Consumer> cList = new ArrayList<Consumer>();
Producer p1 = null;
Consumer c1 = null;
for (int i = 0; i < countOfFiles; i++) {
c = new SharedInteger();
c.processingFile = fileList[i];
p1 = new Producer(c);
p1.setName("Producer--" + i);
pList.add(p1);
pList.get(i).start();
c1 = new Consumer(c);
c1.setName("Consumer--" + i);
cList.add(c1);
cList.get(i).start();
}
}
}