RU beehive logo ITEC dept promo banner
ITEC 120
2012fall
dbraffitt
ibarland

homeinfolectslabsexamshws
tutor/PIsbreeze (snow day)
Object120 + its docsjava.lang docsjava.util docs

lab11b
Reading from a file

reading files and web pages

We've seen we can use java.util.Scanners to read input from the keyboard:

java.util.Scanner scnr = new java.util.Scanner( System.in );
System.out.print("Shout! (or ^C to indicate end-of-file) ");
while (scnr.hasNext()) {
  String msg = scnr.next();
  System.out.println( msg + ", echo, echo..." );
  System.out.print( "Shout more! (or ^C to indicate end-of-file) " );
  }
Really, a Scanner is an object who can group individual characters into meaningful units (words, doubles, etc.).

But a Scanner can read information from any source of characters: they can read information from a file, a web page, or even a String inside your program. When you call the constructor, you just pass in a source of characters.



1 In this case, if you try it out, you realize that web pages are written in html; in html the notion of the “next word” is different from where the next whitespace occurs. Indeed, people have written specialized classes which extend Scanner to so that the next method returns the next word in a way that is appropriate for html.      

homeinfolectslabsexamshws
tutor/PIsbreeze (snow day)
Object120 + its docsjava.lang docsjava.util docs


©2012, Ian Barland, Radford University
Last modified 2012.Nov.08 (Thu)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Powered by PLT Scheme