Page 1 of 1

InputStreamReader question

Posted: Sat Aug 22, 2020 1:44 pm
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?

Re: InputStreamReader question

Posted: Sat Aug 22, 2020 5:58 pm
by admin
Because of the line isr.skip(1). You are skipping 1 char before reading 1 in a loop.