-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChild.java
155 lines (129 loc) · 3.06 KB
/
Child.java
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package pkg1;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
public class Child extends Process implements Runnable{
private int[] nums;
private ByteArrayOutputStream out = new ByteArrayOutputStream();
private String fileName = "";
public Child(String pFileName){
fileName = pFileName;
greeting(fileName);
if(!readFile(fileName)){
System.out.println("There was a problem reading in the file");
}
else{
Arrays.sort(nums);
String tmp = setupOutputData();
System.out.println(fileName + ": Writing this " +tmp);
try {
out.flush();
out.write(tmp.getBytes());
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* Takes in the file name and attempts to read in the ints from the
* file into an int array.
*
* @param fileName
* @return retVal: successful or not.
*/
private boolean readFile(String fileName){
boolean retVal = true;
ArrayList<Integer> temp = new ArrayList<Integer>();
BufferedReader inReader = null;
FileReader inFile = null;
try {
inFile = new FileReader(new File(System.getProperty("user.home") + "/texts/" +fileName));
inReader = new BufferedReader(inFile);
} catch (FileNotFoundException e) {
retVal = false;
e.printStackTrace();
}
inReader = new BufferedReader(inFile);
String s = "";
try {
s = inReader.readLine();
} catch (IOException e) {
retVal = false;
e.printStackTrace();
}
while(s!=null){
int num = Integer.parseInt(s);
System.out.println(fileName+ ": Next int: " + num);
temp.add(num);
try {
s = inReader.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// takes Integers from ArrayList and puts them into int array
nums = new int[temp.size()];
int j = 0;
for(Integer i: temp){
nums[j] = i.intValue();
j++;
}
return retVal;
}
private String setupOutputData(){
String temp = "";
for(int i = 0; i < nums.length; i++){
temp += nums[i];
if(i+1 != nums.length){
temp+= ",";
}
}
return temp;
}
public String getFileName(){
return fileName;
}
private void greeting(String f){
System.out.println("Child created. I will sort file " + f);
}
public ByteArrayOutputStream getOutputStream() {
// TODO Auto-generated method stub
return out;
}
@Override
public InputStream getInputStream() {
// TODO Auto-generated method stub
return null;
}
@Override
public InputStream getErrorStream() {
// TODO Auto-generated method stub
return null;
}
@Override
public int waitFor() throws InterruptedException {
// TODO Auto-generated method stub
return 0;
}
@Override
public int exitValue() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void run() {
// TODO Auto-generated method stub
}
}