InputStreamReader question

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
swarbrick85
Posts: 1
Joined: Sat Aug 22, 2020 1:40 pm
Contact:

InputStreamReader question

Post by swarbrick85 »

Given that the file test.txt contains : 12345678 What will the following code print when compiled and run?

public static void main(String[] args) throws Exception{
try(FileInputStream fis = new FileInputStream("c:\\temp\\test.txt");
InputStreamReader isr = new InputStreamReader(fis)){
while(isr.ready()){
isr.skip(1);
int i = isr.read();
char c = (char) i;
System.out.print(c);
} } }

Why does it print 2468 instead of 2 or something else?

admin
Site Admin
Posts: 10064
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: InputStreamReader question

Post by admin »

Because of the line isr.skip(1). You are skipping 1 char before reading 1 in a loop.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 89 guests