This repository was archived by the owner on Jul 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP30_OddEven_Thread.java
More file actions
63 lines (62 loc) · 1.33 KB
/
P30_OddEven_Thread.java
File metadata and controls
63 lines (62 loc) · 1.33 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
// Emmanuel Jojy
// S3 CSE A
// Roll no.: 53
// Separate odd and even numbers using
// files and thread concept.
import java.io.*;
class MyThread extends Thread{
public void run(){
try{
FileInputStream fin = new FileInputStream("fin.txt");
FileOutputStream feven = new FileOutputStream("feven.txt");
FileOutputStream fodd = new FileOutputStream("fodd.txt");
String s = "";
for(int i = fin.read(); i != -1; i = fin.read()){
i = i - '0';
if(i >= 0 && i <= 9){
s = s + i;
}
else{
try
{
i = Integer.parseInt(s);
}
catch(Exception e){
s = "";
continue;
}
if(i % 2 == 0){
for(int j:s.toCharArray())
feven.write((int)j);
feven.write('\n');
}
else{
for(int j:s.toCharArray())
fodd.write((int)j);
fodd.write('\n');
}
s = "";
}
}
}
catch(FileNotFoundException e){
System.out.println("Could not locate file. " + e);
}
catch(IOException e){
System.out.println("Write Error. " + e);
}
}
}
public class P30_OddEven_Thread{
public static void main(String[] args){
MyThread obj = new MyThread();
obj.start();
try{
obj.join();
}
catch(InterruptedException e){
System.out.println("Thread Exception. " + e);
}
System.out.println("Successfully separated odd and even numbers to files.");
}
}