目錄
二、緩衝流:BufferedInputStream & BufferedOutputStream
三、對象流:ObjectInputStream & ObjectOutputStream
五、轉換流 InputStreamReader & OutputStreamWriter
一、IO 流的結構體系
- 字符流:用於操作文本文件 .txt .java
- 字節流:用於操作非文本文件 .avi .mp3 .jpg
二、緩衝流: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 對象