2022 年最有前景的 5 個 Web IDE
阿新 • • 發佈:2021-12-22
位元組流
位元組流的概述和分類
IO流概述:
- IO:輸入/輸出(Input/Output)
- 流:
- 是一種抽象概念,是物件傳輸的總稱。
- 也就是說資料在裝置間的傳輸稱為流。
- 本質是資料傳輸
- IO流就是用來裝置間資料傳輸問題的
- 常見應用:檔案複製、檔案上傳、檔案下載
IO流分類:
- 按照資料流向:
- 輸入流:讀資料
- 輸出流:寫資料
- 按照資料型別來分
- 位元組流
- 位元組輸入流;位元組輸出流
- 字元流
- 字元輸入流;字元輸出流
- 位元組流
- 一般按照資料型別來分
- 使用場景:
- 能用記事本開啟且能讀懂,就使用字元流;
- 否則使用位元組流(萬能)。
位元組流
1.位元組流抽象基類
- InputStream:這個抽象類是表示位元組輸入流的所有類的超類
- OutputStream:這個抽象類是表示位元組輸出流的所有類的超類
- 子類名特點:以其父類名作為其子類名的字尾
FileOutputStream:檔案輸出流用於將資料寫入File
- FileOutputStream(String name):
- 建立檔案輸出流以指定的名稱寫入檔案
2.步驟:
IO流寫入資料:
1.寫物件
(1).呼叫系統功能建立了檔案
(2).建立了位元組輸出流物件
(3).讓位元組輸出流指向建立好的檔案
2.呼叫位元組輸出流物件寫資料
3.釋放資源
3.位元組流寫資料的三種方式
(1)void write(int b)
將指定的位元組寫入此檔案輸出流,一次寫一個位元組資料
(2)void write(byte[] b)
將b.length位元組從指定的位元組陣列寫入此檔案輸出流
一次寫一個位元組陣列資料
(3)void write(byte[] b,int off,int len)
將len位元組從指定的位元組陣列開始,從偏移量off(開始索引位置)開始寫入此檔案輸出流
一次寫一個位元組陣列的部分資料
4.位元組流寫資料的兩個小問題
-
位元組流如何實現換行:
windows:\r\n Linux:\n Mac:\r
-
位元組流寫資料如何實現追加寫入呢?
public FileOutputStream(String name,boolean append)
建立檔案輸出流以指定的名稱寫入檔案。
如果第二個指定的引數為true,則將寫入檔案的末尾而不是開頭
5.位元組流資料加異常處理
- try/catch處理
//catch塊
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null){
try {
fos.close();//關閉資源
} catch (IOException e) {
e.printStackTrace();
}
}
}
6.位元組流讀資料(一次讀一個位元組資料)
-
需求:把檔案中的fos.txt中的內容讀取出來在控制檯輸出
FileInputStream:從檔案系統中輸入位元組
-
FileInputStream(String name):通過開啟與實際檔案連線來建立一個FileInputStream,該檔案由檔案系統中的路徑名name命名
-
使用步驟:
- 1.建立位元組輸入流物件
- 2.呼叫位元組輸入流對像的讀資料方法
- 3.釋放資源
//位元組流讀資料標準寫法
int by;
while((by=fis.read()) != -1){
System.out.print((char)by);
}
案例:複製文字檔案
分析:
- 思路:
練習:
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("E:\\2021Study\\test.txt");
FileOutputStream fos = new FileOutputStream("基礎語法\\test.txt");
int by;
while ((by=fis.read())!= -1){
fos.write(by);
}
fos.close();
fis.close();
}
執行結果:
案例:位元組流讀資料
package com.guoba.day1222;
import java.io.FileInputStream;
import java.io.IOException;
/*
需求:
把檔案fos.txt中的內容讀取出來在控制檯輸出
使用位元組流輸入資料的步驟:
1.建立位元組輸入流物件
2.呼叫位元組輸入流物件的讀資料方法
3.釋放資源
*/
public class Demo01 {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("基礎語法\\fis.txt");
// byte[] bytes = new byte[5];
//
// int readlen = fis.read(bytes);
// System.out.println(readlen);
// System.out.println(new String(bytes));
//
// readlen = fis.read(bytes);
// System.out.println(readlen);
// System.out.println(new String(bytes));
//
// readlen = fis.read(bytes);
// System.out.println(readlen);
// System.out.println(new String(bytes));
//
// readlen = fis.read(bytes);
// System.out.println(readlen);
// System.out.println(new String(bytes));
byte[] bytes = new byte[1024];//陣列長度為1024或其整數倍
int len;
//迴圈改進
while((len = fis.read(bytes))!=-1){
System.out.println(new String(bytes,0,len));
}
fis.close();
}
}
案例:複製圖片
package com.guoba.day1222;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/*
複製圖片
需求:把 資料來源圖片 複製到 目標地址
思路:
1.根據資料來源建立位元組輸入流物件
2.根據目的地建立位元組輸入流物件
3.讀寫資料,複製圖片(一次讀取一個位元組陣列,一次寫入一個位元組陣列)
*/
public class Demo02 {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("E:\\2021Study\\西施.jpg");
FileOutputStream fos = new FileOutputStream("基礎語法\\西施.jpg");
byte[] bytes = new byte[2048];
int len;
while ((len=fis.read(bytes))!=-1){
fos.write(bytes);
}
}
}
7.位元組緩衝流
位元組緩衝流:
- BufferOutputStream:該類實現緩衝輸出流。通過設定這樣的輸出流,應用程式可以向底層輸出流寫入位元組,而不必為寫入的每個位元組導致底層系統的呼叫
- BufferedInputStream:建立BufferdedInputStream將建立一個內部緩衝區陣列。當從流中讀取或跳過位元組時,內部緩衝區將根據需要從所包含的輸入流中重新填充,一次很多位元組
構造方法:
- 位元組緩衝輸出流:BufferedOutputStream(OutputStream out)
- 位元組緩衝輸入流:BufferedInputStream(InputStream in)
package com.guoba.day1222;
import java.io.*;
public class Demo03 {
public static void main(String[] args) throws IOException {
//建立位元組緩衝輸出流物件
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("基礎語法\\bos.txt"));
//寫資料
bos.write("hello\r\n".getBytes());
bos.write("world\r\n".getBytes());
//釋放資源
bos.close();
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("基礎語法\\bos.txt"));
//一次讀一個位元組
int by;
while ((by=bis.read())!= -1){
System.out.print((char)by);
}
//一次讀一個位元組陣列
byte[] bytes = new byte[1024];
int len;
while ((len = bis.read(bytes)) != -1) {
System.out.print(new String(bytes,0,len));
}
bis.close();
}
}
案例:複製視訊
package com.guoba.day1222;
import java.io.*;
/*
需求:把 資料來源視訊 複製到 目標地址
思路:
1.根據資料來源建立位元組輸入流物件
2.根據目的地建立位元組輸出流物件
3.對寫資料,複製視訊
4.釋放資源
方式:
》基本位元組流一次讀取一個位元組
》基本位元組流一次讀寫一個位元組陣列
》位元組緩衝流一次讀寫一個位元組
》位元組緩衝流一次讀寫一個位元組陣列
*/
public class Demo04_CopyVideo {
public static void main(String[] args) throws IOException{
long startTime = System.currentTimeMillis();//記錄開始時間
//method1();
//method2();
//method3();
method4();
long endTime = System.currentTimeMillis();//記錄結束時間
System.out.println("共耗時:"+(endTime-startTime)+"毫秒");
}
//位元組流一次讀取一個位元組:共耗時:25807毫秒
public static void method1()throws IOException {
FileInputStream fis = new FileInputStream("E:\\2021Study\\test.mp4");
FileOutputStream fos = new FileOutputStream("基礎語法\\test.mp4");
int by;
while ((by=fis.read())!= -1){
fos.write(by);
}
fis.close();
fos.close();
}
//位元組流一次讀寫一個位元組陣列:共耗時:61毫秒
public static void method2()throws IOException {
FileInputStream fis = new FileInputStream("E:\\2021Study\\test.mp4");
FileOutputStream fos = new FileOutputStream("基礎語法\\test.mp4");
byte[] bytes = new byte[1024];
int len;
while ((len=fis.read(bytes))!= -1){
fos.write(bytes,0,len);
}
fis.close();
fos.close();
}
//位元組緩衝流一次一位元組:共耗時:210毫秒
public static void method3()throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("E:\\2021Study\\test.mp4"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("基礎語法\\test.mp4"));
int by;
while ((by=bis.read())!=-1){
bos.write(by);
}
bos.close();
bis.close();
}
//位元組緩衝流一次一位元組陣列:共耗時:714毫秒
public static void method4()throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("E:\\2021Study\\test.mp4"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("基礎語法\\test.mp4"));
byte[] bytes = new byte[1024];
int len;
while ((len=bis.read())!= -1){
bos.write(bytes,0,len);
}
bos.close();
bis.close();
}
}