forked from m4rkyma/FileWriter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileWriters.java
More file actions
40 lines (37 loc) · 1.21 KB
/
FileWriters.java
File metadata and controls
40 lines (37 loc) · 1.21 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
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.BufferedReader;
public class FileWriters{
public static String readFile (String fileName) throws IOException{
FileReader fr = new FileReader (fileName);
StringBuilder s = new StringBuilder ();
while (fr.ready())
{
s.append((char)fr.read());
}
fr.close();
return s.toString();
}
public static void writeFile (String str, String fileName) throws IOException{
FileWriter fw = new FileWriter (fileName);
fw.write(str);
fw.close();
}
public static int countChars (String inputFile) throws IOException{
BufferedReader reader = new BufferedReader (new FileReader (inputFile));
int charCount = 0;
while (reader.read() != -1)
{
charCount++;
}
reader.close();
return charCount;
}
public static void main(String[] args) throws IOException {
System.out.println(readFile ("file.txt"));
writeFile ("poop", "file.txt");
System.out.println (countChars ("file.txt"));
}
}