JAVA知識點 I/O流框架簡要筆記
I/O 框架
流的概念
記憶體與存儲設備之間傳輸數據的通道
流的分類
按方向【重點】
- 輸入流:將<存儲設備>中的內容讀到<記憶體>中
- 輸出流:將<記憶體>中的內容寫到<存儲設備>中
按單位
- 位元組流:以位元組為單位,可以讀寫所有數據
- 字元流:以字元為單位,只能讀寫文本數據
按功能
- 節點流:具有實際傳輸數據的讀寫功能
- 過濾流:在節點流的基礎之上增強功能
位元組流
位元組流的父類(抽象類)
//InputStream 位元組輸入流
public int read(){}
public int read(byte[] b){}
public int read(byte[] b, int off, int len){}
// OutputStream 位元組輸出流
public void write(int n){}
public void write(byte[] b){}
public void write(byte[] b, int off, int len){}
文件位元組流
文件輸入流
psvm(String[] args) throws Exception{
// 1 創建FileInputStream 並指定文件路徑
FileInputStream fis = new FileInputStream("d:\\abc.txt");
// 2 讀取文件
// fis.read();
// 2.1單位元組讀取
int data = 0;
while((data = fis.read()) != -1){
sout((char)data);
}
// 2.2 一次讀取多個位元組
byte[] buf = new byte[3]; // 大小為3的快取區
int count = fis.read(buf); // 一次讀3個
sout(new String(buf));
sout(count);
int count2 = fis.read(buf); // 再讀3個
sout(new String(buf));
sout(count2);
// 上述優化後
int count = 0;
while((count = fis.read(buf)) != -1){
sout(new String(buf, 0, count));
}
// 3 關閉
fis.close();
}
文件輸出流
psvm(String[] args) throws Exception{
// 1 創建文件位元組輸出流
FileOutputStream fos = new FileOutputStream("路徑", true);// true表示不覆蓋 接著寫
// 2 寫入文件
fos.write(97);
fos.write('a');
// String string = "hello world";
fos.write(string.getByte());
// 3 關閉
fos.close();
}
圖片複製案例
// 1 創建流
// 1.1 文件位元組輸入流
FileInputStream fis = new FileInputStream("路徑");
// 1.2 文件位元組輸出流
FileInputStream fos = new FileOutpuStream("路徑");
// 2 邊讀邊寫
byte[] buf = new byte[1024];
int count = 0;
while((count = fis.read(buf)) != -1){
fos.write(buf, 0, count);
}
// 3 關閉
fis.close();
fos.close();
位元組緩衝流
緩衝流:BufferedInputStream/ BufferedOutputStream
- 提高IO效率,減少訪問磁碟次數
- 數據存儲在緩衝區中,flush是將緩衝區的內容寫入文件中,也可以直接close
// 使用位元組緩衝流 讀取 文件
psvm(String[] args) throws Exception{
// 1 創建BufferedInputStream
FileInputStream fis = new FileInputStream("路徑");
BufferedInputStream bis = new BufferedInputStream(fis);
// 2 讀取
int data = 0;
while((data = bis.read()) != -1){
sout((char)data);
}
// 用自己創建的緩衝流
byte[] buf = new byte[1024];
int count = 0;
while((count = bis.read(buf)) != -1){
sout(new String(buf, 0, count));
}
// 3 關閉
bis.close();
}
// 使用位元組緩衝流 寫入 文件
psvm(String[] args) throws Exception{
// 1 創建BufferedInputStream
FileOutputStream fos = new FileOutputStream("路徑");
BufferedOutputStream bis = new BufferedOutputStream(fos);
// 2 寫入文件
for(int i = 0; i < 10; i ++){
bos.write("hello".getBytes());// 寫入8k緩衝區
bos.flush(); // 刷新到硬碟
}
// 3 關閉
bos.close();
}
對象流
ObjectOutputStream / ObjectInputStream
- 增強了緩衝區功能
- 增強了讀寫8種基本數據類型和字元串的功能
- 增強了讀寫對象的功能
readObject()
從流中讀取一個對象writeObject(Object obj)
向流中寫入一個對象
使用流傳輸對象的過程稱為序列化、反序列化
序列化與反序列化
序列化
// 使用objectoutputStream實現序列化
psvm(String[] args){
// 1. 創建對象流
FileOutputStream fos = new FileOutputStream("d:\\st.bin");
ObjectOutputSream oos = new objectOutputSream(fos);
// 2. 序列化(寫入操作)
Student zhangsan = new Student("zs", 20);
oos.WriteObject(zhangsan);
// 3. 關閉
oos.close();
sout("序列化完畢");
}
反序列化
// 使用ObjectInputSteam實現反序列化(讀取重構對象)
psvm(String[] args){
// 1. 創建對象流
FileInputStream fis = new FileInputStream("d:\\stu.bin");
ObjectInputStream ois = new ObjectInputStream(fis);
// 2. 讀取文件(反序列化)
Student s = (Student)ois.readObject();
// 3. 關閉
ois.close();
sout("執行完畢");
sout(s.toString());
}
注意事項
- 某個類要想序列化必須實現Serializable介面
- 序列化類中對象屬性要求實現Serializable介面
- 序列化版本號ID,保證序列化的類和反序列化的類是同一個類
- 使用transient修飾屬性,這個屬性就不能序列化
- 靜態屬性不能序列化
- 序列化多個對象,可以藉助集合來實現
編碼方式
UTF-8… 不贅述
字元流
// 傳統位元組流讀取
psvm(String[] args){
// 1. 創建FileInputStream對象
FileInputSteam fis = new FileInputStream("路徑");
// 2. 讀取
int data = 0;
while((data = fis.read()) != -1){
sout((char)data);
}
// 3. 關閉
fis.close();
}
字元流的父類(抽象類)
reader
字元輸入流
public int read(){}
public int read(char[] c){}
public int read(char[] b, int off, int len){}
Writer
字元輸出流
public void write(int n){}
public void write(String str){}
public void write(char[] c){}
// 1. 創建FileReader 文件字元輸入流
FileReader fr = new FileReader("..");
// 2. 讀取
// 2.1 單個字元讀取
int data = 0;
while((data = fr.read()) != -1){
sout((char)data);// 讀取一個字元
}
char[] buf = new char[2];// 字元緩衝區讀取
int count = 0;
while((count = fr.read(buf) != -1)){
sout(new String(buf, 0, count));
}
// 3. 關閉
fr.close();
// 1. 創建FileWriter對象
FileWriter fw = new FileWriter("..");
// 2. 寫入
for(int i = 0; i < 10; i ++){
fw.write("寫入的內容");
fw.flush();
}
// 3. 關閉
fw.close();
sout("執行完畢");
(案例)使用上述內容進行文本文件複製
不能複製圖片或二進位文件,使用位元組流可以複製任意文件
psvm(String[] args) throws Exception{
// 1. 創建
FileReader fr = new FileReader("...");
FileWriter fw = new FileWriter("...");
// 2. 讀寫
int data = 0;
while((data = fr.read()) != -1){
fw.write(data);
fw.flush();
}
// 3. 關閉
fw.close();
fr.close();
}
字元緩衝流
BufferedReader / BufferedWriter
高效讀寫、支援輸入換行符、可一次寫一行讀一行
psvm(String[] args) throws Exception{
// 創建緩衝流
FileReader fr = new FileReader("..");
BufferedReader br = new BufferedReader(fr);
// 讀取
// 1. 第一種方式
char[] buf = new char[1024];
int count = 0;
while((count = br.read(buf)) != -1){
sout(new String(buf, 0, count));
}
// 2. 第二種方式 一行一行讀取
String line = null;
while((line = br.readLine()) != null){
sout(line);
}
// 關閉
br.close();
}
psvm(String[] args){
// 1. 創建BufferedWriter對象
FileWriter fw = new FileWriter("..");
BufferedWriter bw = new BufferedWriter(fw);
// 2. 寫入
for(int i = 0; i < 10; i ++){
bw.write("寫入的內容");
vw.newLine(); // 寫入一個換行符
bw.flush();
}
// 3. 關閉
bw.close(); // 此時會自動關閉fw
}
PrintWriter
封裝了print() / println()
方法 支援寫入後換行
支援數據原樣列印
psvm(String[] args){
// 1 創建列印流
PrintWriter pw = new PrintWriter("..");
// 2 列印
pw.println(12);
pw.println(true);
pw.println(3.14);
pw.println('a');
// 3 關閉
pw.close();
}
轉換流
橋轉換流 InputStreamReader / OutputStreamWriter
可將位元組流轉換為字元流
可設置字元的編碼方式
psvm(String[] args) throws Exception{
// 1 創建InputStreamReader對象
FileInputStream fis = new FisInputStream("..");
InputStreamReader isr = new InputStreamReader(fis, "utf-8");
// 2 讀取文件
int data = 0;
while((data = isr.read()) != -1){
sout((char)data);
}
// 3 關閉
isr.close();
}
psvm(String[] args) throws Exception{
// 1 創建OutputStreamReader對象
FileOutputStream fos = new FisOutputStream("..");
OutputStreamWRITER osw = new OutputStreamReader(fos, "utf-8");
// 2 寫入
for(int i = 0; i < 10; i ++){
osw.write("寫入內容");
osw.flush();
}
// 3 關閉
osw.close();
}
File類
概念:代表物理盤符中的一個文件或者文件夾
/*
File類的使用
1. 分隔符
2. 文件操作
3. 文件夾操作
*/
public class Demo{
psvm(String[] args){
separator();
}
// 1. 分隔符
public static void separator(){
sout("路徑分隔符" + File.pathSeparator);
sout("名稱分隔符" + File.separator);
}
// 2. 文件操作
public static void fileOpen(){
// 1. 創建文件
if(!file.exists()){ // 是否存在
File file = new File("...");
boolean b = file.creatNewFile();
}
// 2. 刪除文件
// 2.1 直接刪除
file.delete(); // 成功true
// 2.2 使用jvm退出時刪除
file.deleteOnExit();
// 3. 獲取文件資訊
sout("獲取絕對路徑" + file.getAbsolutePaht());
sout("獲取路徑" + file.getPath());
sout("獲取文件名稱" + file.getName());
sout("獲取夫目錄" + file.getParent());
sout("獲取文件長度" + file.length());
sout("文件創建時間" + new Date(file.lashModified()).toLocalString());
// 4. 判斷
sout("是否可寫" + file.canWrite());
sout("是否是文件" + file.isFile());
sout("是否隱藏" + file.isHidden());
}
// 文件夾操作
public static void directoryOpe() throws Exception{
// 1. 創建文件夾
File dir = new File("...");
sout(dir.toString());
if(!dir.exists()){
//dir.mkdir(); // 只能創建單級目錄
dir.mkdirs(); // 創建多級目錄
}
// 2. 刪除文件夾
// 2.1 直接刪除
dir.delete(); // 只能刪除最底層空目錄
// 2.2 使用jvm刪除
dir.deleteOnExit();
// 3. 獲取文件夾資訊
sout("獲取絕對路徑" + dir.getAbsolutePaht());
sout("獲取路徑" + dir.getPath());
sout("獲取文件名稱" + dir.getName());
sout("獲取夫目錄" + dir.getParent());
sout("獲取文件長度" + dir.length());
sout("文件夾創建時間" + new Date(dir.lashModified()).toLocalString());
// 4. 判斷
sout("是否是文件夾" + dir.isFile());
sout("是否隱藏" + dir.isHidden());
// 5. 遍歷文件夾
File dir2 = new File("...");
String[] files = dir2.list();
for(String string : files){
sout(string);
}
// FileFilter介面的使用
File[] files2 = dir2.listFiles(new FileFilter(){
@Override
public boolean accept(File pathname){
if(pathname.getName().endsWith(".jpg")){
return true;
}
return false;
}
});
for(File file : files2){
sout(file.getName());
}
}
}
遞歸遍歷文件夾
psvm(String[] args){
listDir(new File("d:\\myfiles"));
}
public static void listDir(File dir){
File[] files = dir.listFiles();
sout(dir.getAbsolutePath());
if(files != null && files.length > 0){
for(File file : files){
if(file.isDirectory()){
listDir(file); // 遞歸
}else {
sout(file.getAbsolutePath());
}
}
}
}
遞歸刪除文件夾
public static void deleteDir(File dir){
File[] files = dir.listFiles();
if(files != null && files.length > 0){
for(File file : files){
if(file.idDirectory()){
deleteDir(file); // 遞歸
}else{
// 刪除文件
sout(file.getAbsolutePath() + "刪除" + file.delete());
}
}
}
}