-
Notifications
You must be signed in to change notification settings - Fork 1
/
ManyFormsReading.java
111 lines (101 loc) · 3.92 KB
/
ManyFormsReading.java
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Scanner;
public class ManyFormsReading {
public static void main(String[] args) {
// lendo byte a byte
System.out.print("Lendo byte a byte: ");
try (FileInputStream fin = new FileInputStream("a.txt")) {
int i = fin.read();
while (i != -1) {
System.out.print(i);
i = fin.read();
if (i != -1)
System.out.print(", ");
}
System.out.println("");
} catch (Exception e) {
e.printStackTrace();
}
// Lendo caracter a caracter, especificando charset
System.out.print("Lendo caracter a caracter (ISO-8859-1): ");
try (FileInputStream fin = new FileInputStream("a.txt");
InputStreamReader r = new InputStreamReader(fin, "ISO-8859-1")) {
int i = r.read();
while (i != -1) {
System.out.print(i);
i = r.read();
if (i != -1)
System.out.print(", ");
}
System.out.println("");
} catch (Exception e) {
e.printStackTrace();
}
// Lendo caracter a caracter, especificando charset
System.out.print("Lendo caracter a caracter (UTF-8): ");
try (FileInputStream fin = new FileInputStream("a.txt");
InputStreamReader r = new InputStreamReader(fin, "UTF-8")) {
int i = r.read();
while (i != -1) {
System.out.print(i);
i = r.read();
if (i != -1)
System.out.print(", ");
}
System.out.println("");
} catch (Exception e) {
e.printStackTrace();
}
// lendo linhas inteiras com codificação ISO 8859-1
System.out.println("Lendo linhas inteiras (ISO 8859-1): ");
try (FileInputStream fin = new FileInputStream("a.txt");
InputStreamReader r = new InputStreamReader(fin, "ISO-8859-1");
BufferedReader br = new BufferedReader(r);) {
String line = br.readLine();
while (line != null) {
System.out.println(line);
line = br.readLine();
}
} catch (Exception e) {
e.printStackTrace();
}
// lendo linhas inteiras com codificação UTF-8
System.out.println("Lendo linhas inteiras (UTF-8): ");
try (FileInputStream fin = new FileInputStream("a.txt");
InputStreamReader r = new InputStreamReader(fin, "UTF-8");
BufferedReader br = new BufferedReader(r);) {
String line = br.readLine();
while (line != null) {
System.out.println(line);
line = br.readLine();
}
} catch (Exception e) {
e.printStackTrace();
}
//List<String> linhas = new ArrayList(1200000);
// lendo linhas inteiras com Scanner
System.out.println("Lendo linhas inteiras com Scanner (ISO 8859-1): ");
try (FileInputStream fin = new FileInputStream("a.txt");
Scanner s = new Scanner(fin, "ISO-8859-1")) {
while (s.hasNextLine()) {
String line = s.nextLine();
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
}
// lendo linhas inteiras com Scanner
System.out.println("Lendo linhas inteiras com Scanner (UTF-8): ");
try (FileInputStream fin = new FileInputStream("a.txt");
Scanner s = new Scanner(fin, "UTF-8")) {
while (s.hasNextLine()) {
String line = s.nextLine();
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}