-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathFileReadDemo.java
More file actions
79 lines (74 loc) · 2.18 KB
/
FileReadDemo.java
File metadata and controls
79 lines (74 loc) · 2.18 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
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
class ReadWriteJob implements Runnable{
private String filePath;
private String writePath;
ReadWriteJob(String filePath,String writePath){
this.filePath = filePath;
this.writePath = writePath;
}
@Override
public void run(){
System.out.println(Thread.currentThread().getName()+" Running ");
/* for(int i = 1 ; i<=5 ; i++)
{
System.out.println("I is "+i+" "+Thread.currentThread().getName());
}*/
FileOutputStream fo = null;
FileInputStream fi = null;
try {
fo = new FileOutputStream(writePath,true);
fi = new FileInputStream(filePath);
int singleByte = fi.read();
while(singleByte!=-1){
fo.write(singleByte);
singleByte = fi.read();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if(fo!=null){
try {
fo.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(fi!=null){
try {
fi.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
System.out.println(Thread.currentThread().getName()+" Going to End ");
}
}
public class FileReadDemo {
public static void main(String[] args) throws InterruptedException {
ReadWriteJob job1 = new ReadWriteJob("D:\\Java12to2WeekEnd\\Files\\Test1.txt", "D:\\Java12to2WeekEnd\\Files\\Write1.txt");
ReadWriteJob job2 = new ReadWriteJob("D:\\Java12to2WeekEnd\\Files\\Test2.txt", "D:\\Java12to2WeekEnd\\Files\\Write2.txt");
//ReadWriteJob job3 = new ReadWriteJob("D:\\Java12to2WeekEnd\\Files\\Test3.txt", "D:\\Java12to2WeekEnd\\Files\\Write3.txt");
/*Thread worker1 = new Thread(job1,"Worker1");
Thread worker2 = new Thread(job2,"Worker2");
Thread worker3 = new Thread(job3,"Worker3");
worker1.start();
worker2.start();
worker3.start();*/
Thread worker1 = new Thread(job1,"Worker1");
Thread worker2 = new Thread(job1,"Worker2");
//Thread worker3 = new Thread(job1,"Worker3");
worker1.start();
worker2.start();
/*worker1.join();
worker2.start();
worker3.start();*/
}
}