Input and Output in Java Monday, February 10, 2014 Nancy L. Harris.
-
Upload
leona-carter -
Category
Documents
-
view
219 -
download
0
Transcript of Input and Output in Java Monday, February 10, 2014 Nancy L. Harris.
+What can we read and write?
Bytes – used for binary data, sounds, pictures
Characters – used for textual data
We will focus on character data
+ What does a “stream” look like
It is not organized as we are used to looking at a “file”.
It is conceptually an infinitely long series of bytes.
Some readers deal with those bytes as text characters.
And each format item (new lines, tabs, spaces) have a corresponding character representation.
+ What’s a file?
A “file” can be thought of as a named bunch of data.
That data can be binary (like executable programs) or it can be textual (like the source files you make with JGrasp).
Text files are still binary, but their data can be directly interpreted as characters from the Unicode character set.
+ Processing a file
To read from a file Open the file Read its data Close the file
To write to a file Open the file Write its data Close the file
+Java classes (note: there are other classes that deal with file IO, but these will serve our purposes in 159)
Input File class Scanner class
Output File class PrintWriter class
1/20/2011CS239 – Spring 2011 8
File: “An abstract representation of file and directory pathnames.” (java api)
Instantiating a File does not “open” the file nor automatically check its existence.
Instantiating a Scanner or PrintWriter object does open the underlying file. FileNotFoundException if the file cannot be found or opened.
+ Making a copy of a file
Involves reading from a source and writing to a target.
Demo
Note:
File I/O requires the handling of “checked” exceptions.
These exceptions must be handled or re-thrown.
+ Exception Classes
Starting Out With JavaControl Structures to ObjectsBy Tony Gaddis
Copyright © 2005,
Pearson Addison-Wesley. All
rights
reserved
.
Chapter 12Slide #10
Object
Throwable
ExceptionError
RuntimeExceptionIOException
FileNotFoundExceptionEOFException
…
…
…
…
+A note about whitespace
Scanner automatically uses “whitespace” to parse input.
We can force it to use something else by the useDelimiter() method.
Scanner can process:
standard input (System.in)
files
lines of text
Its process of reading individual elements is also called parsing or tokenizing.
CS239 – Spring 2011 11
+What is EOF
EOF stands for End Of File and lets the input processor know that we have no more data.
Scanner’s hasNext method returns true if we have not yet reached EOF. False means we have reached the end of the file.
You might see EOF in submit when there is more output expected, but your file ends or vice versa.
1/20/2011
+Today’s Lab
You will build a “main” to read data from a file and use it to build a TextAnalyzer object. You will then call the TextAnalyzer methods to analyze the text you have read in.
The text may have multiple lines. You should read in all lines and build a String that you can send to the TextAnalyzer. To preserve the new lines in the original text, you will need to add them back in as you build the String.
See solution posted to Canvas if you do not have a TextAnalyzer class that you can use.
1/20/2011