- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference between the byte stream and character stream classes in Java?
Java provides I/O Streams to read and write data where, a Stream represents an input source or an output destination which could be a file, i/o devise, other program etc.
Based on the data they handle there are two types of streams −
- Byte Streams − These handle data in bytes (8 bits) i.e., the byte stream classes read/write data of 8 bits. Using these you can store characters, videos, audios, images etc.
- 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.
Whereas the InputStream and OutputStream classes (abstract) are the super classes of all the input/output stream classes: classes that are used to read/write a stream of bytes.
Following diagram illustrates all the input and output Streams (classes) in Java.
Difference between input/output Streams and Readers/Writers
The major difference between these is that the input/output stream classes read/write byte stream data. Whereas the Reader/Writer classes handle characters.
The methods of input/output stream classes accept byte array as parameter whereas the Reader/Writer classes accept character array as parameter.
The Reader/Writer classes handles all the Unicode characters, comes handy for internalization, comparatively efficient that input/output streams.
Therefore, until you deal with binary data like images it is recommended to use Reader/Writer classes.
Example Input/Output Streams
Following Java program reads data from a particular file using FileInputStream and writes it to another, using FileOutputStream.
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class IOStreamsExample { public static void main(String args[]) throws IOException { //Creating FileInputStream object File file = new File("D:/myFile.txt"); FileInputStream fis = new FileInputStream(file); byte bytes[] = new byte[(int) file.length()]; //Reading data from the file fis.read(bytes); //Writing data to another file File out = new File("D:/CopyOfmyFile.txt"); FileOutputStream outputStream = new FileOutputStream(out); //Writing data to the file outputStream.write(bytes); outputStream.flush(); System.out.println("Data successfully written in the specified file"); } }
Output
Data successfully written in the specified file
Example Reader/Writer Streams
Following Java program reads data from a particular file using FileReader and writes it to another, using FileWriter.
import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class IOStreamsExample { public static void main(String args[]) throws IOException { //Creating FileReader object File file = new File("D:/myFile.txt"); FileReader reader = new FileReader(file); char chars[] = new char[(int) file.length()]; //Reading data from the file reader.read(chars); //Writing data to another file File out = new File("D:/CopyOfmyFile.txt"); FileWriter writer = new FileWriter(out); //Writing data to the file writer.write(chars); writer.flush(); System.out.println("Data successfully written in the specified file"); } }
Output
Data successfully written in the specified file
- Related Articles
- Character Stream vs Byte Stream in Java\n
- C++ Stream Classes Structure
- Difference between Block Cipher and Stream Cipher
- Difference between Batch Processing and Stream Processing
- How to convert an input stream to byte array in java?
- Java Program to create Stream from a String/Byte Array
- Stream In Java
- What is a Stream and what are the types of Streams and classes in Java?
- Java Program to read the next byte of data from the input stream
- Stream sorted() in Java
- Java Stream Collectors toCollection() in Java
- Array To Stream in Java
- How to convert byte array to an object stream in C#?
- Java Program to close this input stream and release any system resources associated with the stream
- Convert Stream to Set in Java
