Java中字節(jié)流和字符流的區(qū)別與聯(lián)系
字節(jié)流和字符流的區(qū)別與聯(lián)系
Java中的字節(jié)流和字符流是用于處理輸入和輸出的兩種不同的流。
區(qū)別
1. 數(shù)據(jù)類型:字節(jié)流以字節(jié)為單位進行讀寫,而字符流以字符為單位進行讀寫。字節(jié)流可以處理任意類型的數(shù)據(jù),包括文本、圖像、音頻等,而字符流主要用于處理文本數(shù)據(jù)。
2. 編碼方式:字節(jié)流是以字節(jié)的形式直接讀寫數(shù)據(jù),不關心數(shù)據(jù)的具體編碼方式。而字符流是以字符的形式讀寫數(shù)據(jù),會根據(jù)指定的字符編碼將字符轉換為字節(jié)進行處理。
3. 處理效率:字節(jié)流的處理效率通常比字符流高,因為字節(jié)流直接操作底層的字節(jié)數(shù)據(jù),不需要進行字符編碼的轉換。
4. 使用場景:字節(jié)流適用于處理二進制數(shù)據(jù),如文件的復制、網(wǎng)絡傳輸?shù)?。字符流適用于處理文本數(shù)據(jù),如文件的讀寫、文本的處理等。
聯(lián)系
1. 繼承關系:字節(jié)流和字符流都是抽象類InputStream和OutputStream的子類,以及Reader和Writer的子類。
2. 使用方式:字節(jié)流和字符流都提供了類似的讀寫方法,如read()和write()方法。
3. 轉換:可以通過InputStreamReader和OutputStreamWriter類將字節(jié)流轉換為字符流,以便處理文本數(shù)據(jù)。
4. 字符流是建立在字節(jié)流的基礎上的。在字符流中,使用了字符編碼來處理字符數(shù)據(jù),而字符編碼又是通過字節(jié)流來實現(xiàn)的。因此,字符流可以看作是字節(jié)流的高級封裝,提供了更方便的字符處理功能。
字節(jié)流相關的類:
1. InputStream:所有字節(jié)輸入流的超類。
2. FileInputStream:從文件中讀取數(shù)據(jù)的輸入流。
3. BufferedInputStream:帶有緩沖區(qū)的輸入流,提高讀取效率。
4. DataInputStream:可以從輸入流中讀取Java基本數(shù)據(jù)類型的輸入流。
5. ObjectInputStream:可以從輸入流中讀取Java對象的輸入流。
6. OutputStream:所有字節(jié)輸出流的超類。
7. FileOutputStream:向文件中寫入數(shù)據(jù)的輸出流。
8. BufferedOutputStream:帶有緩沖區(qū)的輸出流,提高寫入效率。
9. DataOutputStream:可以向輸出流中寫入Java基本數(shù)據(jù)類型的輸出流。
10. ObjectOutputStream:可以向輸出流中寫入Java對象的輸出流。
11. ByteArrayInputStream:是一個輸入流,它從一個字節(jié)數(shù)組中讀取數(shù)據(jù)
12. ByteArrayOutputStream:是一個輸出流,它將數(shù)據(jù)寫入一個字節(jié)數(shù)組中。
字符流相關的類:
1. Reader:所有字符輸入流的超類。
2. FileReader:從文件中讀取字符的輸入流。
3. BufferedReader:帶有緩沖區(qū)的輸入流,提高讀取效率。
4. InputStreamReader:將字節(jié)流轉換為字符流的輸入流。
5. Writer:所有字符輸出流的超類。
6. FileWriter:向文件中寫入字符的輸出流。
7. BufferedWriter:帶有緩沖區(qū)的輸出流,提高寫入效率。
8. OutputStreamWriter:將字符流轉換為字節(jié)流的輸出流。
字節(jié)流相關的類示例代碼
InputStream示例代碼:
InputStream inputStream = new FileInputStream("example.txt");
int data = inputStream.read();
while (data != -1) {
System.out.print((char) data);
data = inputStream.read();
}
inputStream.close();這段代碼創(chuàng)建了一個文件輸入流,讀取了文件中的每一個字節(jié),并將其轉換為字符輸出到控制臺上。最后關閉了輸入流。
FileInputStream示例代碼:
FileInputStream inputStream = new FileInputStream("example.txt");
int data = inputStream.read();
while (data != -1) {
System.out.print((char) data);
data = inputStream.read();
}
inputStream.close();這段代碼創(chuàng)建了一個文件輸入流,讀取了文件中的每一個字節(jié),并將其轉換為字符輸出到控制臺上。最后關閉了輸入流。
BufferedInputStream示例代碼:
InputStream inputStream = new FileInputStream("example.txt");
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
int data = bufferedInputStream.read();
while (data != -1) {
System.out.print((char) data);
data = bufferedInputStream.read();
}
bufferedInputStream.close();這段代碼創(chuàng)建了一個文件輸入流,并將其包裝在一個帶有緩沖區(qū)的輸入流中。讀取了文件中的每一個字節(jié),并將其轉換為字符輸出到控制臺上。最后關閉了輸入流。
DataInputStream示例代碼:
InputStream inputStream = new FileInputStream("example.txt");
DataInputStream dataInputStream = new DataInputStream(inputStream);
int data = dataInputStream.readInt();
System.out.println(data);
dataInputStream.close();這段代碼創(chuàng)建了一個文件輸入流,并將其包裝在一個可以讀取Java基本數(shù)據(jù)類型的輸入流中。讀取了文件中的一個整數(shù),并將其輸出到控制臺上。最后關閉了輸入流。
ObjectInputStream示例代碼:
InputStream inputStream = new FileInputStream("example.txt");
ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
Object object = objectInputStream.readObject();
System.out.println(object);
objectInputStream.close();這段代碼創(chuàng)建了一個文件輸入流,并將其包裝在一個可以讀取Java對象的輸入流中。讀取了文件中的一個對象,并將其輸出到控制臺上。最后關閉了輸入流。
OutputStream示例代碼:
OutputStream outputStream = new FileOutputStream("example.txt");
String data = "Hello, world!";
outputStream.write(data.getBytes());
outputStream.close();這段代碼創(chuàng)建了一個文件輸出流,將一個字符串轉換為字節(jié)數(shù)組,并將其寫入到文件中。最后關閉了輸出流。
FileOutputStream示例代碼:
FileOutputStream outputStream = new FileOutputStream("example.txt");
String data = "Hello, world!";
outputStream.write(data.getBytes());
outputStream.close();這段代碼創(chuàng)建了一個文件輸出流,將一個字符串轉換為字節(jié)數(shù)組,并將其寫入到文件中。最后關閉了輸出流。
BufferedOutputStream示例代碼:
OutputStream outputStream = new FileOutputStream("example.txt");
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
String data = "Hello, world!";
bufferedOutputStream.write(data.getBytes());
bufferedOutputStream.close();這段代碼創(chuàng)建了一個文件輸出流,并將其包裝在一個帶有緩沖區(qū)的輸出流中。將一個字符串轉換為字節(jié)數(shù)組,并將其寫入到文件中。最后關閉了輸出流。
DataOutputStream示例代碼:
OutputStream outputStream = new FileOutputStream("example.txt");
DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
int data = 42;
dataOutputStream.writeInt(data);
dataOutputStream.close();這段代碼創(chuàng)建了一個文件輸出流,并將其包裝在一個可以寫入Java基本數(shù)據(jù)類型的輸出流中。將一個整數(shù)寫入到文件中。最后關閉了輸出流。
ObjectOutputStream示例代碼:
OutputStream outputStream = new FileOutputStream("example.txt");
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
Object object = new Object();
objectOutputStream.writeObject(object);
objectOutputStream.close();這段代碼創(chuàng)建了一個文件輸出流,并將其包裝在一個可以寫入Java對象的輸出流中。將一個對象寫入到文件中。最后關閉了輸出流。
ByteArrayInputStream示例代碼:
byte[] byteArray = {1, 2, 3, 4, 5};
ByteArrayInputStream inputStream = new ByteArrayInputStream(byteArray);
int data;
while ((data = inputStream.read()) != -1) {
System.out.println(data);
}創(chuàng)建了一個ByteArrayInputStream對象,將一個字節(jié)數(shù)組傳遞給它。然后我們使用while循環(huán)從輸入流中讀取數(shù)據(jù),并在控制臺上打印出來。
ByteArrayOutputStream示例代碼:
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); outputStream.write(1); outputStream.write(2); outputStream.write(3); outputStream.write(4); outputStream.write(5); byte[] byteArray = outputStream.toByteArray(); System.out.println(Arrays.toString(byteArray));
創(chuàng)建了一個ByteArrayOutputStream對象,并使用write方法向它寫入數(shù)據(jù)。最后,我們使用toByteArray方法將數(shù)據(jù)轉換為字節(jié)數(shù)組,并在控制臺上打印出來。
擴展:
ByteArrayInputStream和ObjectInputStream的關系,并給出ByteArrayInputStream和ObjectInputStream結合使用的示例代碼
ByteArrayInputStream和ObjectInputStream都是Java IO庫中的輸入流類,用于從字節(jié)數(shù)組和對象流中讀取數(shù)據(jù)。它們的關系是,ObjectInputStream是建立在ByteArrayInputStream之上的,即ObjectInputStream需要一個InputStream對象作為參數(shù)來初始化,而ByteArrayInputStream正好是一種InputStream對象。
下面是一個示例代碼,演示如何使用ByteArrayInputStream和ObjectInputStream結合使用來讀取Java對象:
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
public class ObjectInputStreamExample {
public static void main(String[] args) throws IOException, ClassNotFoundException {
// 創(chuàng)建一個包含Java對象的字節(jié)數(shù)組
byte[] objectBytes = createObjectBytes();
// 創(chuàng)建一個ByteArrayInputStream對象
ByteArrayInputStream bais = new ByteArrayInputStream(objectBytes);
// 創(chuàng)建一個ObjectInputStream對象,將ByteArrayInputStream對象作為參數(shù)傳入
ObjectInputStream ois = new ObjectInputStream(bais);
// 從ObjectInputStream中讀取Java對象
ArrayList<String> list = (ArrayList<String>) ois.readObject();
// 打印讀取到的Java對象
System.out.println(list);
}
private static byte[] createObjectBytes() throws IOException {
ArrayList<String> list = new ArrayList<>();
list.add("hello");
list.add("world");
// 創(chuàng)建一個ByteArrayOutputStream對象
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 創(chuàng)建一個ObjectOutputStream對象,將ByteArrayOutputStream對象作為參數(shù)傳入
ObjectOutputStream oos = new ObjectOutputStream(baos);
// 將Java對象寫入ObjectOutputStream中
oos.writeObject(list);
// 返回ByteArrayOutputStream中的字節(jié)數(shù)組
return baos.toByteArray();
}
}在上面的示例代碼中,首先通過createObjectBytes方法創(chuàng)建了一個包含Java對象的字節(jié)數(shù)組。然后,創(chuàng)建了一個ByteArrayInputStream對象,并將這個字節(jié)數(shù)組作為參數(shù)傳入。接著,創(chuàng)建了一個ObjectInputStream對象,并將ByteArrayInputStream對象作為參數(shù)傳入。最后,通過ObjectInputStream對象讀取了Java對象,并打印出來。
字符流相關的類示例代碼
Reader示例代碼:
Reader reader = new FileReader("file.txt");
int data = reader.read();
while (data != -1) {
System.out.print((char) data);
data = reader.read();
}
reader.close();代碼含義:創(chuàng)建一個Reader對象,使用FileReader將文件"file.txt"作為輸入流。然后使用read()方法讀取字符,并將其打印出來,直到讀取到文件末尾(返回-1)。最后關閉輸入流。
FileReader示例代碼:
FileReader reader = new FileReader("file.txt");
int data = reader.read();
while (data != -1) {
System.out.print((char) data);
data = reader.read();
}
reader.close();代碼含義:創(chuàng)建一個FileReader對象,將文件"file.txt"作為輸入流。然后使用read()方法讀取字符,并將其打印出來,直到讀取到文件末尾(返回-1)。最后關閉輸入流。
BufferedReader示例代碼:
BufferedReader reader = new BufferedReader(new FileReader("file.txt"));
String line = reader.readLine();
while (line != null) {
System.out.println(line);
line = reader.readLine();
}
reader.close();代碼含義:創(chuàng)建一個BufferedReader對象,使用FileReader將文件"file.txt"作為輸入流,并帶有緩沖區(qū)。然后使用readLine()方法讀取一行字符,并將其打印出來,直到讀取到文件末尾(返回null)。最后關閉輸入流。
InputStreamReader示例代碼:
InputStreamReader reader = new InputStreamReader(new FileInputStream("file.txt"), "UTF-8");
int data = reader.read();
while (data != -1) {
System.out.print((char) data);
data = reader.read();
}
reader.close();代碼含義:創(chuàng)建一個InputStreamReader對象,使用FileInputStream將文件"file.txt"作為字節(jié)流輸入流,并將其轉換為字符流輸入流。指定字符編碼為UTF-8。然后使用read()方法讀取字符,并將其打印出來,直到讀取到文件末尾(返回-1)。最后關閉輸入流。
Writer示例代碼:
Writer writer = new FileWriter("file.txt");
writer.write("Hello, World!");
writer.close();代碼含義:創(chuàng)建一個Writer對象,使用FileWriter將字符寫入到文件"file.txt"中。然后使用write()方法寫入字符串"Hello, World!"。最后關閉輸出流。
FileWriter示例代碼:
FileWriter writer = new FileWriter("file.txt");
writer.write("Hello, World!");
writer.close();代碼含義:創(chuàng)建一個FileWriter對象,將字符寫入到文件"file.txt"中。然后使用write()方法寫入字符串"Hello, World!"。最后關閉輸出流。
BufferedWriter示例代碼:
BufferedWriter writer = new BufferedWriter(new FileWriter("file.txt"));
writer.write("Hello, World!");
writer.newLine();
writer.write("This is a new line.");
writer.close();代碼含義:創(chuàng)建一個BufferedWriter對象,使用FileWriter將字符寫入到文件"file.txt"中,并帶有緩沖區(qū)。然后使用write()方法寫入字符串"Hello, World!“,使用newLine()方法寫入一個新行,再使用write()方法寫入字符串"This is a new line.”。最后關閉輸出流。
OutputStreamWriter示例代碼:
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream("file.txt"), "UTF-8");
writer.write("Hello, World!");
writer.close();代碼含義:創(chuàng)建一個OutputStreamWriter對象,使用FileOutputStream將字節(jié)流輸出到文件"file.txt"中,并將其轉換為字符流輸出流。指定字符編碼為UTF-8。然后使用write()方法寫入字符串"Hello, World!"。最后關閉輸出流。
到此這篇關于Java中字節(jié)流和字符流的區(qū)別與聯(lián)系的文章就介紹到這了,更多相關java 字節(jié)流河字符流內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Springboot詳解RocketMQ實現(xiàn)消息發(fā)送與接收流程
這篇文章主要介紹了SpringBoot整合RocketMQ實現(xiàn)消息發(fā)送和接收功能,我們使用主流的SpringBoot框架整合RocketMQ來講解,使用方便快捷,本文分步驟給大家介紹的非常詳細,需要的朋友可以參考下2022-06-06
解決spring?data?jpa?saveAll()?保存過慢問題
這篇文章主要介紹了解決spring?data?jpa?saveAll()保存過慢問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
淺談SpringBoot中的@Conditional注解的使用
這篇文章主要介紹了淺談SpringBoot中的@Conditional注解的使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-04-04

