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

Java File Class

The document discusses Java I/O and file handling. It defines streams as sequences of objects from a source that support aggregate operations. It describes input and output streams for reading from and writing to files and other sources. It provides examples of byte streams like FileInputStream and FileOutputStream for handling bytes, and character streams like FileReader and FileWriter for handling text. It also discusses common stream operations like sorted, map, filter, collect, and forEach.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Java File Class

The document discusses Java I/O and file handling. It defines streams as sequences of objects from a source that support aggregate operations. It describes input and output streams for reading from and writing to files and other sources. It provides examples of byte streams like FileInputStream and FileOutputStream for handling bytes, and character streams like FileReader and FileWriter for handling text. It also discusses common stream operations like sorted, map, filter, collect, and forEach.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Java File Class

Files and I/O


• To process the input and generate an output, Java I/O (Input and
Output) is used.
• Java utilizes the stream idea to speed up the I/O operation. The
java.io package includes all the classes which are necessary for
input and output operations.
• We can use Java I/O API to handle files.
• The java.io package contains almost every class in Java that you
may ever need to perform input and output (I / O). All these
streams are a source of input and a destination of output. The
stream in the java.io package supports a lot of information like
primitives, object, localized characters, etc.
What is Stream?
• Stream represents a sequence of objects from a source, which
supports aggregate operations. Following are the
characteristics of a Stream.
• Sequence of elements
• Source
• Aggregate operations
• Pipelining
• Automatic iterations
Stream
• A stream can be described as a data sequence. There are two types of streams
available:
• InputStream: The InputStream is used from a source to read data.
• OutputStream: To write data to a destination, the OutputStream is used in Java.

• Java offers powerful but flexible file and network I/O support, but this tutorial
includes very fundamental stream and I/O functionality. We'll see one by one the
most frequently used instances.
Byte Streams
• Java byte streams are used to execute 8-bit bytes input and output.
• Although there are many classes linked to byte streams, FileInputStream
and FileOutputStream are the most commonly used classes.
• Following is an instance to copy an input file into an output file using these
two classes. InputStream OutputStream
FIleInputStream FileOutputStream
ByteArrayInputStream ByteArrayOutputStream
ObjectInputStream ObjectOutputStream
PipedInputStream PipedOutputStream
FilteredInputStream FilteredOutputStream
BufferedInputStream BufferedOutputStream
DataInputStream DataOutputStream
Character Streams
• Character Streams − These handle data in 16 bit Unicode. Using these you
can read and write text data only.
• The Reader and Writer classes (abstract) are the super classes of all the
character stream classes: classes that are used to read/write character
streams. Following are the character array stream classes provided by Java
Reader Writer
BufferedReader BufferedWriter
CharacterArrayReader CharacterArrayWriter
StringReader StringWriter
FileReader FileWriter
InputStreamReader InputStreamWriter
FileReader FileWriter
Streams and its operations
• Streams are sequences of objects from a source, which support aggregate
operations. These were introduced in Java 8.
• With Java 8, Collection interface has two methods to generate a Stream.
• stream() − Returns a sequential stream considering collection as its source.
• parallelStream() − Returns a parallel Stream considering collection as its
source.
• Some operations on streams are −
Operations on Stream
sorted − The sorted method is use to sort the stream lexicographically or in
ascending order
map − The map method maps the elements in the collection to other objects
according to the Predicate passed as a parameter
filter − The filter method is used to select elements according to the
Predicate parameter passed
collect − The collect method returns the outcome of the intermediate
operations
forEach − This method iterates through every element in the stream
How to read file in Java – BufferedInputStream
How to read file in Java using BufferedReader

You might also like