Java Reference
In-Depth Information
if (username.equals("admin") && new String(password).equals("swordfish")) {
c.writer().println("Access granted");
} else {
c.writer().println("Oops, didn't recognize you there");
}
}
}
2.
Executing this program in Eclipse's console will not work, as Eclipse does not provide a so-called
“interactive command-line.” You will need to export the class as a Runnable JAR file like you
did before. Make sure to select GetPassword as the launch configuration (execute the program in
Eclipse if this does not appear in the list).
3.
Once you have exported the program, you can test it using a Windows command-line. Note that
the characters you type for the password will not appear on the screen. See Figure 8-12.
fiGure 8-12  
How It Works
This class is fairly straightforward, as most methods of System.console() are self-explanatory. The
only thing you need to keep in mind is to first test whether you can access the console in your environ-
ment by performing a null check. Next, you use the readLine and readPassword methods to get user
input. Note that the latter does not return a string but an array of characters, which you thus convert to
a string to perform the username/password check.
We now turn our attention back to files and continue our discussion on working with file I/O in Java
in the following section.
input and output from files
You've already seen some example programs that dealt with files in Java. The file copying programs,
for instance, illustrated how you can use byte, character, and buffered streams to get input from and
send output to files.
However, there's more to file I/O than just taking and dropping contents from and into files. Files
can be checked for existence, deleted, moved, copied, created, can contain metadata of various sorts,
and can be organized into directories, which are also traversable in Java. The following sections
teach you how to handle all of these things.
 
Search WWH ::




Custom Search