- Input/Output in Java is used to process the input and produce the output based on the input. An input source may be a network connection or memory bugger or a disk file. Similarly, the output destination may be any physical device. A stream handles all these devices.
- Stream is a logical entity that either produces or consumes information. Java allows storing and managing data using files. This is known as file processing data into files. Depending on the requirement, files can be read or written either at bytes level or at characters level, or at fields level.
- File processing includes creating and updating files and manipulating In Java, a record can be represented as an object. Java also allows reading and writing class objects directly.
- Input/Output in the Java system links streams with physical devices. Java provides a wide range of I / O classes which are defined by java.io
Some of these classes are listed below –
- The process of reading and writing objects is called object serialization. The file input refers to the flow of data into a program from either keyboard, mouse, memory, disk, network, or even another program.
- Similarly, the file output refers to the flow of data from a file to either screen, printer, memory, disk, network, or a program.
- Java stream is nothing but an ordered sequence of data. It is a common characteristic. Java streams are of two types: input stream and output stream.
Table of Contents
The file class does not operate on stream. It directly deals with files and file systems. That is it describes the properties of the file.
The commonly used File methods are
|getname()||returns name of the file|
|getPath()||returns path of the file|
|getparent()||returns name of the parent directory|
|exists()||returns true if the file exists|
|canWrite()||returns whether the file is writable|
|canRead()||returns whether the file is readable|
|isDirectory()||returns whether it is a directory|
|isFile()||returns true if called on a file and returns false if called on a directory|
|renameTo()||It is used to rename the file to specified name.|
|delete()||to set the invoking file to read-only|
|isAbsolute()||returns true if the file has an absolute path and returns false if the file path is relative|
|lastModification()||returns date of last modification done in file|
|length()||returns length of file in bytes|
|setReadOnly()||to set the invoking file to read-on. In Java, the directory is a type of file that contains other files and directories.|
In Java, 3 streams are created for us automatically. All these streams are attached to the console.
1) System.out: the standard output stream
2) System.in the standard input stream
3) System.err: standard error stream
A stream is a sequence of data. In Java, a stream is composed of bytes. It’s called a stream because it’s like a stream of water that continues to flow.
Java application uses an output stream to write data to a destination, it may be a file, an array, peripheral device, or socket.
Java application uses an input stream to read data from a source, it may be a file, an array, peripheral device, or socket.
- In Java input stream (byte), InputStream is an abstract class and hence we can not create the object of this class.
- Some of the commonly used InputStream methods are –
|Reading bytes||read ( )|
|Repositioning to the beginning of the stream||reset ()|
|Skipping ahead of a string||skip ()|
|Finding the number of bytes in a stream available||available ( )|
- Setting the input pointer to the previously set mark: reset(); The base class for output stream (byte) is OutputStream. This is also an abstract class.
- Some of the commonly used OutputStream methods are
- Writing byte(s) to o/p stream : write ()
- Closing output stream : close ()
- Other classes that implement InputStream and OutputStream are tabulated below with their purpose. These are byte stream classes.
|Name Of Class||Purpose|
|FileInputStream||To read bytes from file|
|FileOutputStream||To write bytes to a file|
|ByteArrayInputStream||Uses a byte array as the source|
|ByteArrayOutputStream||Uses a byte array as the destination.|
|FilteredlnputStream||Is the wrapper around the input stream that transparently provides some extended level of functionality|
|FilteredOutputStream||Is the wrapper around the output stream that transparently provides some extended level of functionality.|
|BufferedInputStream||Extends a filtered input class by attaching a memory buffer. This buffer allows java to perform input operations on more than a byte at a time. This is used to increase performance.|