-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJava166_stream.java
More file actions
75 lines (65 loc) · 1.54 KB
/
Copy pathJava166_stream.java
File metadata and controls
75 lines (65 loc) · 1.54 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package java0908_stream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class Java166_stream {
public static void main(String[] args) {
File file = new File("sample.txt");
FileReader fr = null;
int data;
try {
fr = new FileReader(file);
// read() : 파일의 끝일때 -1을 리턴한다.
while ((data = fr.read()) != -1) {
System.out.print((char) data);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("//////////////////////////////");
BufferedReader br = null;
try {
fr = new FileReader(file);
br = new BufferedReader(fr);
String line = "";
// 파일의 끝이면 readLine()는 null를 리턴한다.
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("///////////////////////////////");
Scanner sc = null;
try {
sc = new Scanner(file);
while (sc.hasNextLine()) {
System.out.println(sc.nextLine());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
sc.close();
}
}
}