I have this line of code.And numbers.txt has a string of these numbers : 123456789 .Running it prints : 235689 . Why? What does .read() do ? And when 'while(fin.read() > -1 )' is TRUE? Also about the Exception why i get IOException Error when not using it since the program is correct?
import java.io.*;
public class Read {
public static void main(String[] args) throws Exception {
FileReader fin = new FileReader("numbers.txt");
while(fin.read() > -1 ){
System.out.print((char) fin.read());
System.out.print((char) fin.read());
}
fin.close();
}
}
You are discarding every third character. I suggest storing the character you read and printing that.
for(int ch; (ch = fin.read()) > -1; )
System.out.print((char) ch);
I suggest you use a BufferedReader instead like this
try(BufferedReader br = new BufferedReader(new FileReader("numbers.txt"))) {
for(String line; (line = br.readLine()) != null; )
System.out.println(line);
} // closes the br
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With