forked from freesampul/ReadWrite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadwrite.java
More file actions
47 lines (43 loc) · 1.38 KB
/
readwrite.java
File metadata and controls
47 lines (43 loc) · 1.38 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
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
// Reads a file
public class readwrite {
public String read(String inputFile) throws IOException {
String str = "";
File file = new File(inputFile);
BufferedReader reader = new BufferedReader(new FileReader(file));
String line;
while ((line = reader.readLine()) != null) {
str += line;
}
reader.close();
return str;
}
// Asks for a file and a text string, writes the string into the file
public void write(String codeFile, String text) throws IOException {
try (PrintWriter writer = new PrintWriter(codeFile)) {
writer.println(text);
} catch (IOException e) {
e.printStackTrace();
}
}
// public static void main(String[] args) {
// try {
// readwrite rw = new readwrite("input.txt");
// rw.writeCode("output.txt", "aidan rahill is my baby boy i love him");
// } catch (IOException e
public int countCharacters(String fileName) throws IOException {
int count;
try {
count = read(fileName).length();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
count = 0;
}
return count;
}
}