Вы находитесь на странице: 1из 15

Input/Output 2

Character Stream Classes


Can be used to read and write 16-bit Unicode characters.
Reader Stream Classes. Used to read characters from files. Writer Stream Classes. All output operations on files.

File class
Used only to know the details about the file. Cannot be used to read or write bytes into file. Helps to study the file before handling it.
File(String dirpath) File(String dirpath, String filename) File(File object, String filename)

Methods
String getName() String getPath() String getAbsolutePath() String getParent() boolean exists() boolean canWrite() boolean canRead() boolean isDirectory() boolean isFile() boolean isHidden()

long lastModified() long length() boolean renameTo(File newname) boolean delete() boolean setReadOnly() void deleteOnExit() String[] list()

FileInputStream
Read bytes from a disk file.
FileInputStream(String filename) FileInputStream(File object)

These constructors throws FileNotFoundException

FileOutputStream
FileOutputStream(String filename) FileOutputStream(File fobject) FileOutputStream(String filename, boolean append) The above constructors throw an IOException or SecurityException.

Memory Handling
ByteArrayInputStream ByteArrayOutputStream

ByteArrayInputStream
Used to read bytes from a memory. Objects of this class are used to create input stream with memory buffers as data source. Constructors
ByteArrayInputStream(byte b[]) ByteArrayInputStream(byte b[], int off, int len)

ByteArrayOutputStream
Write bytes into memory. Objects of this class are used to create output stream with memory buffers as data sink. Constructors :
ByteArrayOutputStream() ByteArrayOutputStream(int len)
will throw IllegalArgumentException is len is negative.

Filtered Byte Streams


Each stream in Java provides methods to access the data in different forms. The basic byte streams access the data in byte form. The raw bytes cannot be used for any useful purpose. For converting bytes to useful forms such as char, string, int etc java has several streams. The streams work on other streams and convert between byte to useful form or vice versa. Such streams that can take other stream as argument are called filtered streams.

Char, string

Filtered InputStream byte

Input Stream

Data Source

char, string

Filtered Outputstream

byte

OutputStream

Data Sink

BufferedInputStream
This class is used to increase the efficiency of reading bytes from an input stream. Fetching a byte and sending it to the destination one after another is a time consuming process. Instead bytes can be read and stored in a buffer memory before sending it to the destination. Once, the buffer is full, the bytes can be sent to the destination.

BufferedInputStream(InputStream ins) BufferedInputStream(InputStream ins, int size)

BufferedOutputStream
Subclass of FilterOutputStream. This class is used to send buffered bytes to an underlying output stream in an efficient way. Constructors :
BufferedOutputStream(OutputStream ous) BufferedOutputStream(OutputStream ous, int size)

Вам также может понравиться