-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfileReader.java
More file actions
33 lines (30 loc) · 1.14 KB
/
fileReader.java
File metadata and controls
33 lines (30 loc) · 1.14 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
import java.io.FileNotFoundException;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
//file reader class uses buffered reader to read a passed in string that has the name of a text file within your workspace
//the buffered readre then is stored in a "val" variable that is casted into a char which is concatnated to a local string variable
public class fileReader {
public static void main(String[] args) throws IOException {
System.out.println(readText("282^2I7%74.txt"));
}
public static String readText(String fileName) throws IOException {
String textRead = "";
BufferedReader br = new BufferedReader(new FileReader(fileName));
while (br.ready()) {
int val = br.read();
textRead += (char) val;
}
br.close();
return textRead;
}
public int countChar(String fileName) throws IOException {
int charCount = 0;
BufferedReader reader = new BufferedReader(new FileReader(fileName));
while (reader.read() != -1) {
charCount++;
}
reader.close();
return charCount;
}
}