forked from biblelamp/JavaExercises
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileWriteRead.java
More file actions
33 lines (29 loc) · 825 Bytes
/
FileWriteRead.java
File metadata and controls
33 lines (29 loc) · 825 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
28
29
30
31
32
33
/**
* Java. Level 1. Lesson 3. Example of writing/reading file
*
* @author Sergey Iryupin
* @version dated Mar 12, 2018
*/
import java.io.FileWriter;
import java.io.FileReader;
import java.io.IOException;
class FileWriteRead {
public static void main(String[] args) {
// writing to file
try (FileWriter file = new FileWriter("text.txt")) {
file.write("Hello\nJava");
//file.close();
} catch (IOException ex) {
ex.printStackTrace();
}
// reading from file
try (FileReader file = new FileReader("text.txt")) {
int b;
while ((b = file.read()) != -1)
System.out.print((char)b);
//file.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}