The difference and connection between byte stream and character stream
Byte streams and character streams in Java are two different streams used to process inputs and outputs.
the difference
1. Data type: Byte streams read and write in bytes, while character streams read and write in characters. A byte stream can process any type of data, including text, images, audio, etc., while a character stream is mainly used to process text data.
2. Encoding method: Byte streams directly read and write data in the form of bytes, and do not care about the specific encoding method of the data. The character stream reads and writes data in the form of characters, and converts characters into bytes according to the specified character encoding for processing.
3. Processing efficiency: The processing efficiency of a byte stream is usually higher than that of a character stream, because the byte stream directly operates the underlying byte data and does not require character encoding conversion.
4. Usage scenario: Byte streams are suitable for processing binary data, such as file copying, network transmission, etc. Character streams are suitable for processing text data, such as reading and writing files, processing text, etc.
connect
1. Inheritance relationship: Byte streams and character streams are both subclasses of abstract classes InputStream and OutputStream, as well as subclasses of Reader and Writer.
2. Usage method: Both byte streams and character streams provide similar read and write methods, such as read() and write() methods.
3. Conversion: Byte streams can be converted into character streams through the InputStreamReader and OutputStreamWriter classes to process text data.
4. Character streams are based on byte streams. In a character stream, character encoding is used to process character data, and character encoding is implemented through a byte stream. Therefore, character streams can be regarded as advanced encapsulation of byte streams, providing more convenient character processing functions.
Byte stream related classes:
1. InputStream: The superclass of all byte input streams.
2. FileInputStream: The input stream that reads data from the file.
3. BufferedInputStream: Input stream with buffers to improve read efficiency.
4. DataInputStream: You can read input streams of Java basic data types from the input stream.
5. ObjectInputStream: You can read the input stream of Java objects from the input stream.
6. OutputStream: Superclass of all byte output streams.
7. FileOutputStream: The output stream that writes data to the file.
8. BufferedOutputStream: Output stream with buffer to improve write efficiency.
9. DataOutputStream: The output stream of Java basic data type can be written to the output stream.
10. ObjectOutputStream: The output stream that can write Java objects to the output stream.
11. ByteArrayInputStream: is an input stream that reads data from a byte array
12. ByteArrayOutputStream: is an output stream that writes data into a byte array.
Character stream related classes:
1. Reader: The superclass of all character input streams.
2. FileReader: The input stream of characters read from the file.
3. BufferedReader: Input stream with buffer to improve read efficiency.
4. InputStreamReader: The input stream that converts a byte stream into a character stream.
5. Writer: The superclass of all character output streams.
6. FileWriter: The output stream of characters written to the file.
7. BufferedWriter: Output stream with buffers to improve write efficiency.
8. OutputStreamWriter: The output stream that converts a character stream into a byte stream.
Byte stream related class sample code
InputStream sample code:
InputStream inputStream = new FileInputStream(""); int data = (); while (data != -1) { ((char) data); data = (); } ();
This code creates a file input stream that reads every byte in the file and converts it into characters to output to the console. Finally the input stream is closed.
FileInputStream sample code:
FileInputStream inputStream = new FileInputStream(""); int data = (); while (data != -1) { ((char) data); data = (); } ();
This code creates a file input stream that reads every byte in the file and converts it into characters to output to the console. Finally the input stream is closed.
BufferedInputStream sample code:
InputStream inputStream = new FileInputStream(""); BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream); int data = (); while (data != -1) { ((char) data); data = (); } ();
This code creates a file input stream and wraps it in an input stream with a buffer. Each byte in the file is read and converted into characters and output to the console. Finally the input stream is closed.
DataInputStream sample code:
InputStream inputStream = new FileInputStream(""); DataInputStream dataInputStream = new DataInputStream(inputStream); int data = (); (data); ();
This code creates a file input stream and wraps it in an input stream that can read Java basic data types. An integer in the file is read and output to the console. Finally the input stream is closed.
ObjectInputStream sample code:
InputStream inputStream = new FileInputStream(""); ObjectInputStream objectInputStream = new ObjectInputStream(inputStream); Object object = (); (object); ();
This code creates a file input stream and wraps it in an input stream that can read Java objects. An object in the file is read and output to the console. Finally the input stream is closed.
OutputStream sample code:
OutputStream outputStream = new FileOutputStream(""); String data = "Hello, world!"; (()); ();
This code creates a file output stream, converts a string into a byte array, and writes it to the file. Finally the output stream is turned off.
FileOutputStream sample code:
FileOutputStream outputStream = new FileOutputStream(""); String data = "Hello, world!"; (()); ();
This code creates a file output stream, converts a string into a byte array, and writes it to the file. Finally the output stream is turned off.
BufferedOutputStream sample code:
OutputStream outputStream = new FileOutputStream(""); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream); String data = "Hello, world!"; (()); ();
This code creates a file output stream and wraps it in an output stream with a buffer. Convert a string to a byte array and write it to a file. Finally the output stream is turned off.
DataOutputStream sample code:
OutputStream outputStream = new FileOutputStream(""); DataOutputStream dataOutputStream = new DataOutputStream(outputStream); int data = 42; (data); ();
This code creates a file output stream and wraps it in an output stream that can be written to the Java basic data type. Write an integer to the file. Finally the output stream is turned off.
ObjectOutputStream sample code:
OutputStream outputStream = new FileOutputStream(""); ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream); Object object = new Object(); (object); ();
This code creates a file output stream and wraps it in an output stream that can be written to a Java object. Write an object to a file. Finally the output stream is turned off.
ByteArrayInputStream sample code:
byte[] byteArray = {1, 2, 3, 4, 5}; ByteArrayInputStream inputStream = new ByteArrayInputStream(byteArray); int data; while ((data = ()) != -1) { (data); }
Created a ByteArrayInputStream object, passing an array of bytes to it. We then use a while loop to read the data from the input stream and print it out on the console.
ByteArrayOutputStream sample code:
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); (1); (2); (3); (4); (5); byte[] byteArray = (); ((byteArray));
A ByteArrayOutputStream object is created and the data is written to it using the write method. Finally, we use the toByteArray method to convert the data into a byte array and print it out on the console.
Extensions:
The relationship between ByteArrayInputStream and ObjectInputStream is given, and example code is given for combining ByteArrayInputStream and ObjectInputStream
ByteArrayInputStream and ObjectInputStream are both input stream classes in Java IO library for reading data from byte arrays and object streams. Their relationship is that the ObjectInputStream is built on the ByteArrayInputStream, that is, the ObjectInputStream requires an InputStream object as a parameter to be initialized, and ByteArrayInputStream happens to be an InputStream object.
Here is a sample code that demonstrates how to use ByteArrayInputStream and ObjectInputStream to read Java objects:
import ; import ; import ; import ; public class ObjectInputStreamExample { public static void main(String[] args) throws IOException, ClassNotFoundException { // Create a byte array containing Java objects byte[] objectBytes = createObjectBytes(); // Create a ByteArrayInputStream object ByteArrayInputStream bais = new ByteArrayInputStream(objectBytes); // Create an ObjectInputStream object and pass the ByteArrayInputStream object as a parameter ObjectInputStream ois = new ObjectInputStream(bais); // Read Java object from ObjectInputStream ArrayList<String> list = (ArrayList<String>) (); // Print the read Java object (list); } private static byte[] createObjectBytes() throws IOException { ArrayList<String> list = new ArrayList<>(); ("hello"); ("world"); // Create a ByteArrayOutputStream object ByteArrayOutputStream baos = new ByteArrayOutputStream(); // Create an ObjectOutputStream object and pass the ByteArrayOutputStream object as a parameter ObjectOutputStream oos = new ObjectOutputStream(baos); // Write Java objects into ObjectOutputStream (list); // Returns the byte array in ByteArrayOutputStream return (); } }
In the above example code, first, a byte array containing Java objects is created by the createObjectBytes method. Then, a ByteArrayInputStream object is created and this byte array is passed in as a parameter. Next, an ObjectInputStream object is created and the ByteArrayInputStream object is passed in as a parameter. Finally, the Java object is read through the ObjectInputStream object and printed out.
Class sample code related to character streams
Reader sample code:
Reader reader = new FileReader(""); int data = (); while (data != -1) { ((char) data); data = (); } ();
Code meaning: Create a Reader object and use FileReader to use the file "" as the input stream. Then use the read() method to read the characters and print them out until they are read to the end of the file (returning -1). Finally, close the input stream.
FileReader sample code:
FileReader reader = new FileReader(""); int data = (); while (data != -1) { ((char) data); data = (); } ();
Code meaning: Create a FileReader object and take the file "" as the input stream. Then use the read() method to read the characters and print them out until they are read to the end of the file (returning -1). Finally, close the input stream.
BufferedReader sample code:
BufferedReader reader = new BufferedReader(new FileReader("")); String line = (); while (line != null) { (line); line = (); } ();
Code meaning: Create a BufferedReader object, use FileReader to take the file "" as the input stream, with a buffer. Then use the readLine() method to read a line of characters and print them out until they are read to the end of the file (returning null). Finally, close the input stream.
InputStreamReader sample code:
InputStreamReader reader = new InputStreamReader(new FileInputStream(""), "UTF-8"); int data = (); while (data != -1) { ((char) data); data = (); } ();
Code meaning: Create an InputStreamReader object, use FileInputStream to input file "" as a byte stream, and convert it into a character stream input stream. Specifies that the character encoding is UTF-8. Then use the read() method to read the characters and print them out until they are read to the end of the file (returning -1). Finally, close the input stream.
Writer sample code:
Writer writer = new FileWriter(""); ("Hello, World!"); ();
Code meaning: Create a Writer object and use FileWriter to write characters into the file "". Then use the write() method to write the string "Hello, World!". Finally, the output stream is turned off.
FileWriter sample code:
FileWriter writer = new FileWriter(""); ("Hello, World!"); ();
Code meaning: Create a FileWriter object and write characters to the file "". Then use the write() method to write the string "Hello, World!". Finally, the output stream is turned off.
BufferedWriter sample code:
BufferedWriter writer = new BufferedWriter(new FileWriter("")); ("Hello, World!"); (); ("This is a new line."); ();
Code meaning: Create a BufferedWriter object, use FileWriter to write characters to the file "" with a buffer. Then use the write() method to write the string "Hello, World!", use the newLine() method to write a new line, and then use the write() method to write the string "This is a new line." Finally, the output stream is turned off.
OutputStreamWriter sample code:
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(""), "UTF-8"); ("Hello, World!"); ();
Code meaning: Create an OutputStreamWriter object, use FileOutputStream to output the byte stream to the file "" and convert it into a character stream output stream. Specifies that the character encoding is UTF-8. Then use the write() method to write the string "Hello, World!". Finally, the output stream is turned off.
This is the article about the difference and connection between byte streams and character streams in Java. For more related Java byte streams and character streams, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!