File tree 2 files changed +48
-25
lines changed
2 files changed +48
-25
lines changed Original file line number Diff line number Diff line change @@ -9,25 +9,37 @@ public class Child {
9
9
public static void main (String [] args ) throws java .io .IOException , InterruptedException {
10
10
int ch ;
11
11
System .out .print ("Let's echo: " );
12
+
13
+ // echo out whatever comes from system in
12
14
while ((ch = System .in .read ()) != '\n' )
13
15
System .out .print ((char ) ch );
14
- BufferedWriter bw =new BufferedWriter (
16
+
17
+ // the Child process will be logged in a file here
18
+ BufferedWriter bufferedWriter = new BufferedWriter (
15
19
new FileWriter (new File ("mycal2022.txt" )));
16
- int cont = 0 ;
17
- while (cont < 49 ) {
18
- System .out .println (cont ++);
19
- cont %= 50 ;
20
- bw .write (cont + "\n " );
21
-
22
- sleep (200 );
23
-
24
- if (System .in .available () > 0 ) {
25
- ch = System .in .read ();
26
- if (ch == '*' ) {
27
- cont = 0 ;
28
- }
20
+
21
+ int cont = 0 ;
22
+ while (cont <= 50 ) {
23
+ System .out .println (cont ++);
24
+
25
+ // add this to loop the counting (useful for testing)
26
+ //cont %= 50;
27
+
28
+ // added this to log data in local file + EOL
29
+ bufferedWriter .write (cont + "\n " );
30
+ bufferedWriter .flush ();
31
+
32
+ sleep (1000 );
33
+
34
+ if (System .in .available () > 0 ) {
35
+ ch = System .in .read ();
36
+
37
+ // reset counter if asterisk
38
+ if (ch == '*' ) {
39
+ cont = 0 ;
29
40
}
30
41
}
31
- bw .close ();
42
+ }
43
+ bufferedWriter .close ();
32
44
}
33
45
}
Original file line number Diff line number Diff line change 2
2
import java .util .concurrent .TimeUnit ;
3
3
4
4
public class Parent {
5
- public static void main (String [] args ) throws IOException {
5
+ public static void main (String [] args ) throws IOException {
6
6
Runtime runtime = Runtime .getRuntime ();
7
7
Process process = null ;
8
8
@@ -43,15 +43,26 @@ public static void main(String[] args) throws IOException {
43
43
// send to file
44
44
bufferedWriter .write (line );
45
45
bufferedWriter .flush ();
46
- while (line != null ) {
47
- System .out .println (line + " - " + Integer .parseInt (line ));
48
- line = br .readLine ();
49
- if (Integer .parseInt (line ) == 37 ) {
50
- writer .write ('*' );
51
- writer .flush (); // needed because of the buffered output
52
- System .out .println ("sent message" );
53
- }
54
- }
46
+
47
+ while (line != null ) {
48
+ // send to screen
49
+ System .out .println (line );
50
+
51
+ // send to file + EOL
52
+ bufferedWriter .write (line + "\n " );
53
+ bufferedWriter .flush ();
54
+
55
+ // read next line
56
+ line = bufferedReader .readLine ();
57
+
58
+ // this will force the reset of the counter
59
+ // the program will therefore never end
60
+ if (Integer .parseInt (line ) == 37 ) {
61
+ writer .write ('*' );
62
+ writer .flush (); // needed because of the buffered output
63
+ System .out .println ("sent message" );
64
+ }
65
+ }
55
66
process .destroy ();
56
67
}
57
68
}
You can’t perform that action at this time.
0 commit comments