目錄
一、IO 流的結構體系
二、緩沖流:BufferedInputStream & BufferedOutputStream
三、對象流:ObjectInputStream & ObjectOutputStream
四、控制台IO
五、轉換流 InputStreamReader & OutputStreamWriter
六、java.io.File 類 :
一、IO 流的結構體系
- 字元流:用于操作文本檔案 .txt .java
- 位元組流:用于操作非文本檔案 .avi .mp3 .jpg
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiAzNfRHLGZkRGZkRfJ3bs92YsYTMfVmepNHLwkFROp3ZE9UMNpHW4Z0MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zROBlL4IDN4MTOxATM3ETNwAjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
二、緩沖流:BufferedInputStream & BufferedOutputStream
處理流的一種,包裝現有節點流,用于提高效率,内置緩沖區
//非文本檔案的複制
@Test
public void test1(){
//3. 建立 BufferedInputStream 的執行個體,包裝對應的節點流,用于提高效率
BufferedInputStream bis = null;
//4. 建立 BufferedOutputStream 的執行個體,包裝對應的節點流,用于提高效率
BufferedOutputStream bos = null;
try {
//1. 建立 FileInputStream 的執行個體,同時打開指定檔案
FileInputStream fis = new FileInputStream("C:\\Users\\LI/Desktop/1.jpg");
//2. 建立 FileOutputStream 的執行個體,同時打開指定檔案
FileOutputStream fos = new FileOutputStream("C:\\Users\\LI\\Desktop\\2.jpg");
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
//5. 通過 read(byte[] b)讀取指定檔案的内容
byte[] b = new byte[1024];
int len = 0; //接收實際讀取的位元組總數
while((len = bis.read(b)) != -1){
//6. 通過 write(byte[] b, int off, int len) 将讀取的内容寫到目标地點去
bos.write(b, 0, len);
//bos.flush();//強制清空換緩沖區,但是會影響效率
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//7. 關閉流
if(bos != null){
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(bis != null){
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
三、對象流:ObjectInputStream & ObjectOutputStream
-
對象的序列化:将記憶體中的對象永久的以二進制形式儲存到磁盤中。
①建立節點流的對象
②(可選)使用緩沖流包裝現有節點流,用于提高效率
③建立對象流包裝現有緩沖流,用于序列化
④通過 writeXxx() 方法 Xxx: 對應的資料類型,完成對象的序列化操作
⑤關閉流
⑥需要序列化對象的類必須實作 java.io.Serializable 接口(該類其他屬性也必須實作該接口)
⑦需要提供一個序列号,保證如何序列化就如何反序列化
public static final long serialVersionUID = 9239893899894L;
- 對象的反序列化:将磁盤中的資料進行讀取
public class ObjectStreamTest {
// 反序列化
@Test
public void test1() {
ObjectInputStream ois = null;
try {
FileInputStream fis = new FileInputStream("./person.dat");
BufferedInputStream bis = new BufferedInputStream(fis);
ois = new ObjectInputStream(bis);
List<Person> list = (List<Person>) ois.readObject();
Iterator<Person> it = list.iterator();
while(it.hasNext()){
Person p = it.next();
System.out.println(p);
}
} catch (ClassNotFoundException | IOException e1) {
e1.printStackTrace();
} finally {
if (ois != null) {
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// 序列化
public static void main(String[] args) {
Person p1 = new Person("張三", 18, 98.88);
Person p2 = new Person("李四", 20, 100.22);
Person p3 = new Person("王五", 38, 300.33);
List<Person> list = new ArrayList<>();
list.add(p1);
list.add(p2);
list.add(p3);
ObjectOutputStream oos = null;
try {
FileOutputStream fos = new FileOutputStream("./person.dat");
BufferedOutputStream bos = new BufferedOutputStream(fos);
oos = new ObjectOutputStream(bos);
oos.writeObject(list);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (oos != null) {
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
四、控制台IO
【了解】
- System.in : “标準”的輸入流
- System.out : "标準"的輸出流(System.setOut() 可以改變 println 方法的預設輸出位置)
- System.err :"标準"的錯誤輸出流
列印流:PrintStream & PrintWriter
五、轉換流 InputStreamReader & OutputStreamWriter
編碼:字元串 -> 位元組數組 OutputStreamWriter
//編碼
@Test
public void test2(){
String str = "好好學習天天向上!";
BufferedWriter bw = null;
try {
//注意:應該使用字元流操作文本檔案,暫時為了示範轉換流的用法,使用位元組流操作文本檔案
FileOutputStream fos = new FileOutputStream("hello.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK");
bw = new BufferedWriter(osw);
bw.write(str);
} catch (IOException e) {
e.printStackTrace();
} finally {
if(bw != null){
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
解碼:位元組數組 -> 字元串
//解碼
@Test
public void test3(){
BufferedReader br = null;
try {
FileInputStream fis = new FileInputStream("hello.txt");
InputStreamReader isr = new InputStreamReader(fis, "ISO8859-1");
br = new BufferedReader(isr);
String str = br.readLine();
System.out.println(str);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
六、java.io.File 類 :
描述檔案或目錄,用于操作檔案或目錄建立、删除、重命名等基本操作
若需要操作檔案的内容,File 對象将無能為力,需要使用 IO 流
通常 File 對象與 IO 配合使用,将 File對象作為參數傳遞給 IO 流的構造器。
通路檔案名:
- getName() :擷取檔案名
- getPath():擷取相對路徑
- File getAbsoluteFile() :擷取絕對路徑的 File 對象
- String getAbsolutePath() :擷取絕對路徑對應的字元串
- getParent():擷取父路徑
- renameTo(File newName) :重命名
- file1.renameTo(file2) : file1 必須存在,file2 必須不存在
檔案檢測
- exists() : 判斷檔案或目錄是否存在
- canWrite() : 判斷是否可寫
- canRead() :是否可讀
- isFile() :判斷是不是一個檔案
- isDirectory() :判斷是不是一個目錄
擷取正常檔案資訊
- lastModified() :擷取最後修改時間對應的毫秒值
- length():擷取檔案的大小,但是注意目錄沒有大小
檔案操作相關
- createNewFile() :建立一個“檔案”
- delete() :删除檔案,若需要删除目錄必須将目錄中所有的内容全部删掉再删目前目錄
目錄操作相關
- mkDir() :建立一個目錄,如:e:/io/abc 若io包不存在則abc不能建立,若io存在則abc可以建立成功
- mkDirs() :建立目錄,如:e:/io/abc 若io包不存在則自動建立io包
- String[] list():擷取指定目錄下所有的檔案清單字元串名稱
- File[] listFiles():擷取指定目錄下所有檔案和目錄的 File 對象