Repository navigation
Expand file tree
/
Copy pathSet7_FileIO.java
More file actions
157 lines (118 loc) · 4.36 KB
/
Copy pathSet7_FileIO.java
File metadata and controls
157 lines (118 loc) · 4.36 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import java.util.*;
import java.io.*;
class writeFile extends Thread {
String filename, data;
writeFile(String filename, String data) {
this.filename = filename;
this.data = data;
}
public void run() {
writeFile.write(filename, data);
}
public static void write(String filename, String data) {
try {
File f = new File(filename);
BufferedWriter bw = new BufferedWriter(new FileWriter(f));
for (int i = 0; i < data.length(); i++) {
bw.write(data.charAt(i));
}
bw.close();
} catch (IOException e) {
System.out.println(e);
} finally {
System.out.println("Data successfully inserted!");
}
}
}
class readFile extends Thread {
String filename;
readFile() {
System.out.println("Thread Created!");
}
readFile(String filename) {
this.filename = filename;
}
public void run() {
System.out.println(read(filename));
}
public static String read(String filename) {
StringBuilder file_data = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(filename));
String line;
while ((line = br.readLine()) != null) {
file_data.append(line).append("\n");
}
br.close();
} catch (IOException e) {
System.out.println(e);
}
return file_data.toString();
}
}
class copyFile extends Thread {
String sourcefile, targetfile;
copyFile(String sourcefile, String targetfile) {
this.sourcefile = sourcefile;
this.targetfile = targetfile;
}
public void run() {
copy(sourcefile, targetfile);
}
public static void copy(String readfile, String writefile) {
System.out.println("Enter the source file:");
String sourcedata = readFile.read(readfile);
writeFile.write(writefile, sourcedata);
}
}
public class Set7_FileIO extends Thread {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int choice;
try {
while (true) {
System.out.println("Enter an option:");
System.out.println(
"\n1. Read contents of a file\n2. Write to a file\n3. Copy contents of a file to another\n4. Exit...");
String choice2 = sc.nextLine();
choice = Integer.parseInt(choice2);
String filename, targetfile, sourcefile;
switch (choice) {
case 1:
System.out.println("Enter the file name to read:");
filename = sc.nextLine();
readFile threadRead = new readFile(filename);
threadRead.start();
break;
case 2:
System.out.println("Enter the file name to write! (with extension)");
filename = sc.nextLine();
System.out.println("Enter the data to write into the file:");
String data = sc.nextLine();
writeFile threadWrite = new writeFile(filename, data);
threadWrite.start();
break;
case 3:
System.out.println("Enter the source file name! (with extension)");
sourcefile = sc.nextLine();
System.out.println("Enter the source file name! (with extension)");
targetfile = sc.nextLine();
copyFile threadCopy = new copyFile(sourcefile, targetfile);
threadCopy.start();
break;
case 4:
return;
default:
System.out.println("Invalid Choice!\n");
break;
}
}
} finally {
sc.close();
}
}
@Override
public void run() {
throw new UnsupportedOperationException("Unimplemented method 'run'");
}
}