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

Sign up
Appearance settings

Commit 3f2d982

Browse files
Merge pull request #1 from coderolls/example/add-buffer-reader-examples
Add buffer reader examples
2 parents 0c46b12 + e020366 commit 3f2d982

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.coderolls;
2+
3+
import java.io.*;
4+
5+
public class BufferReaderExanple {
6+
7+
public static void main(String[] args) {
8+
BufferedReader bufferedReader = null;
9+
try {
10+
bufferedReader = new BufferedReader(new FileReader("F:\\sample-text.txt"));
11+
12+
} catch (FileNotFoundException e) {
13+
// TODO Auto-generated catch block
14+
e.printStackTrace();
15+
}
16+
17+
readFileCharacterByCharacter(bufferedReader);
18+
19+
readFileLineByLine(bufferedReader);
20+
21+
try {
22+
bufferedReader.close();
23+
} catch (IOException e) {
24+
e.printStackTrace();
25+
}
26+
}
27+
28+
/**
29+
* A method to read file content character by character using the BufferReader
30+
* read() method
31+
*
32+
* @param bufferedReader
33+
*/
34+
public static void readFileCharacterByCharacter(BufferedReader bufferedReader) {
35+
try {
36+
int i;
37+
//read each character using read() method and print it
38+
while((i=bufferedReader.read())!=-1){
39+
System.out.print((char)i);
40+
}
41+
} catch (IOException e) {
42+
e.printStackTrace();
43+
}
44+
}
45+
46+
/**
47+
* A method to read file content line by line using the BufferReader
48+
* readLine() method
49+
*
50+
* @param bufferedReader
51+
*/
52+
public static void readFileLineByLine(BufferedReader bufferedReader) {
53+
54+
try {
55+
String line;
56+
//read each line using readLine() method and print it
57+
while((line = bufferedReader.readLine()) != null){
58+
System.out.println(line);
59+
}
60+
}catch (IOException e) {
61+
e.printStackTrace();
62+
}
63+
}
64+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.coderolls;
2+
3+
import java.io.*;
4+
5+
/**
6+
* A java program to read file line by line using the
7+
* readLine() method of the BufferReader Class.
8+
*
9+
* @author Gaurav Kukade at coderolls.com
10+
*
11+
*/
12+
public class BufferReaderReadLineMethodExample {
13+
14+
public static void main(String[] args) {
15+
16+
BufferedReader bufferedReader = null;
17+
try {
18+
bufferedReader = new BufferedReader(new FileReader("F:\\sample-text.txt"));
19+
20+
String line;
21+
//read each line using readLine() method and print it
22+
while((line = bufferedReader.readLine()) != null){
23+
System.out.println(line);
24+
}
25+
26+
}catch (IOException e) {
27+
e.printStackTrace();
28+
}finally {
29+
try {
30+
bufferedReader.close();
31+
} catch (IOException e) {
32+
e.printStackTrace();
33+
}
34+
}
35+
}
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.coderolls;
2+
3+
import java.io.*;
4+
5+
/**
6+
* A java program to read file character by character using the
7+
* read() method of the BufferReader Class.
8+
*
9+
* @author Gaurav Kukade at coderolls.com
10+
*
11+
*/
12+
public class BufferReaderReadMethodExample {
13+
14+
public static void main(String[] args) {
15+
16+
BufferedReader bufferedReader = null;
17+
try {
18+
bufferedReader = new BufferedReader(new FileReader("F:\\sample-text.txt"));
19+
20+
int i;
21+
//read each character using read() method and print it
22+
while((i=bufferedReader.read())!=-1){
23+
System.out.print((char)i);
24+
}
25+
26+
}catch (IOException e) {
27+
e.printStackTrace();
28+
}finally {
29+
try {
30+
bufferedReader.close();
31+
} catch (IOException e) {
32+
e.printStackTrace();
33+
}
34+
}
35+
}
36+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /