java中IO流的字節流和字符流有什么區別?可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
一、緒論
如果要進行文件內容的操作那么必須依靠數據流完成,而數據流分為兩種:
字節流:InputStream(字節輸入流)、OutputStream(字節輸出流);
字符流:Reader(字符輸入流)、Writer(字符輸出流);
二、區別
字節流是原生的操作,而字符流是經過處理后的操作。
在進行網絡數據傳輸、磁盤數據保存所保存所支持的數據類型只有:字節。而所有磁盤中的數據必須先讀取到內存后才能進行操作,而內存中會幫助我們把字節變為字符。字符更加適合處理中文。如果處理中文使用字符流,其他的任何數據都使用字節流。
三、字節輸出流:(OutputStream)
OutputStream類定義有三個重要的輸出操作方法:
1. 將給定的字節數組內容全部輸出:
public void write(byte b[]) throws IOException
2. 將部分字節數組內容輸出:(重點)
public void write(byte b[], int off, int len) throws IOException
3. 輸出單個字節:
public abstract void write(int b) throws IOException
OutputStream是一個抽象類,按照抽象類的基本原則來講,如果想要取得OutputStream類的實例化對象那么一定需要子類,如果要進行文件的操作,可以使用FileOutputStream類來處理,這個類的構造方法如下:
1. 接收File類(覆蓋):
public FileOutputStream(File file) throws FileNotFoundException
2. 接收File類(追加):
public FileOutputStream(File file, boolean append)
//第一步:定義要輸出的文件的File類對象
File file = new File("e:"+File.separator+"hello"+File.separator+"my.txt");
//輸出信息的時候文件可以不存在,但是目錄必須存在
if(!file.getParentFile().exists()) {//父路徑不存在
file.getParentFile().mkdirs();//創建父路徑
}
//第二步:利用OutputStream的子類為父類進行實例化
OutputStream output = new FileOutputStream(file);
//第三步:輸出文字信息
String msg = "富則達濟天下,窮則獨善其身";//字符串
//為了方便輸出需要將字符串變為字節數組
byte data[] = msg.getBytes();//變為字節數組
output.write(data);//輸出數據
output.close();//關閉流輸出文件的部分內容
output.write(data,0,10);//輸出部分數據
使用循環方式進行單個字節的信息輸出
for(int x = 0;x < data.length; x++) {
output.write(data[x]);//單個字節輸出數據
}但是使用單個字節輸出會將之前的內容都被覆蓋了。所以需要進行數據的追加操作
OutputStream output = new FileOutputStream(file,true);//此處為追加操作
四、字節輸入流:(InputStream)
InputStream類中定義有三個數據的讀取操作方法:
1.讀取單個字節:
public abstract int read() throws IOException;
每次執行此方法將讀取當個字節數據,如果已經讀取完成了,那么最后返回-1。
2.讀取數據到字節數組中:
public int read(byte b[]) throws IOException.
最常用方法,每次講數據讀取到數組之中,那么會返回一個讀取長度的數據,如果沒有數據則返回的長度為-1,
可是要考慮兩種情況:
要讀取的內容大于開辟的數組內容:長度就是整個數組的長度。
要讀取的內容小于開辟數組的內容,長度就是全部最后的內容長度,數組裝不滿。
3.讀取部分內容到字節數組中:
public int read(byte b[], int off,int len) throws IOException
每次讀取內容到部分字節數組,只允許讀取滿限制的數組的字節個數。此方法依然會返回讀取的長度。
InputStream是一個抽象類,所以要進行文件的讀取使用FileInputStream子類,子類定義的構造方法如下:
構造方法:public FileInputStream(File file) throws FileNotFoundException.
//第一步:定義要輸出的文件的File類對象
File file = new File("e:"+File.separator+"hello"+File.separator+"my.txt");
//第二步:實例化InputStream
InputStream input = new FileInputStream(file);
//實現數據的讀取操作
byte data[] = new byte[1024];
int len = input.read(data);//將數據讀取到數組之中
System.out.println("讀取的內容【" +new String(data,0,len)+"】");
//第四步關閉輸入流
input.close();補充:DataInputStream 和 DataOutputStream
DataInputStream類繼承了InputStream。也就是說DataInputStream是InputStream的子類。但它們同是實現了DataInput接口。
DataOutputStream類繼承了OutputStream。也就是說DataOutputStream是OutputStream的子類。但它們同是實現了DataOutput接口。
五、字符輸出流:(Writer)
Writer是一個抽象類,要進行文件字符流操作可以使用FileWriter類處理,其構造方法為:
public FileWriter(File file)
//第一步:定義要輸出的文件的File類對象
File file = new File("e:"+File.separator+"hello"+File.separator+"my.txt");//你的路徑
if(!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
Writer out = new FileWriter(file);
String str = "一定要好好學習,天天向上。。。";
out.write(str);
out.close();雖然Writer類提供有字符數組的輸出操作能力,但是從本質上來講使用Writer類就意味著要執行字符串的直接輸出。字符流最適合操作中文,但并不意味著字節流就無法操作中文。
六、字符輸入流:(Reader)
Reader是一個抽象類,要進行文件字符流操作可以使用FileReader類處理,其構造方法為:
public FileReader (File file)
//第一步:定義要輸出的文件的File類對象
File file = new File("e:"+File.separator+"hello"+File.separator+"my.txt");//你的路徑
if(file.exists()) {
Reader in = new FileReader(file);
char data[] = new char[1024];
int len = in.read(data);//向字符數組保存數據,返回長度。
System.out.println(new String(data,0,len));
in.close();七、補充
為了提高字符流讀寫的效率,引入了緩沖機制,java提供了緩存流類:BufferedInputStream、BufferedOutputStream 類和BufferedReader、 BufferedWriter類
//使用buffer進行文件讀寫
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream
(new File("D:\\BIGBANG - IF YOU (Live).mp3")));
File newFile = new File("D:\\copyMusic\\BIGBANG - IF YOU (Live).mp3");
newFile.createNewFile();
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(newFile));
byte[] bytes= new byte[1024];
int length = 0;
while ((length=bufferedInputStream.read(bytes))!=-1){
bufferedOutputStream.write(bytes,0,length);
}以上就是java中IO流的字節流和字符流的區別,看完之后是否有所收獲呢?如果想了解更多相關內容,歡迎關注億速云行業資訊,感謝各位的閱讀。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。