CORNELL CS 211 - CS211 Recitation 5: Reading from a file; URLs and links

Unformatted text preview:

URLsProtocolsDomain names, or hostsPortsAbsolute pathsRelative URLsFragmentsClass URLCS211 Recitation 5: Reading from a file; URLsand linksIntroductionWe assume that everyone has done some reading of a file in a earlier class, either in C++, Java, or Matlab.Material on reading from files, read Sec. 2.6.3 of Weiss. For ProgramLive, read Sec. 5.7.2.The core Java language has no facilities for input-output (I/O). Instead, all IO is handled by classes in the Java API.There are two issues in handling reading (or writing) a file. First, how do we let the user tell us what file to read or write? Second, how do we hook up to that file and read or write it?We handle the first one first:Class JFileChooserAn instance of class JfileChooser (in package java.io) is associated with a dialog box on your monitor, which the user can navigate in order to choose a file. When the user has chosen the file, the dialog box closes and the program resumes. Here’s the sequence of Java statements to do this:JFileChooser jd = new JFileChooser();jd.setDialogTitle("Choose input file");jd.showOpenDialog(null);File f = jd.getSelectedFile();The first two statements are self-explanatory. Thethird one causes the dialog box to open; the statementfinished executing only when the user closes the dialog box. When the third statement finishes, object jd contains information about the file the user selected. The fourth statement then retrieves an objectthat represents the file selected and stores it in variable j —null is stored in j if the user canceled the dialog without selecting a file. So we have to test f before using it as a description of a file.Class File is also in package java.io.That is all there is to the best way of obtaining a file from the user.Input streamsConsider the following statement, assuming that f, of class File, contains a description of a file:FileReader fr= new FileReader(f);This expression associates a “file reader” fr with the file described by f. Using methods of object fr, weare able to read a character of the file at a time. This is too low-level for us, and we would rather be able toread a line at a time. Java provides a class BufferedReader for this purpose. Thus, execution of BufferedReader br= new BufferedReader(new FileReader(f));stores in br an object that can read a line at a time from file f. Evaluation of br.readLine()reads and returns the next line of file f, as a String, without the end-of-line character; if there is no next line, it returns null.That’s all there is to reading a file. On the last page, we give a program that gets a file from the user and then prints the length of each line.Notice the throws clauses, which are needed because some IO exceptions are “checked”.Notice also the call br.close() when the file processing has finished. Always close a file in this fashion when finished with it.Take a careful look at function getReader. Whenever you want to obtain a file from a user and create a BufferedReader to read from it, copy this method into your file and use it! That is the easiest way to deal with input from files.Finally, look at the loop with initialization that process the input file. The first line is read outside theloop to truthify the invariant. This is needed because the file may be empty, in which case there is no first line.URLsURL stands for uniform resource locator. URLs are used on the internet to define files and the protocols with which they should be processed. Here's an example of a URL:http://www.cs.cornell.edu/Courses/cs211/2001fa/index.htmlIt consists of1. An identification of a service or protocol (http);2. A domain name or host (www.cs.cornell.edu), which is associated 0with a computer that is attached to the internet; and3. A path on that computer (Courses/cs211/2001fa/index.html).A URL can have other components; here is the general form:<protocol>://<authority><path>?<query>#<fragment>We won't discuss or use the authority or the query.ProtocolsHere are the protocols one usually sees:1. http: This stands for HyperText Transport Protocol, which is the most used protocol for accessing files over the internet. Generally, the files are html files, but they could be any files, including text files, .jpg files, and .gif files. A domain name must be given.2. file: This is used for a file that is on the local computer. The domain name is generally not present.3. ftp: This stands for File Transfer Protocol, a protocol that provides for files and directories of files to be transferred from one computer to another.4. mailto: Used to bring up a window that one can use to send mail. In this case, the // and domain name are omitted, and instead of a path one has an email address, e.g. consider this URL: mailto:[email protected] names, or hostsThe appearance of "//" in the URL signals the beginning of a domain name, which is a name that has been registered as being assigned to or associated with a particular computer. Here are examples of domain names:1. www.cs.cornell.edu2. www.cs.toronto.edu3. www.datadesk.comIt used to cost about $75 per year to register and maintain a domain name. It’s a lot less now. Here is a link to the web page (http://www.icann.org/) for the company makes up rules for domain names and registers them. The domain-name part of a URL is often called the "host" —that name is used later, so remember it.PortsA URL can optionally specify a "port", which is the port number to which the TCP connection is made on the remote host machine. If the port is not specified, the default port for the protocol is used instead. For example, the default port for http is 80. An alternative port could be specified as:http://www.ncsa.uiuc.edu:8080/demoweb/url-primer.htmlWe won't deal with ports, since they are usually not given in the URLs that we deal with.Absolute pathsThe path part of a URL is a path on the computerto the file that the URL describes. For example,/~gries/Logic/Introduction.htmlindicates that the hard drive of the computer whose domain name was given has a folder (directory) ~gries; in that folder is a folder named Logic, and in that folder is a file named Introduction.html.The character / is used to separate entities on the path, regardless of the operating system that is running on the computer --Unix-like, Windows, or Macintosh.If the file name is missing at the end (so that the last entity is a folder), then a default file is chosen, usually index.html or index.htm. This default dependson (and


View Full Document

CORNELL CS 211 - CS211 Recitation 5: Reading from a file; URLs and links

Documents in this Course
B-Trees

B-Trees

10 pages

Hashing

Hashing

3 pages

Load more
Download CS211 Recitation 5: Reading from a file; URLs and links
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view CS211 Recitation 5: Reading from a file; URLs and links and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view CS211 Recitation 5: Reading from a file; URLs and links 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?