Determine end-of-file using Java's Scanner class
I am trying to read text from a text file. 开发者_运维百科I need help figuring out when the end of file has occurred. How can I determine this in Java?
FileInputStream istream = new FileInputStream("\""+filename+"\"");      
Scanner input = new Scanner(istream);
while(EOF != true)
{
 ....
}
You can check using hasNextLine():
Scanner input = new Scanner(new File("\""+filename+"\""));
while(input.hasNextLine())
{
   String data = input.nextLine();
}
Line based retrieval may be what you want, but token based can also be useful.
You can see in documentation of Scanner
public boolean hasNext()
Returns
trueif thisScannerhas another token in its input. This method may block while waiting for input to scan. TheScannerdoes not advance past any input.Specified by:
hasNextin interfaceIterator<String>Returns:
trueif and only if thisScannerhas another tokenThrows:
IllegalStateException- if thisScanneris closedSee Also:
Iterator
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论