Without the Ability to Interact With the Rest of the World
Click here to load reader
-
Upload
jake-tyler -
Category
Documents
-
view
214 -
download
0
Transcript of Without the Ability to Interact With the Rest of the World
8/8/2019 Without the Ability to Interact With the Rest of the World
http://slidepdf.com/reader/full/without-the-ability-to-interact-with-the-rest-of-the-world 1/6
WITHOUT THE ABILITY TO INTERACT WITH the rest of the world, a program
would be useless. The interaction of a program with the rest of the world is
referred to as input/output or I/O. Historically, one of the hardest parts of
programming language design has been coming up with good facilities for doing
input and output. A computer can be connected to many different types of inputand output devices. If a programming language had to deal with each type of
device as a special case, the complexity would be overwhelming. One of the major
achievements in the history of programming has been to come up with good
abstractions for representing I/O devices. In Java, the I/O abstractions are
called streams. This section is an introduction to streams, but it is not meant to
cover them in full detail. See the official Java documentation for more
information.
All fundamental I/O in Java is based on streams. A stream represents a flow of data, or a channel of communication. Conceptually, there is a reading process at
one end of the stream and a writing process at the other end. Java 1.0 supported
only byte streams, which meant that Unicode characters were not always handled
correctly. As of Java 1.1, there are classes in java.io for both byte streams and
character streams. The character stream classes, which are
called readers andwriters, handle Unicode characters appropriately.
6.1 Input Streams and Readers
The InputStream class is an abstract class that defines methods to read
sequentially from a stream of bytes. Java provides subclasses of
the InputStreamclass for reading from files, StringBuffer objects, and byte arrays,
among other things. Other subclasses of InputStream can be chained together to
provide additional logic, such as keeping track of the current line number or
combining multiple input sources into one logical input stream. It is also easy to
define a subclass of InputStream that reads from any other kind of data source.
6.2 Output Streams and Writers
The OutputStream class is an abstract class that defines methods to write a stream
of bytes sequentially. Java provides subclasses of the OutputStream class for
writing to files and byte arrays, among other things. Other subclasses
of OutputStream can be chained together to provide additional logic, such as
writing multibyte data types or converting data to a string representation. It is also
easy to define a subclass of OutputStream that writes to another kind of destination.
8/8/2019 Without the Ability to Interact With the Rest of the World
http://slidepdf.com/reader/full/without-the-ability-to-interact-with-the-rest-of-the-world 2/6
BYTE STREAMS
The streaming byte Input of Java is defined by this Abstract Class .all the methods in this class will always throw
an IOException whenever any kind of error will occur.
Few methods of this class are as under:
1. available( ): This method will return the number of bytes of input which is currently available for reading.
2. close( ) : this method is used to close the input source. Once the input is closed if we will try to read it again
we will get the IOException.
3. mark(intnumBytes): This method places a mark at the current point in the input stream which will remain valid
until all the numBytes bytes are read.
4. markSupported( ) : This method since as Boolean will return ³true ³ if mark( )/reset( ) are supported by the
invoking stream.
5. read( ) : This method returns an integer representation of the next available byte of input. ±1 is returned when
the end of the file is encountered.
6. reset( ) : This method will reset the input pointer to the previously set mark.
Java OutputStream Class
This abstract class defines the streaming byte output. All of the methods in this class return a void value and
throw an IOException in the case of errors..
Few methods of this class are as under:
1. close( ) : This method will close Closes the output stream. After the output is closed it will generate an
IOException if we will try to perform any operation on it.
2. flush( ) : This method finalizes the output state so that any buffers are cleared. It means that it flushes the
output buffers.
3. write(int b) : This simple method writes a single byte to an output stream. The parameter isn this method
should always be an integer value.
4. write(byte buffer[ ]) : This method will write a complete array of bytes to an output stream.
FileInputStream Class
This class will create an InputStream which can be used to read bytes from a file.the two very basic and common
constructors of FileInputStream class are :
1. FileInputStream(String filepath)
2. FileInputStream(File fileObj)
FileOutputStream Class
This class creates an OutputStream which is used to write bytes to a file. This is not dependent on the existingfile but it itself creates a file before opening it for the output whenever we create the object.
8/8/2019 Without the Ability to Interact With the Rest of the World
http://slidepdf.com/reader/full/without-the-ability-to-interact-with-the-rest-of-the-world 3/6
The basic constructors associated to this class are :
1.FileOutputStream(String filePath):
2. FileOutputStream(File fileObj)
3. FileOutputStream(String filePath, boolean append)
4. FileOutputStream(File fileObj, boolean append)
ByteArrayOutputStream
This class is an implementation of an output stream that uses a byte array as the destination. It has twoconstructors that are shown below:
1.ByteArrayOutputStream( ) A buffer of 32 bytes is created in this case.
2. ByteArrayOutputStream(intnumBytes) In this case a buffer will be created which will be equal to the size of numBytes and it can be automatically increased if required.
Character streams
Version 1.1 of the Java Developer's Kit introduces support for character
streams to the java.io package.
Prior to this release, the standard I/O facilities supported only byte streams, via
the InputStream and OutputStream classes and their subclasses. Character streams
are like byte streams, but they contain 16 -bit Unicode characters rather than eight -
bit bytes. They are implemented by the Reader and Writer classes and their
subclasses. Readers and Writers support essentially the same operations as
InputStreams and OutputStreams, except that where byte-stream methods operate
on bytes or byte arrays, character -stream methods operate on characters, character
arrays, or strings.
Why use character streams?
The primary advantage of character streams is that they make it easy to write
programs that are not dependent upon a specific character encoding, and are
therefore easy tointernationalize.
Java represents strings in Unicode, an international standard character encoding
that is capable of representing most of the world's written languages. Typical
human-readable text files, however, use encodings that are not necessarily related
to Unicode, or even to ASCII, and there are many such encodings. Character
streams hide the complexity of dealing with these encodings by providing two
classes that serve as bridges between byte streams and character streams.
The InputStreamReader class implements a character-input stream that reads bytes
8/8/2019 Without the Ability to Interact With the Rest of the World
http://slidepdf.com/reader/full/without-the-ability-to-interact-with-the-rest-of-the-world 4/6
from a byte-input stream and converts them to characters according to a specified
encoding. Similarly, the OutputStreamWriter class implements a character-output
stream that converts characters into bytes according a specified encoding and
writes them to a byte-output stream.
A second advantage of character streams is that they are potentially much moreefficient than byte streams. The implementations of many of Java's original byte
streams are oriented around byte-at-a-time read and write operations. The
character-stream classes, in contrast, are oriented around buffer-at-a-time read
and write operations. This difference, in combination with a more efficient locking
scheme, allows the character stream classes to more than make up for the added
overhead of encoding conversion in many cases.
Character Streams versus Byte StreamsPrior to JDK 1.1, the input and output classes (mostly found in the java.io package) only supported 8-
bit byte streams. The concept of 16-bit Unicode character streams was introduced in JDK 1.1. While byte
streams were supported via the java.io.InputStream and java.io.OutputStream classes and their subclasses,
character streams are implemented by the java.io.Reader and java.io.Writer classes and their subclasses.
Most of the functionality available for byte streams is also provided for character streams. The methods for character streams generally accept parameters of data typechar parameters, while byte streams, you guessed it,work with byte data types. The names of the methods in both sets of classes are almost identical except for the
suffix, that is, character-stream classes end with the suffix Reader or Writer and byte-stream classes end with
the suffix InputStream and OutputStream . For example, to read files using character streams, you would use
the java.io.FileReader class; for reading it using byte streams you would use java.io.FileInputStream .
Unless you are working with binary data, such as image and sound files, you should use readers and writers
(character streams) to read and write information for the following reasons:
y They can handle any character in the Unicode character set (while the byte streams are limited to ISO-Latin-1 8-bit bytes).
y They are easier to internationalize because they are not dependent upon a specific character encoding.
y They use buffering techniques internally and are therefore potentially much more efficient than byte streams.
Bridging the Gap Between Byte and Character Streams
To bridge the gap between the byte and character stream classes, JDK 1.1 and JDK 1.2 provide
the java.io.InputStreamReader and java.io.OutputStreamWriter classes. The only purpose of these classesis to convert byte data into character-based data according to a specified (or the platform default) encoding. For
example, the static data member "in" in the "System" class is essentially a handle to the Standard Input (stdin)
device. If you want to w rap this inside the java.io.BufferedReader class that works with character-streams, you
use InputStreamReader class as follows:
BufferedReader in = new BufferedReader(newInputStreamReader(System.in));
Files
8/8/2019 Without the Ability to Interact With the Rest of the World
http://slidepdf.com/reader/full/without-the-ability-to-interact-with-the-rest-of-the-world 5/6
A computer file is a block of arbitrary information, or resource for storing information, which
is available to a computer program and is usually based on some kind of durable storage.
Java File Handling
Most of the classes defined by java.io operate on streams, the File class does not. It deals directly with files and
the file system. That is, the File class does not specify how information is retrieved from or stored in files; it
describes the properties of a file itself. A File object is used to obtain or manipulate the information associated
with a disk file, such as the permissions, time, date, and directory path, and to navigate subdirectory hierarchies.
Files are a primary source and destination for data within many programs. Although there are severe restrictions
on their use within applets for security reasons, files are still a central resource for storing persistent and shared
information.
In all these four cases ,directoryPath is the path name of the file, filename is the name of the file, dirObj is a File
object that specifies a directory, and uriObj is a URI object that describes a file.
File defines various methods through which we can easily obtain the required information related to any file . For
example:
1. getName ( ) method is used to get the name of a particular file
2. getParent ( ) method gives the name of the parent directory exists ( ) method return the value ³ true ³ if a file
exists else gives ³ false´ as result if it doesn¶t find the file of that name which it is trying to locate a particular file.
3. isFile ( ) return true if it is called on a file and false if called on a directory isAbsolute ( ) method return ³ true´ if
the file is of absolute type end returns ³ false´ if the file is of relative type.
4. renameTo( ) methiod is used to change the name of an existing file booleanrenameTo(File newName) It will
return the value as ³true´ if it successfully changes the name of the file else returns ³false´ if it is not bale to locate
the file which has to be changed.
5. delete( ) This method is used to delete a particular file and it return value as ³ true´ if it successfully deletes the
false else returns ³false´ if it fails in its attempt. Boolean delete ( )
6. deleteOnExit( ) This method removes the file associated with the invoking object when the Java Virtual
Machine terminates.
7. isHidden( ) Returns true if the particular file is hidden. And gives value as false in the other case.
Directories
Directory can be defined as a repository of a number of files and directories at a place. Whenever we create a file
which is also a directory then the method isDirectory ( ) will return value as ³true´ else will give ³false´ as the
result.
Two methods which are directly related to the directory are mkdir ( ) and mkdirs( ). The mkdir( ) method creates a
directory and gives value as´ True´ if it is able tpsuccessfullt create the directory and returns´ false´ if fails in the
operation.
The mkdirs( ) method is used to create a directory for which no path exists, It creates both a directory and all the
parents of the directory.
8/8/2019 Without the Ability to Interact With the Rest of the World
http://slidepdf.com/reader/full/without-the-ability-to-interact-with-the-rest-of-the-world 6/6