DOC PREVIEW
CORNELL CS 211 - Study Notes

This preview shows page 1 out of 4 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

0CS211 Recitation 5: Reading from a file; URLsand linksIntroductionWe assume that everyone has done some readingof a file in a earlier class, either in C++, Java, orMatlab.Material on reading from files, read Sec. 2.6.3 ofWeiss. For ProgramLive, read Sec. 5.7.2.The core Java language has no facilities forinput-output (I/O). Instead, all IO is handled byclasses in the Java API.There are two issues in handling reading (orwriting) a file. First, how do we let the user tell uswhat file to read or write? Second, how do we hookup to that file and read or write it?We handle the first one first:Class JFileChooserAn instance of class JfileChooser (in packagejava.io) is associated with a dialog box on yourmonitor, which the user can navigate in order tochoose a file. When the user has chosen the file, thedialog box closes and the program resumes. Here’sthe 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.The third one causes the dialog box to open; thestatement finished executing only when the usercloses the dialog box. When the third statementfinishes, object jd contains information about the filethe user selected. The fourth statement then retrievesan object that represents the file selected and stores itin variable j —null is stored in j if the user canceledthe dialog without selecting a file. So we have to testf 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 afile from the user.Input streamsConsider the following statement, assuming thatf, of class File, contains a description of a file:FileReader fr= new FileReader(f);This expression associates a “file reader” fr withthe file described by f. Using methods of object fr,we are able to read a character of the file at a time.This is too low-level for us, and we would rather beable to read a line at a time. Java provides a classBufferedReader 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 timefrom 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 nextline, it returns null.That’s all there is to reading a file. On the lastpage, we give a program that gets a file from the userand then prints the length of each line.Notice the throws clauses, which are neededbecause some IO exceptions are “checked”.Notice also the call br.close() when the fileprocessing has finished. Always close a file in thisfashion when finished with it.Take a careful look at function getReader.Whenever you want to obtain a file from a user andcreate a BufferedReader to read from it, copy thismethod into your file and use it! That is the easiestway to deal with input from files.Finally, look at the loop with initialization thatprocess the input file. The first line is read outside theloop to truthify the invariant. This is needed becausethe file may be empty, in which case there is no firstline.URLsURL stands for uniform resource locator. URLsare used on the internet to define files and theprotocols 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 associatedwith a computer that is attached to theinternet; and3. A path on that computer(Courses/cs211/2001fa/index.html).A URL can have other components; here is thegeneral form:<protocol>://<authority><path>?<query>#<fragment>We won't discuss or use the authority or the query.ProtocolsHere are the protocols one usually sees:11. http: This stands for HyperText TransportProtocol, which is the most used protocol foraccessing files over the internet. Generally, thefiles are html files, but they could be any files,including text files, .jpg files, and .gif files. Adomain name must be given.2. file: This is used for a file that is on the localcomputer. The domain name is generally notpresent.3. ftp: This stands for File Transfer Protocol, aprotocol that provides for files and directories offiles to be transferred from one computer toanother.4. mailto: Used to bring up a window that one canuse to send mail. In this case, the // and domainname are omitted, and instead of a path one hasan email address, e.g. consider this URL:mailto:[email protected] names, or hostsThe appearance of "//" in the URL signals thebeginning of a domain name, which is a name thathas been registered as being assigned to or associatedwith a particular computer. Here are examples ofdomain names:1. www.cs.cornell.edu2. www.cs.toronto.edu3. www.datadesk.comIt used to cost about $75 per year to register andmaintain a domain name. It’s a lot less now. Here is alink to the web page (http://www.icann.org/) for thecompany makes up rules for domain names andregisters them. The domain-name part of a URL isoften called the "host" —that name is used later, soremember it.PortsA URL can optionally specify a "port", which isthe port number to which the TCP connection ismade on the remote host machine. If the port is notspecified, the default port for the protocol is usedinstead. 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 notgiven 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 whosedomain name was given has a folder (directory)~gries; in that folder is a folder named Logic, and inthat folder is a file named Introduction.html.The character / is used to separate entities on thepath, regardless of the operating system that isrunning on the computer --Unix-like, Windows, orMacintosh.If the file name is missing at the end (so that thelast entity is a folder), then a default file is chosen,usually index.html or index.htm. This defaultdepends on (and can be changed) the computer onwhich the file resides. On some computers, we haveseen the following defaults: default.html, default.htm,home.html, and home.htm.Any


View Full Document

CORNELL CS 211 - Study Notes

Documents in this Course
B-Trees

B-Trees

10 pages

Hashing

Hashing

3 pages

Load more
Download Study Notes
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 Study Notes 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 Study Notes 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?