0% found this document useful (0 votes)
31 views

File Handling

Streams provide a standard way to read and write data in Java. The document discusses input and output streams, stream classes in the java.io package, basic stream operations like read() and write(), stream readers and writers for character data, standard input/output streams, tokenizing streams, getting data from the keyboard, accessing files and directories, and creating and writing files. Streams abstract the underlying input/output devices and provide a consistent interface for processing data in a device-independent way.

Uploaded by

santhilatakv
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

File Handling

Streams provide a standard way to read and write data in Java. The document discusses input and output streams, stream classes in the java.io package, basic stream operations like read() and write(), stream readers and writers for character data, standard input/output streams, tokenizing streams, getting data from the keyboard, accessing files and directories, and creating and writing files. Streams abstract the underlying input/output devices and provide a consistent interface for processing data in a device-independent way.

Uploaded by

santhilatakv
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 21

Understanding Streams

Streams and new I/O capability


• To be able to read data from various kinds from key
board

• To be able to create formatted output to the


command line

• To be able read and write files containing basic data

• To be able to read and write files containing objects


Streams
• Included in java.io

• A Stream is an abstract representation of


input and output device, that is a source of
or destination of data.

– Output Stream.

– Input Stream.
Program

Input Stream Data


Data

Output Stream

Key board
Monitor
Why Streams?
• Program becomes independent of the devices that
are used.

• A stream is often equipped with a Buffer of


memory.

• Stream with buffer is called as Buffered Stream.

• Buffers reduce the number of input / output


operations necessary.

• Flushing buffers.
Streams
• Binary Streams: stores bytes of data
– 4 bytes for int type etc.

• Character streams: for storing and retrieving text files or


any other files that not written by Java programs.
– All programs have to get converted to characters to be
represented by this stream.

• Java.io package provides input and output stream classes.


These classes are abstract.

• Input Stream: the base class for byte input operations

• Output Stream: the base class for byte output operations


Basic Input Stream operations
• read():
– abstract method of InputStream class.
– Return type is ‘int’ (if EOF, -1)
– Returns next byte available
– Throws IOException
• read(byte[] array):
– Reads bytes into successive elements of ‘array’ up to array.length
– Returns number of bytes read (-1, for no bytes)
– Throws IOException, NullPointerException
• read(byte[] array, int offset, int length):
– Works exactly like the previous method
– Starts reading into the array from the offset
• Skip(long s):
– Skips ‘s’ number of bytes from the InputStream
Subclasses of Inputstream
InputStream

AudioInputStream SequenceInputStream

FileInputStream ByteArrayInputStream

PipedInputStream
ObjectInputStream

FilterInputStream
Basic output Stream operations
• Write() mirrors the InputStream class
OutputStream

FileOutputStream ByteArrayOutputStream

PipedOutputStream
ObjectOutputStream

FilterOutputStream
Stream Readers & Writers
• Readers and Writers are the objects that can
read or write byte streams as character
streams

• Reader: base class for reading character


stream

• Writer: base class for writing character


stream
Reader sub classes Writer sub classes

InputStreamReader OutputStreamWriter

PipedReader PipedWriter

BufferedReader BufferedWriter

FilterReader FilterWriter

CharArrayReader CharArrayWriter

StringReader StringWriter

PrintWriter
Standard streams
• A standard input stream:
– Key board usually
– Encapsulated by ‘in’ member of the ‘System’ class
(type InputStream)
• A standard output stream:
– Output on the command line
– ‘out’ member of the ‘System’ class (type PrintStream)
• A standard error output stream:
– Error messages on to the command line
– ‘err’ member of the ‘System’ class (type PrintStream)
Tokenizing a stream
• A StreamTokenizer class defines objects
that can read an input stream and parse it
into tokens
• Kinds of tokens a streamTokenizer can
recognize:
– Numbers
– Strings
– Words
– Comments
– White spaces
Getting data from the keyboard
StreamTokenizer st = new
StreamTokenizer( new
BufferedReader(new
InputStreamReader(System.in)));
Accessing files and Directories
File handling
• Create and use the file objects to examine
files and directories
• Create new files and directories
• Create temporary files
• Writing on to the files
Working with file objects
• File object encapsulates a pathname or reference to a physical file or
directory

• File object enables us to check the pathname

• They are used to create file stream objects

• File myDir = new File (“C:\\Project\\java\\io”)

• Caution: File object does not check whether the given path is valid or
not.

• Note: for a File, specify the file name towards the end of the pathname
Creating files
• File objects are immutable

File myDir = new File (“C:\\Project\\java\\io”);


File myFile = new File (myDir, “myFile.java”);

File remoteFile = new File( new


URL( http://abc.xyz.com/java));

• Static member: File.separator


Methods on ‘File’
• getName() • exists()
• getPath() • isDirectory()
• getParent() • isFile()
• toString() • canRead()
• equals() • canWrite()
• …. • ……
Create files
File tempFile = File.createTempFile(“list”,null)
tempFile.deleteOnExit();
• Creates a file with name ‘listxxxxx.tmp’.
• Other methods on files
– renameTo()
– mkdir()
– createNewFile()
– delete() // wont delete directories if not empty
Writing Files
• File Channels: File Channel objects define
a channel for a physical file.
• Buffers: a particular buffer store a sequence
of elements of a given type (char, int,
long…)
• //WriteAString.java

You might also like