15-01-2013, 02:34 PM
I/O
IO.ppt (Size: 284 KB / Downloads: 23)
What Is a Stream?
A stream is an ordered sequence of bytes of undetermined length.
Input streams move bytes of data into a Java program from some generally external source.
Output streams move bytes of data from Java to some generally external target
A program often needs to communicate with other devices. In other words it should receive input and send output.
There are many types of input sources:
Reading a file from a local disk / diskette
Receiving a web page from a remote server
Receiving a communication message through a network. Receiving a signal from a sensor of a robot
Scanner, video camera, ...
Similarly, there are many types of output destinations:
Writing to a file on a local disk / diskette
Sending query information to a remote web server
Sending communication message to a remote host. Sending a command to a robot controller.
Printing a document to a printer / fax
Displaying graphics on the screen Mouse, keyboard, joystick, ..
Files and File Systems
A file is a logical grouping of related bytes stored in secondary storage.
A file system is software used to organize and maintain files on a second storage device
The Java file I/O process consists of the following steps:
Import the necessary classes from the packages java.util and java.io into the program.
Create and associate the appropriate objects with the I/O sources.
Use the appropriate methods associated with the variables created in Step 2 to input/output the data.
Close the files.
Why Use Files for I/O
Keyboard input, screen output deal with temporary data
When program ends, data is gone
Data in a file remains after program ends
Can be used next time program runs
Can be used by another program
The File Class
is contained in the java.io package and
defines the methods you use to interact with the file system and to navigate the directory structure, much the same way you interactively move from one subdirectory to another.
You create an instance of a File class by using one of three constructors, as shown here:
File file1 = new File(String directory);
File file1 = new File(String directory, String fileName);
File file1 = new File(File directoryObject, String fileName);
It is important to remember that these constructors do not create a directory or subdirectory, nor do they create a file. Instead, think of these constructors as pointing to either a directory path or a file.
Introduction to collections
a Collection is a container of Objects, it groups many Objects into a single one
Arrays are too static (but can be also considered to be collections)
Arrays have very few built-in features
The Java Collections Framework consists of:
Interfaces – abstract data types representing various collections. Allow collections to be manipulated independently of their implementations.
Implementations – these are the concrete implementations of the interfaces. They are reusable data structures.
Algorithms – these are able to perform useful computations, like searching and sorting, on the implementations of the interfaces. So, the algorithms are polymorphic and therefore are reusable functionality.
Benefits of Collections
Reduces programming effort
Increases program speed and quality
Allows interoperability among unrelated APIs
Reduces effort to learn and use new APIs
Reduces effort to design new APIs
Helps to reuse the code