Download Document

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Input and Output in Java
Monday, February 10, 2014
Nancy L. Harris
+
Reference for this topic

Java Tutorials I/O
CS239 – Spring 2012
1/20/2012
+
Pictorial view of data streams (from
the Java tutorial)
+
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
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.
CS239 – Spring 2011 8
1/20/2011
+
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.
Addi
sonWes
ley.
All
right
s
rese
rved
.
+ Exception Classes
Object
Throwable
Error
…
Exception
…
IOException
RuntimeException
…
EOFException
FileNotFoundException
…
Chapter 12
Slide #10
Starting
Out With
+
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