Java Reference
In-Depth Information
12.32
Rewrite the code in the preceding question using a try-with-resources syntax.
12.33
How do you create a
Scanner
to read data from a file? What is the reason to define
throws Exception
in the main method in Listing 12.15, ReadData.java? What
would happen if the
close()
method were not invoked in Listing 12.15?
12.34
What will happen if you attempt to create a
Scanner
for a nonexistent file? What
will happen if you attempt to create a
PrintWriter
for an existing file?
12.35
Is the line separator the same on all platforms? What is the line separator on Windows?
12.36
Suppose you enter
45 57.8 789
, then press the
Enter
key. Show the contents of the
variables after the following code is executed.
Scanner input =
new
Scanner(System.in);
int
intValue = input.nextInt();
double
doubleValue = input.nextDouble();
String line = input.nextLine();
12.37
Suppose you enter
45
, press the
Enter
key,
57.8
, press the
Enter
key,
789
, and press
the
Enter
key. Show the contents of the variables after the following code is executed.
Scanner input =
new
Scanner(System.in);
int
intValue = input.nextInt();
double
doubleValue = input.nextDouble();
String line = input.nextLine();
Just like you can read data from a file on your computer, you can read data from a file
on the Web.
Key
Point
In addition to reading data from a local file on a computer or file server, you can also access
data from a file that is on the Web if you know the file's URL (Uniform Resource Locatorā
for the file
index.html
located on the Google Web server. When you enter the URL in a Web
browser, the Web server sends the data to your browser, which renders the data graphically.
FigureĀ 12.10 illustrates how this process works.
Client
Server
Web
Browser
Web
Server
Internet
Application
Program
Local files
F
IGURE
12.10
The client retrieves files from a Web server.
For an application program to read data from a URL, you first need to create a
URL
object
using the
java.net.URL
class with this constructor:
public
URL(String spec)
throws
MalformedURLException
1
try
{
2 URL url =
new
URL(
"http://www.google.com/index.html"
);
3 }
Search WWH ::
Custom Search