-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathReadAndWrite.java
More file actions
43 lines (38 loc) · 1.2 KB
/
ReadAndWrite.java
File metadata and controls
43 lines (38 loc) · 1.2 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
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
public class ReadAndWrite {
StringBuilder sb;
public ReadAndWrite(String inputFile) throws IOException {
File file = new File(inputFile);
BufferedReader reader = new BufferedReader(new FileReader(file));
sb = new StringBuilder();
int character;
while ((character = reader.read()) != -1) {
sb.append((char) character);
}
reader.close();
}
public String getString() {
return sb.toString();
}
public void write(String strFile, String str) throws IOException {
PrintWriter writer = new PrintWriter(strFile);
writer.println(str);
writer.close();
}
public int countCharacters(String fileName) throws FileNotFoundException, IOException {
int count = 0;
File file = new File(fileName);
BufferedReader reader = new BufferedReader(new FileReader(file));
while (reader.ready()) {
count++;
reader.read();
}
reader.close();
return count - 1;
}
}