forked from meganjkim/FileWriter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMilo.java
More file actions
35 lines (33 loc) · 1.09 KB
/
Milo.java
File metadata and controls
35 lines (33 loc) · 1.09 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
import java.util.*;
import java.io.*;
public class Milo {
public static void writeToFile(String s, String fileName) throws IOException {
// String fileName = "milo.txt";
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(fileName)));
pw.print(s);
pw.close();
}
public static String readFromFile(String fileName) throws IOException {
// String fileName = "milobear.txt";
BufferedReader br = new BufferedReader(new FileReader(fileName));
String s = br.readLine();
br.close();
return s;
}
public static int countCharacters (String fileName) throws IOException
{
int count = 0;
BufferedReader br = new BufferedReader(new FileReader(fileName));
while (br.ready())
{
br.read();
count++;
}
return count;
}
public static void main(String[] args) throws IOException {
// writeToFile("milo","milo.txt");
System.out.println(readFromFile ("milo.txt"));
System.out.println(countCharacters("milo.txt"));
}
}