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?
InputStreamReader question
Moderator: admin
-
- Posts: 1
- Joined: Sat Aug 22, 2020 1:40 pm
- Contact:
-
- Site Admin
- Posts: 10064
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: InputStreamReader question
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.
Who is online
Users browsing this forum: No registered users and 89 guests