Java File Class
Java File Class
• 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