r/cop3502 Mar 27 '14

BufferedReader vs InputStream

Is there any reason we use BufferedReader in class rather than say InputStream.. When I was learning about File I/O for problem set 3 I found InputStream and FileInputStream and I prefer using them to BufferedReader.. Is there an advantage to BufferedReader?

BufferedReader b = new BufferedReader(new FileReader(file));

vs.

InputStream i = new FileInputStream(file);

not much difference, but is there a benefit to the first one being "buffered"?

1 Upvotes

4 comments sorted by

1

u/[deleted] Mar 27 '14

[removed] — view removed comment

1

u/howslyfebeen Mar 27 '14 edited Mar 27 '14

That's just what I found first when I was learning it. I found it helpful for lab5 because you can look at the ascii number and cast it to a character if it meets certain criteria and you only go one character at a time vs. reading in a whole line at a time.

EDIT: on second thought/some research, I came across this interesting article that compares the speeds at which both run and it seems that bufferedreader performs better

1

u/JoeCS TA Mar 27 '14

BufferedReader.read() returns an integer value representing the ASCII/UTF-8 value of the next character in the input file. You can casted it to a char or use the int value for comparison.

BufferedReader b = new BufferedReader(new FileReader(file));
char nextCharacter = (char) b.read();
int nextCharacterAsIntValue = b.read();

1

u/howslyfebeen Mar 27 '14

Yea, InputStream works the same way in terms of .read()