IO流筆記常用位元組流字元流的使用
阿新 • • 發佈:2021-08-23
IO:Input Output
流的概念和作用
流是一組順序的,有起點和終點的位元組集合,是對資料傳輸的總成或抽象,即資料在兩裝置間傳輸稱為流,流的本質是資料傳輸,根據資料傳輸特性將流抽象為各種類,方便更直觀的進行資料操作。
IO流的分類
- 根據 處理的資料型別不同可以分為:字元流和位元組流
- 根據資料的流向可以分為:輸入流和輸出流
什麼情況下使用字元流:如果讀寫的都是字元資料,這時候我們就使用字元流。文字檔案。
什麼情況使用位元組流: 讀取到資料不需要經過編碼或者解碼的情況情況下這時候使用位元組流。比如:圖片資料、視訊資料字元流 = 位元組流 + 編碼(解碼)
字元流和位元組流字元流的由來: 因為資料編碼的不同,而有了對字元進行高效操作的流物件。本質其實就是基於位元組流讀取時,去查了指定的碼錶。 位元組流和字元流的區別:- 讀寫單位不同:位元組流以位元組(8bit)為單位,字元流以字元為單位,根據碼錶對映字元,一次可能讀多個位元組。- 處理物件不同:位元組流能處理所有型別的資料(如圖片、avi等),而字元流只能處理字元型別的資料。
結論:只要是處理純文字資料,就優先考慮使用字元流。 除此之外都使用位元組流。
FileRead 和 FileWriter
@Test public void testFileReader(){ FileReader fileReader = null; try { // fileReader = new FileReader("IO.txt");// 查詢當前檔案目錄下的 fileReader = new FileReader("E:\\IDEAWorkspace\\JavaStudy\\JavaSE\\src\\IOTest\\IO.txt"); // 等於上面那個 // int ch = fileReader.read(); // 讀取第一個字元 // System.out.println(ch); // System.out.println((char)ch);*/ // // 一個一個字元的讀 // int ch = -1; // while ((ch = fileReader.read()) != -1){ // System.out.println(ch); // System.out.println((char)ch); // } char[] buffer = new char[5]; int length = -1; // 將讀取的資料存放在buffer中,返回讀取的長度,當取完之後返回的是-1 while ((length = fileReader.read(buffer)) != -1){ System.out.println(Arrays.toString(buffer)); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (fileReader != null){ try { fileReader.close(); } catch (IOException e) { e.printStackTrace(); } } } }
InputStream 和 OutputStream
@Test public void testInputOutputStream(){ FileInputStream fileInputStream = null; FileOutputStream fileOutputStream = null; try { fileInputStream = new FileInputStream("E:\\IDEAWorkspace\\JavaStudy\\JavaSE\\src\\IOTest\\bd.png"); // fileInputStream = new FileInputStream("IOTest\\bd.png"); fileOutputStream = new FileOutputStream("E:\\IDEAWorkspace\\JavaStudy\\JavaSE\\src\\IOTest\\bd_back.png"); byte[] buffer = new byte[1024]; int length = -1; while((length = fileInputStream.read()) != -1) { fileOutputStream.write(buffer, 0, length); } System.out.println("success"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (fileInputStream != null){ try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (fileOutputStream != null){ try { fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } }
ObjectInputStram 和 ObjectOutputStream
import day12.Student;
import org.junit.Test;
import java.io.*;
public class ObjectIODemo {
@Test
public void testObjectOutputStream(){
Student student = new Student(1,"zhangsan",18,"男");
ObjectOutput objectOutput = null; // 負責物件轉換
FileOutputStream fileOutputStream = null;// 真正寫檔案的還是fileOutputStream
try {
fileOutputStream = new FileOutputStream("student");
objectOutput = new ObjectOutputStream(fileOutputStream);
objectOutput.writeObject(student);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (objectOutput != null){
try {
objectOutput.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileOutputStream != null){
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void testObjectInputStream(){
FileInputStream fileInputStream = null;
ObjectInput objectInput = null;
try {
fileInputStream = new FileInputStream("student");
objectInput = new ObjectInputStream(fileInputStream);
Student student = (Student) objectInput.readObject();
System.out.println(student);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
if (fileInputStream != null){
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (objectInput != null){
try {
objectInput.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}