Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
73 lines (58 loc) · 2.18 KB

File metadata and controls

73 lines (58 loc) · 2.18 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
# BufferedReader
Automagically prefetches reads larger chunks than immediately required.
Faster than a non-buffered reader if you are going to read the whole file anyways.
http://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html
Is itself a reader, and simply acts as a wraper around another `Reader`:
http://docs.oracle.com/javase/7/docs/api/java/io/Reader.html
The most common reader to use wrap around is `FileReader`.
*/
import java.io.BufferedReader;
import java.io.StringReader;
import java.io.IOException;
public class BufferedReaderCheat {
public static void main(String[] args) {
/*
# Read file line-by-line
# readLine
A line is considered to be terminated by any one of a line feed ('\n'),
a carriage return ('\r'), or a carriage return followed immediately by a linefeed.
Using BufferedReader + FileReader is the most common combo.
Readers must be used instead of the stream because this operation
is encoding dependant.
*/
{
BufferedReader br = new BufferedReader(new StringReader("ab\ncd\n"));
try {
assert(br.readLine().equals("ab"));
assert(br.readLine().equals("cd"));
assert(br.readLine() == null);
} catch (IOException e) {}
// Loop usage.
{
// Good method: line has small scope.
/*
for (String line; (line = br.readLine()) != null;) {
// line
}
*/
// Saner version.
/*
String line = null;
do {
line = br.readLine();
} while (line != null);
*/
// Another possibility.
/*
String line;
while ((line = br.readLine()) != null) {}
*/
}
/*
Check if reader is at EOF: not possible without reading.
http://stackoverflow.com/questions/3714090/how-to-see-if-a-reader-is-at-eof
*/
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.