-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFileWriters.java
More file actions
27 lines (25 loc) · 805 Bytes
/
FileWriters.java
File metadata and controls
27 lines (25 loc) · 805 Bytes
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
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
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 void main(String[] args) throws IOException {
System.out.println(readFile ("file.txt"));
writeFile ("poop", "file.txt");
}
}