I'm new to Java, what does it mean for while(t!=-1) [READFILE]? -
i'm new java , i'm learning how read file .txt. i've stumbled across numerous notes lecture , i'm wondering why has while(t!=-1) when i'm trying read file small , simple java code.
thanks answers. :)
edit: thank guys answer. understand -1 means end-of-file, loop continue until file read. have -1? kind of specific rule in java? thank you.
import java.io.*; public class test2 { public static void main(string[] args) throws exception{ filereader inone = new filereader("myfile.txt"); int t=inone.read(); while (t!=-1) { system.out.print((char)t); t=inone.read(); } } }
i guess talking loop reads file contents.
usually looks like:
while (int symbol = inputstreamref.read()!=-1) { // }
inputstreamref
inputstream subclass object.
inputstreamref.read()
- reads file, , returns -1
when end of file has been reached.
while (symbol != -1)
means end of file has not been reached , need continue reading file contents.
update
according code you've posted, check comments:
// initializing reader file filereader inone = new filereader("myfile.txt"); // reading first byte 't' variable int t=inone.read(); // if file empty, 't' contain -1 // , loop won't start while (t!=-1) { // if t not contain -1, end of file not reached // printing byte system.out.print((char)t); // reading next byte t=inone.read(); }
Comments
Post a Comment