Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Outline
■
■
■
■
■
■
■
5. Java I/O 1
Model for output
Classes for output
Output to disks
Model for input
Keyboard input and interactive I/O
Input from disks
Applets versus Applications
Computer Science Dept Va Tech August 2002
Programming in Java
©2002 D Barnette, B Keller & P Schoenhoff
Kinds of Input and Output
■
Input/Output (General)
–
■
read from keyboard, write to console
Disk I/O (“File I/O”)
–
■
reading and writing data
Standard I/O
–
■
5. Java I/O 2
read from and write to file on disk
Reading from website
–
network communication
Computer Science Dept Va Tech August 2002
Programming in Java
©2002 D Barnette, B Keller & P Schoenhoff
Output…. Write this down!!!!
SCREEN
5. Java I/O 3
FILES
PrintStream
PrintStream
System.out
FileOutputStream
File
String
Computer Science Dept Va Tech August 2002
Programming in Java
©2002 D Barnette, B Keller & P Schoenhoff
Model of Output
5. Java I/O 4
w
Program
o
Data flows out as
stream of characters
c
n
w
o
r
B
Need more objects if
want formatted printing
A
Data
Target
Computer Science Dept Va Tech August 2002
Programming in Java
©2002 D Barnette, B Keller & P Schoenhoff
Classes for Output
■
5. Java I/O 5
Generally have three classes involved
–
–
–
Class for data target
Class for unstructured data stream
Class for structured/formatted data stream
Computer Science Dept Va Tech August 2002
Programming in Java
©2002 D Barnette, B Keller & P Schoenhoff
Disk Files
■
■
■
5. Java I/O 6
Disk files allow storing data persistently on disk – data exists on disk until
deleted
Attributes of file: (1) name, and (2) contents
File operations
–
–
–
–
Writing to file
Deleting file
Renaming file
Overwriting file
Computer Science Dept Va Tech August 2002
Programming in Java
©2002 D Barnette, B Keller & P Schoenhoff
The File Class
■
■
5. Java I/O 7
A File object models a disk file
Constructor takes file name as argument
File f1 = new File(”letter”);
■
■
Constructor does not create the disk file, just object that represents it
However, if file exists can rename or delete it
f1
File object
reference returned by new()
Computer Science Dept Va Tech August 2002
Programming in Java
?
©2002 D Barnette, B Keller & P Schoenhoff
FileOutputStream Class
■
■
5. Java I/O 8
Unstructured stream of data to disk file
Constructor opens file that exists, and requests Operating System to create the
file if it does not exist
File f = new File(”baby.babble”);
FileOutputStream fs = new FileOutputStream(f);
■
Not convenient to use for producing text output – will use a PrintStream
Computer Science Dept Va Tech August 2002
Programming in Java
©2002 D Barnette, B Keller & P Schoenhoff
PrintStream Class
■
5. Java I/O 9
Can construct PrintStream class that writes to file
File f = new File(”baby.babble”);
FileOutputStream fs = new FileOutputStream(f);
PrintStream babyfile = new PrintStream(fs);
babyfile.println(”dada dada dada”);
Computer Science Dept Va Tech August 2002
Programming in Java
©2002 D Barnette, B Keller & P Schoenhoff
Composition
■
5. Java I/O 10
Can compose object creation steps to simplify program
PrintStream babyfile;
babyfile = new PrintStream(
new FileOutputStream(
new File(”baby.babble”)));
■
Just eliminates use of reference variables – not the objects themselves
Computer Science Dept Va Tech August 2002
Programming in Java
©2002 D Barnette, B Keller & P Schoenhoff
Example Program
5. Java I/O 11
import java.io.*;
class PrintBackup {
public static void main (String[] arg) throws Exception {
PrintStream backup;
backup = new PrintStream(
new FileOutputStream(
new File(”backup”)));
System.out.println(”Very important information”);
backup.println(”Very important information”);
}
}
Computer Science Dept Va Tech August 2002
Programming in Java
©2002 D Barnette, B Keller & P Schoenhoff
Things Can Go Wrong
■
■
■
■
5. Java I/O 12
Interactions with files could result in error – if disk is full, then can’t write
data
Such an error could cause the program to “crash” – terminate abruptly
The phrase throws Exception tells compiler that an error could occur
Compiler will issue error if not included
Computer Science Dept Va Tech August 2002
Programming in Java
©2002 D Barnette, B Keller & P Schoenhoff
Input…. Write this down!!!!
5. Java I/O 13
KEYBOARD
BufferedReader
FILES
BufferedReader
WEB
BufferedReader
InputStreamReader
InputStreamReader
InputStreamReader
FileInputStream
FilterInputStream
InputStream
System.in
File
* URL
url.openStream( )
String
String
Input is done with BufferedReader objects, which must be constructed from
other input objects.
Computer Science Dept Va Tech August 2002
Programming in Java
©2002 D Barnette, B Keller & P Schoenhoff
Model of Input
Data
Source
5. Java I/O 14
w
o
c
Think of data flowing
as stream of characters
n
w
o
r
B
Need additional
objects to break flow
into recognizable
pieces
A
Program
Computer Science Dept Va Tech August 2002
Programming in Java
©2002 D Barnette, B Keller & P Schoenhoff
Classes for Keyboard Input
■
5. Java I/O 15
BufferedInputStream – represents source
Predefined object System.in represents keyboard
■
InputStreamReader – unstructured stream of characters
■
BufferedReader – structured stream of characters
■
buffer – storage to temporarily
hold data
Computer Science Dept Va Tech August 2002
Programming in Java
©2002 D Barnette, B Keller & P Schoenhoff
Reading from Keyboard
5. Java I/O 16
BufferedReader kb;
kb = new BufferedReader(
new InputStreamReader(System.in));
String line = kb.readLine();
Computer Science Dept Va Tech August 2002
Programming in Java
©2002 D Barnette, B Keller & P Schoenhoff
Example Program: Pluralizer
5. Java I/O 17
import java.io.*;
class Pluralizer {
public static void main (String[] arg) throws Exception {
BufferedReader kb; //models keyboard
kb = new BufferedReader(
new InputStreamReader(System.In));
String inputLine; //holds input string
inputLine = kb.readLine();
System.out.print(inputLine);
System.out.println(”s”);
}
}
Computer Science Dept Va Tech August 2002
Programming in Java
©2002 D Barnette, B Keller & P Schoenhoff
Output - Program: Pluralizer
Computer Science Dept Va Tech August 2002
Programming in Java
5. Java I/O 18
©2002 D Barnette, B Keller & P Schoenhoff
Interactive Programs
–
–
5. Java I/O 19
Programs that expect input from a user,
should prompt for it (HCI principle)
Example the Pluralizer program, (from textbook), should prompt for a word to
be made plural:
System.out.println(”Please give word to pluralize:”);
inputLine = kb.readLine();
Computer Science Dept Va Tech August 2002
Programming in Java
©2002 D Barnette, B Keller & P Schoenhoff
Problem: Buffered Output
■
■
■
■
5. Java I/O 20
Output is buffered! (remember?) – many characters are output to console at once
Buffering is more efficient than sending one or only a few characters at a time
… but buffering only guarantees that output
will eventually be sent.
Means that your prompt may not be
printed before the input line is read!!
Computer Science Dept Va Tech August 2002
Programming in Java
©2002 D Barnette, B Keller & P Schoenhoff
Flushing the Output Buffer
■
■
5. Java I/O 21
Can force all output currently in buffer to be printed with PrintStream method
flush
Example should have a flush message
System.out.println(”Word to pluralize? ”);
System.out.flush(); //forces display of prompt
inputLine = kb.readLine();
■
Now prompt appears before input is read
Look at Ch 4.3 – 4.5 the “InteractiveIO class”
Computer Science Dept Va Tech August 2002
Programming in Java
©2002 D Barnette, B Keller & P Schoenhoff
Reading from a File
5. Java I/O 22
File f = new File(”budget.data”);
FileInputStream fs = new FileInputStream(f);
InputStreamReader isr;
BufferedReader fileInput;
isr = new InputStreamReader(fs);
fileInput = new BufferedReader(isr);
//
String line;
line = fileInput.readLine();
Almost same as
keyboard input!
Computer Science Dept Va Tech August 2002
Programming in Java
©2002 D Barnette, B Keller & P Schoenhoff
Reading from a File
5. Java I/O 23
BufferedReader fileInput;
fileInput = new BufferedReader(
new InputStreamReader(
new FileInputStream(
new File(”budget.data”)));
Computer Science Dept Va Tech August 2002
Programming in Java
©2002 D Barnette, B Keller & P Schoenhoff
Example: Copying a File
5. Java I/O 24
import java.io.*;
class FileCopy {
public static void main(String[] arg) throws Exception {
BufferedReader kb,
origFile;
PrintStream
cpyFile;
String
fileName,
copyFileName;
kb = new BufferedReader(
new InputStreamReader(System.in));
Computer Science Dept Va Tech August 2002
Programming in Java
©2002 D Barnette, B Keller & P Schoenhoff
Example (cont)
5. Java I/O 25
System.out.print(”name of file to copy: “);
System.out.flush();
fileName = kb.readLine().trim(); //what does trim do?
copyFileName = fileName.concat(”.copy”);
origFile = new BufferedReader(
new InputStreamReader(
new FileInputStream(
new File(fileName))));
cpyFile = new PrintStream(
new FileOutputStream(
new File(copyFileName)));
Computer Science Dept Va Tech August 2002
Programming in Java
©2002 D Barnette, B Keller & P Schoenhoff
Example (cont)
5. Java I/O 26
//copy two lines
line = origFile.readLine();
cpyFile.println(line);
line = origFile.readLine();
cpyFile.println(line);
} //end of main
} //end of class
Computer Science Dept Va Tech August 2002
Programming in Java
©2002 D Barnette, B Keller & P Schoenhoff
Graphical User Interfaces
5. Java I/O 27
■
Not all interactive I/O strictly done with keyboard and console
■
Most programs have a Graphical User Interface (GUI)
■
Includes windows to type in, buttons to push, fields to type in, menus to select from
Computer Science Dept Va Tech August 2002
Programming in Java
©2002 D Barnette, B Keller & P Schoenhoff
Application versus Applet
5. Java I/O 28
■
Application – program that runs on computer
■
Applet – program that runs as part of web page
■
Applications can use text-based console interface or GUI
■
Applets always use a GUI
Computer Science Dept Va Tech August 2002
Programming in Java
©2002 D Barnette, B Keller & P Schoenhoff
Net Input
■
5. Java I/O 29
Internet address components
URL webVTaddr;
webVTaddr =
new URL(http://www.vt.edu/”);
InputStream vtWWW = webVTaddr.openStream();
Computer Science Dept Va Tech August 2002
Programming in Java
©2002 D Barnette, B Keller & P Schoenhoff
Net Input Example
5. Java I/O 30
import java.net.*;
import java.io.*;
class WHWWW {
public static void main(String[] arg) throws Exception {
URL u = new URL("http://www.whitehouse.gov/");
URLConnection uC = u.openConnection();
BufferedInputStream
ins =
(BufferedInputStream)uC.getContent();
InputStreamReader isr = new InputStreamReader(ins);
BufferedReader whiteHouse = new BufferedReader(isr);
}
}
System.out.println(whiteHouse.readLine());
System.out.println(whiteHouse.readLine());
System.out.println(whiteHouse.readLine());
System.out.println(whiteHouse.readLine());
System.out.println(whiteHouse.readLine());
Computer Science Dept Va Tech August 2002
Programming in Java
©2002 D Barnette, B Keller & P Schoenhoff