目錄
- File類
- File類的靜態成員變數
- 絕對路徑和相對路徑
- File類的構造方法
- File類獲取功能的方法
- File類判斷功能的方法
- File類創建和洗掉功能的方法
- File類的目錄的遍歷功能
- IO流
- 位元組流
- 位元組輸出流【OutputStream】
- 位元組輸出流的續寫和換行
- 方法摘要
- 位元組輸入流FileInputStream類
- 構造方法摘要
- 位元組輸入流一次讀取多個位元組
- 位元組流完成復制檔案操作
- 位元組輸出流【OutputStream】
- 字符流Reader
- 3個共性成員方法
- 構造方法
- 字符流讀取硬碟中資料
- 字符輸入流FileReader
- 使用步驟
- 字符輸出流Writer
- 檔案字符輸出流FileWriter
- 構造方法摘要
- 字符輸出流Writer的使用步驟
- 寫入單個字符
- 寫入多個字符【字符輸出流寫資料得其它方法】
- 字符輸出流的續寫和換行
- IO例外的處理
- 位元組流
File類
java.lang.Object
java.io.File
java把電腦中的檔案,檔案夾(目錄)封裝成了一個File類,我們可以使用File類對檔案和檔案夾進行操作
可以使用File類的方法去:
? 創建一個檔案/檔案夾
? 洗掉檔案/檔案夾
? 獲取檔案/檔案夾
? 判斷檔案/檔案夾是否存在
? 對檔案夾進行遍歷
? 獲取檔案的大小
File類是一個與系統無關的類,任何作業系統都可以使用這個類中的方法
重點:記住3個單詞
- file:檔案
- directory:檔案夾/目錄
- path:路徑
File類的靜態成員變數
| 欄位摘要 | |
|---|---|
static String |
pathSeparator 與系統有關的路徑分隔符,為了方便,它被表示為一個字串, |
static char |
pathSeparatorChar 與系統有關的路徑分隔符, |
static String |
separator 與系統有關的默認名稱分隔符,為了方便,它被表示為一個字串, |
static char |
separatorChar 與系統有關的默認名稱分隔符, |
public class DemosFile {
public static void main(String[] args) {
String pathSeparator = File.pathSeparator;
System.out.println(pathSeparator);//檔案名稱分隔符:windows:反斜杠 Linux:正斜杠/
char pathSeparatorChar = File.pathSeparatorChar;
System.out.println(pathSeparatorChar);//檔案名稱分隔符:windows:反斜杠 Linux:正斜杠/
String separator = File.separator;
System.out.println(separator);//路徑分隔符 windows:分號 Linux:冒號
char separatorChar = File.separatorChar;
System.out.println(separatorChar);//路徑分隔符 windows:分號 Linux:冒號
/*操作路徑不能寫死了
* 例如:
* C:\Users\98398\Desktop\我的MarkDown檔案 Windows
* C:/Users/98398/Desktop/我的MarkDown檔案 Linnux
*正確寫法:
* "C:"+File.separator+"User"+File.separator+"a"+File.separator+"a.png"
*
* */
}
}

絕對路徑和相對路徑
路徑:
? 絕對路徑:是一個完整的路徑
? 以盤符開始的路徑
? 相對路徑:是一個簡化的路徑
? 相對指的是相對于當前專案的根目錄
? 如果使用當前專案的根目錄,路徑可以簡化書寫(可以省略根目錄不寫)
? 注意:
1. 路徑不區分大小寫
2. 路徑中檔案名稱分隔符Windows使用反斜杠,反斜杠是轉義字符,兩個反斜杠代表一個普通的反斜杠
File類的構造方法
import java.io.File;
public class DemosFile2 {
/**
* File類的構造方法
*/
public static void main(String[] args) {
show1();
show2("C:\\","c.txt ");
show3();
}
private static void show3() {
File parent = new File("C:\\");
File file3 = new File(parent, "d.java");
System.out.println(file3);
}
private static void show2(String a,String b) {
File file2 = new File(a, b);
System.out.println(file2);
}
private static void show1() {
File file1 = new File("C:\\Users\\98398\\Desktop\\我的MarkDown檔案\\a.txt");
System.out.println(file1);
File file2 = new File("C:\\Users\\98398\\Desktop\\我的MarkDown檔案");
System.out.println(file2);
File file3 = new File("b.txt");
System.out.println(file3);
}
}
File類獲取功能的方法
方法摘要
boolean canExecute()
測驗應用程式是否可以執行此抽象路徑名表示的檔案,
boolean canRead()
測驗應用程式是否可以讀取此抽象路徑名表示的檔案,
boolean canWrite()
測驗應用程式是否可以修改此抽象路徑名表示的檔案,
int compareTo(File pathname)
按字母順序比較兩個抽象路徑名,
boolean createNewFile()
當且僅當不存在具有此抽象路徑名指定名稱的檔案時,不可分地創建一個新的空檔案,
static File createTempFile(String prefix, String suffix)
在默認臨時檔案目錄中創建一個空檔案,使用給定前綴和后綴生成其名稱,
static File createTempFile(String prefix, String suffix, File directory)
在指定目錄中創建一個新的空檔案,使用給定的前綴和后綴字串生成其名稱,
boolean delete()
洗掉此抽象路徑名表示的檔案或目錄,
void deleteOnExit()
在虛擬機終止時,請求洗掉此抽象路徑名表示的檔案或目錄,
boolean equals(Object obj)
測驗此抽象路徑名與給定物件是否相等,
boolean exists()
測驗此抽象路徑名表示的檔案或目錄是否存在,
File getAbsoluteFile()
回傳此抽象路徑名的絕對路徑名形式,
String getAbsolutePath()
回傳此抽象路徑名的絕對路徑名字串,
File getCanonicalFile()
回傳此抽象路徑名的規范形式,
String getCanonicalPath()
回傳此抽象路徑名的規范路徑名字串,
long getFreeSpace()
回傳此抽象路徑名指定的磁區中未分配的位元組數,
String getName()
回傳由此抽象路徑名表示的檔案或目錄的名稱,
String getParent()
回傳此抽象路徑名父目錄的路徑名字串;如果此路徑名沒有指定父目錄,則回傳 null,
File getParentFile()
回傳此抽象路徑名父目錄的抽象路徑名;如果此路徑名沒有指定父目錄,則回傳 null,
String getPath()
將此抽象路徑名轉換為一個路徑名字串,
long getTotalSpace()
回傳此抽象路徑名指定的磁區大小,
long getUsableSpace()
回傳此抽象路徑名指定的磁區上可用于此虛擬機的位元組數,
int hashCode()
計算此抽象路徑名的哈希碼,
boolean isAbsolute()
測驗此抽象路徑名是否為絕對路徑名,
boolean isDirectory()
測驗此抽象路徑名表示的檔案是否是一個目錄,
boolean isFile()
測驗此抽象路徑名表示的檔案是否是一個標準檔案,
boolean isHidden()
測驗此抽象路徑名指定的檔案是否是一個隱藏檔案,
long lastModified()
回傳此抽象路徑名表示的檔案最后一次被修改的時間,
long length()
回傳由此抽象路徑名表示的檔案的長度,
String[] list()
回傳一個字串陣列,這些字串指定此抽象路徑名表示的目錄中的檔案和目錄,
String[] list(FilenameFilter filter)
回傳一個字串陣列,這些字串指定此抽象路徑名表示的目錄中滿足指定過濾器的檔案和目錄,
File[] listFiles()
回傳一個抽象路徑名陣列,這些路徑名表示此抽象路徑名表示的目錄中的檔案,
File[] listFiles(FileFilter filter)
回傳抽象路徑名陣列,這些路徑名表示此抽象路徑名表示的目錄中滿足指定過濾器的檔案和目錄,
File[] listFiles(FilenameFilter filter)
回傳抽象路徑名陣列,這些路徑名表示此抽象路徑名表示的目錄中滿足指定過濾器的檔案和目錄,
static File[] listRoots()
列出可用的檔案系統根,
boolean mkdir()
創建此抽象路徑名指定的目錄,
boolean mkdirs()
創建此抽象路徑名指定的目錄,包括所有必需但不存在的父目錄,
boolean renameTo(File dest)
重新命名此抽象路徑名表示的檔案,
boolean setExecutable(boolean executable)
設定此抽象路徑名所有者執行權限的一個便捷方法,
boolean setExecutable(boolean executable, boolean ownerOnly)
設定此抽象路徑名的所有者或所有用戶的執行權限,
boolean setLastModified(long time)
設定此抽象路徑名指定的檔案或目錄的最后一次修改時間,
boolean setReadable(boolean readable)
設定此抽象路徑名所有者讀權限的一個便捷方法,
boolean setReadable(boolean readable, boolean ownerOnly)
設定此抽象路徑名的所有者或所有用戶的讀權限,
boolean setReadOnly()
標記此抽象路徑名指定的檔案或目錄,從而只能對其進行讀操作,
boolean setWritable(boolean writable)
設定此抽象路徑名所有者寫權限的一個便捷方法,
boolean setWritable(boolean writable, boolean ownerOnly)
設定此抽象路徑名的所有者或所有用戶的寫權限,
String toString()
回傳此抽象路徑名的路徑名字串,
URI toURI()
構造一個表示此抽象路徑名的 file: URI,
URL toURL()
已過時, 此方法不會自動轉義 URL 中的非法字符,建議新的代碼使用以下方式將抽象路徑名轉換為 URL:首先通過 toURI 方法將其轉換為 URI,然后通過 URI.toURL 方法將 URI 裝換為 URL,
import java.io.File;
public class DemosFile3 {
/**
* String getAbsolutePath() 回傳此抽象路徑名的絕對路徑名字串,
* String getName() 回傳由此抽象路徑名表示的檔案或目錄的名稱,
* String getPath() 將此抽象路徑名轉換為一個路徑名字串,
* long length() 回傳由此抽象路徑名表示的檔案的長度,
*/
public static void main(String[] args) {
show1();
System.out.println("———————————————————————————————————");
show2();
System.out.println("———————————————————————————————————");
show3();
System.out.println("———————————————————————————————————");
show4();
}
/*long length() 回傳由此抽象路徑名表示的檔案的長度,
* 此方法獲取的是構造方法中指定的檔案的大小,以位元組為單位
* 注意:
* 1.只能獲取檔案的大小,無法獲取檔案夾的大小
* (因為計算機中檔案夾的大小屬性是沒有值的
* 我們可以通過屬性去查看檔案夾的大小,但是看到的大小值其實是該檔案夾里所有檔案的大小總和)
*
* 2.如果構造方法中給出的路徑不存在,那么length()方法回傳的就是0*/
private static void show4() {
File file1 = new File("C:\\Users\\98398\\Desktop\\我的截圖\\hahaahahah.jpg");//真實存在的檔案(絕對路徑)
long length1 = file1.length();//獲取到的是檔案大小
System.out.println(length1);
File file2 = new File("pom.xml");//真實存在的檔案(相對路徑)
long length2 = file2.length();//獲取到的是檔案大小
System.out.println(length2);
File file3 = new File("wuwuwuwuwu.jpg");//不存在的檔案
long length3 = file3.length();//檔案不存在,獲取到的回傳值是0
System.out.println(length3);
File file4 = new File("E:\\IdeaProjects\\spring-study\\poker");//真實存在的檔案夾
long length4 = file4.length();//檔案夾存在,獲取到的回傳值是0
System.out.println(length4);
File file5 = new File("E:\\IdeaProjects\\spring-study\\poker");//不存在的檔案夾
long length5 = file5.length();//檔案夾不存在,獲取到的回傳值是0
System.out.println(length5);
}
/*String getName() 回傳由此抽象路徑名表示的檔案或目錄的名稱,
* 此方法獲取的是構造方法中傳遞的路徑
* getName()方法回傳的是路徑的結尾部分*/
private static void show3() {
File file1 = new File("E:\\IdeaProjects\\spring-study\\a.txt");//結尾是檔案名(絕對路徑)
String name1 = file1.getName();//獲取到的是檔案名
System.out.println(name1);
File file2 = new File("E:\\IdeaProjects\\spring-study");//結尾是檔案夾名(絕對路徑)
String name2 = file2.getName();//獲取到的是檔案夾名
System.out.println(name2);
File file3 = new File("a.txt");//結尾是檔案夾名(相對路徑)
String name3 = file3.getName();//獲取到的是檔案夾名
System.out.println(name3);
}
/*String getPath() 將此抽象路徑名轉換為一個路徑名字串,
* 此方法獲取的是構造方法中傳遞的路徑
* 路徑是什么,getPath()方法就回傳什么
*
* toString呼叫的就是getPath()方法*/
private static void show2() {
File file1 = new File("E:\\IdeaProjects\\spring-study\\a.txt");//絕對路徑
String path1 = file1.getPath();//獲取到的就是絕對路徑
System.out.println(path1);
File file2 = new File("a.txt");//相對路徑
String path2 = file2.getPath();//獲取到的就是相對路徑
System.out.println(path2);
System.out.println(file1.toString());//toString呼叫的就是getPath()方法
System.out.println(file2.toString());//toString呼叫的就是getPath()方法
}
/*String getAbsolutePath() 回傳此抽象路徑名的絕對路徑名字串,
* 此方法獲取的是構造方法中傳遞的路徑
* 無論路徑是絕對路徑還是相對路徑,getAbsolutePath()方法回傳的都是 絕對路徑 */
private static void show1() {
File file1 = new File("E:\\IdeaProjects\\spring-study\\a.txt");//絕對路徑
String absolutePath1 = file1.getAbsolutePath();//獲取到的是絕對路徑
System.out.println(absolutePath1);
File file2 = new File("a.txt");//相對路徑
String absolutePath2 = file2.getAbsolutePath();//獲取到的還是絕對路徑
System.out.println(absolutePath2);
}
}
運行結果:

File類判斷功能的方法
boolean isDirectory()
測驗此抽象路徑名表示的檔案是否是一個目錄,
boolean isFile()
測驗此抽象路徑名表示的檔案是否是一個標準檔案,
boolean exists()
測驗此抽象路徑名表示的檔案或目錄是否存在,
import java.io.File;
public class DemosFile4 {
/*
File類判斷功能的方法
boolean exists() 測驗此抽象路徑名表示的檔案或目錄是否存在,
boolean isDirectory() 測驗此抽象路徑名表示的檔案是否是一個目錄,
boolean isFile() 測驗此抽象路徑名表示的檔案是否是一個標準檔案,
*/
public static void main(String[] args) {
show1();
System.out.println("———————————————————————————————————");
show2();
}
private static void show3() {
}
/**
* boolean isDirectory() 測驗此抽象路徑名表示的檔案是否是一個目錄,
* 用于判斷構造方法中路徑是否以檔案夾結尾
* 是:true
* 否:false
*
*boolean isFile() 測驗此抽象路徑名表示的檔案是否是一個標準檔案,
* 用于判斷構造方法中路徑是否以檔案結尾
* 是:true
* 否:false
*
* 注意:
* 電腦硬碟中只有檔案或檔案夾,所以兩個方法是互斥的
* 這兩個方法的使用前提是,路徑必須是真實存在的,否則都回傳false
*/
private static void show2() {
File file1 = new File("E:\\IdeaProjects\\spring-studyxxxx");//不存在的絕對路徑
System.out.println(file1.isDirectory());//false
System.out.println(file1.isFile());//false
//代碼優化
//先判斷路徑存不存在,如果路徑不存在就沒有必要獲取
if(file1.exists()){//路徑不存在,不執行下面兩行代碼
System.out.println(file1.isDirectory());
System.out.println(file1.isFile());
}
File file2 = new File("E:\\IdeaProjects\\spring-study");//真實存在的絕對路徑,結尾是檔案夾
if(file2.exists()){
System.out.println(file2.isDirectory());//結尾是檔案夾,所以回傳值是true
System.out.println(file2.isFile());//結尾是檔案夾不是檔案,所以回傳值是false
}
File file3 = new File("E:\\IdeaProjects\\spring-study\\FileAndIO\\pom.xml");//真實存在的絕對路徑,結尾是檔案
if(file3.exists()){
System.out.println(file3.isDirectory());//結尾是檔案不是檔案夾,所以回傳值是false
System.out.println(file3.isFile());//結尾是檔案,所以回傳值是true
}
}
/**
* boolean exists() 測驗此抽象路徑名表示的檔案或目錄是否存在,
* 用于判斷構造方法中路徑是否存在
* 回傳值型別是boolean
* 存在:true
* 不存在:false
*/
private static void show1() {
File file1 = new File("E:\\IdeaProjects\\spring-study");//真實存在的絕對路徑
boolean exists1 = file1.exists();//存在則回傳true
System.out.println(exists1);
File file2 = new File("E:\\IdeaProjects\\spring-studyxxxx");//不存在的絕對路徑
boolean exists2 = file2.exists();//不存在則回傳false
System.out.println(exists2);
File file3 = new File("pom.xml");//真實存在的相對路徑
boolean exists3 = file3.exists();//存在則回傳true
System.out.println(exists3);
File file4 = new File("pomxxx.xml");//不存在的相對路徑
boolean exists4 = file4.exists();//不存在則回傳false
System.out.println(exists4);
}
}
運行結果:

File類創建和洗掉功能的方法
boolean createNewFile()
當且僅當不存在具有此抽象路徑名指定名稱的檔案時,不可分地創建一個新的空檔案,
boolean delete()
洗掉此抽象路徑名表示的檔案或目錄,
boolean mkdir()
創建此抽象路徑名指定的目錄,
boolean mkdirs()
創建此抽象路徑名指定的目錄,包括所有必需但不存在的父目錄,
import java.io.File;
import java.io.IOException;
public class DemosFile5 {
/*
boolean createNewFile() 當且僅當不存在具有此抽象路徑名指定名稱的檔案時,不可分地創建一個新的空檔案,
boolean delete() 洗掉此抽象路徑名表示的檔案或目錄,
boolean mkdir() 創建此抽象路徑名指定的目錄,
boolean mkdirs() 創建此抽象路徑名指定的目錄,包括所有必需但不存在的父目錄,
*/
public static void main(String[] args) throws IOException {
show1();
show2();
show3();
}
/*
* boolean delete() 洗掉此抽象路徑名表示的檔案或目錄,
* 此方法,可以洗掉構造方法路徑中的檔案也可以洗掉構造方法路徑中的檔案夾
* 回傳值:
* true:檔案/檔案夾洗掉成功,回傳true
* false:檔案夾中有內容,就不會洗掉檔案檔案夾,回傳false;或者構造方法中路徑是錯的,路徑不存在,就洗掉不了,也會回傳false
* 注意事項:
* delete方法是直接從硬碟洗掉檔案/檔案夾,不會經過回收站,所以使用此方法要謹慎,避免洗掉重要檔案導致不必要的損失
* */
private static void show3() throws IOException {
File file1 = new File("FileAndIO\\src\\main\\java\\a");//構造方法路徑所表示的檔案/檔案夾就是要洗掉的物件
boolean b1 = file1.delete();//a檔案夾被洗掉
System.out.println("b1:"+b1);//true
File file2 = new File("FileAndIO\\src\\main\\java\\a.txt");//構造方法路徑所表示的檔案/檔案夾就是要洗掉的物件
boolean b2 = file2.delete();//a.txt檔案被洗掉
System.out.println("b2:"+b2);//true
File file3 = new File("FileAndIO\\src\\main\\java\\asasas.txt");//構造方法路徑所表示的檔案/檔案夾就是要洗掉的物件
boolean b3 = file3.delete();//檔案不存在或路徑錯誤,洗掉失敗
System.out.println("b3:"+b3);//false
//先再abc.txt檔案夾里放點東西,我隨便創建了一個.java檔案
File file4 = new File("FileAndIO\\src\\main\\java\\abc.txt\\a.txt");//絕對路徑
System.out.println(file4.createNewFile());//true
File file5 = new File("FileAndIO\\src\\main\\java\\abc.txt");//構造方法路徑所表示的檔案/檔案夾就是要洗掉的物件
boolean b5 = file5.delete();//檔案夾中有內容,無法洗掉檔案夾
System.out.println("b5:"+b5);//false
}
/* boolean mkdir() 創建此抽象路徑名指定的目錄,
boolean mkdirs() 創建此抽象路徑名指定的目錄,包括所有必需但不存在的父目錄,
創建檔案的路徑和名稱再構造方法中給出(構造方法的引數)
* 回傳值:
* true:檔案夾不存在,不會創建,回傳true
* false:檔案夾存在,不會創建,回傳false
* 注意:
* 1.此方法只能創建檔案夾,不能創建檔案
*/
private static void show2() {
File file1 = new File("FileAndIO\\src\\main\\java\\a");//只要路徑存在,檔案夾名字唯一,就能創建成功
boolean mkdir1 = file1.mkdir();
System.out.println("mkdir1:"+mkdir1);//true
File file2 = new File("FileAndIO\\src\\main\\java\\a\\b\\c\\d");//無法創建一個有一個嵌套的檔案夾
boolean mkdir2 = file2.mkdir();
System.out.println("mkdir2:"+mkdir2);//false
File file3 = new File("FileAndIO\\src\\main\\java\\abc.txt");//創建的還是一個檔案夾,abc.txt是檔案夾的名字
boolean mkdir3 = file3.mkdir();
System.out.println("mkdir3:"+mkdir3);//true
File file4 = new File("Fil\\ccc.txt");//路徑錯誤,無法創建,但是這個方法沒有例外拋出
boolean mkdir4 = file4.mkdir();
System.out.println("mkdir4:"+mkdir4);//false
}
/**
*boolean createNewFile() 當且僅當不存在具有此抽象路徑名指定名稱的檔案時,不可分地創建一個新的空檔案,
* 創建檔案的路徑和名稱再構造方法中給出(構造方法的引數)
* 回傳值:
* true:檔案不存在,不會創建,回傳true
* false:檔案存在,不會創建,回傳false
* 注意:
* 1.此方法只能創建檔案,不能創建檔案夾
* 2.創建檔案的路徑必須存在,否則會拋出例外
*/
private static void show1() throws IOException {
File file1 = new File("E:\\IdeaProjects\\spring-study\\FileAndIO\\src\\main\\java\\a.txt");//絕對路徑
boolean b1 = file1.createNewFile();
System.out.println("b1:"+b1);//true
File file2 = new File("FileAndIO\\src\\main\\java\\b.txt");//絕對路徑
System.out.println(file2.createNewFile());//true
}
}
運行結果:

File類的目錄的遍歷功能
String[] list()
回傳一個字串陣列,這些字串指定此抽象路徑名表示的目錄中的檔案和目錄,
File[] listFiles()
回傳一個抽象路徑名陣列,這些路徑名表示此抽象路徑名表示的目錄中的檔案,
import java.io.File;
public class DemosFile6 {
/*
String[] list()
回傳一個字串陣列,這些字串指定此抽象路徑名表示的目錄中的檔案和目錄,
File[] listFiles()
回傳一個抽象路徑名陣列,這些路徑名表示此抽象路徑名表示的目錄中的檔案,
注意:
list方法和listFiles方法遍歷的是構造方法中給出的目錄
如果構造方法中給出的目錄的路徑不存在,就會拋出空指標例外
如果構造方法中給出的路徑不是一個目錄,也會拋出空指標例外
*/
public static void main(String[] args) {
show1();
show2();
}
/*File[] listFiles()
回傳一個抽象路徑名陣列,這些路徑名表示此抽象路徑名表示的目錄中的檔案,
此方法會遍歷構造方法中給出的目錄,會獲取目錄中所有檔案/檔案夾的名稱,把獲取到的所有檔案/檔案夾封裝為File物件并存盤到File陣列中
*/
private static void show2() {
File file1 = new File("E:\\IdeaProjects\\spring-study\\FileAndIO\\src\\main\\java");
File[] files = file1.listFiles();
System.out.println(files);
for (File file : files) {
System.out.println(file);
}
}
/*String[] list()
回傳一個字串陣列,這些字串指定此抽象路徑名表示的目錄中的檔案和目錄,
此方法會遍歷構造方法中給出的目錄,會獲取目錄中所有檔案/檔案夾的名稱,把獲取到的多個名稱存盤到一個String型別的陣列中*/
private static void show1() {
File file1 = new File("E:\\IdeaProjects\\spring-study\\FileAndIO\\src\\main\\java");
String[] list1 = file1.list();
System.out.println(list1);
//遍歷列印list1陣列
for (String s : list1) {
System.out.println(s);
}
}
}
運行結果:

IO流
I:input 輸入(讀取)->把硬碟中的資料,讀取到記憶體中使用
O:output 輸出(寫入)->把記憶體中的資料,寫入到硬碟中保存
流資料(字符,位元組):1字符=2位元組,1位元組=8位

位元組流
一切皆是位元組
一切檔案資料(文本,圖片,視頻等)再存盤時,都是以二進制數字的形式保存,都是一個一個位元組,那么傳輸時一樣如此,所以位元組流可以傳輸任意檔案資料,在操作流的時候,我們要時刻明確,無論使用什么樣的流物件,底層傳輸的始終為二進制資料,
位元組輸出流【OutputStream】
它是所有位元組輸出流的一個最頂層的父類,它是一個抽象類,里面定義了所有位元組流都可以使用的一些公共方法,
方法摘要
void close()
關閉此輸出流并釋放與此流有關的所有系統資源,
void flush()
重繪此輸出流并強制寫出所有緩沖的輸出位元組,
void write(byte[] b)
將 b.length 個位元組從指定的 byte 陣列寫入此輸出流,
void write(byte[] b, int off, int len)
將指定 byte 陣列中從偏移量 off 開始的 len 個位元組寫入此輸出流,
abstract void write(int b)
將指定的位元組寫入此輸出流,
FilterOutputStream:檔案位元組輸出流
作用:把記憶體中的資料寫入到硬碟的檔案中
構造方法摘要
FileOutputStream(File file)
創建一個向指定 File 物件表示的檔案中寫入資料的檔案輸出流,
FileOutputStream(File file, boolean append)
創建一個向指定 File 物件表示的檔案中寫入資料的檔案輸出流,
FileOutputStream(FileDescriptor fdObj)
創建一個向指定檔案描述符處寫入資料的輸出檔案流,該檔案描述符表示一個到檔案系統中的某個實際檔案的現有連接,
FileOutputStream(String name)
創建一個向具有指定名稱的檔案中寫入資料的輸出檔案流,
FileOutputStream(String name, boolean append)
創建一個向具有指定 name 的檔案中寫入資料的輸出檔案流,
其中常用的兩個構造方法:
FileOutputStream(File file)
創建一個向指定 File 物件表示的檔案中寫入資料的檔案輸出流,
FileOutputStream(String name)
創建一個向具有指定名稱的檔案中寫入資料的輸出檔案流,
引數:資料寫入的位置/目的地,可以是一個檔案路徑,也可以是一個檔案
? String name:目的地是一個檔案的路徑
? File file:目的地是一個檔案
構造方法的作用:
1. 創建一個FileOutputStream物件
2. 會根據構造方法中傳遞的檔案/檔案路徑,創建一個空的檔案
3. 會把FileOutputStream物件指向創建好的檔案
怎么把單個位元組由記憶體中寫入到硬碟中?
import java.io.FileOutputStream;
import java.io.IOException;
public class DemosFile8 {
public static void main(String[] args) throws IOException {
//1.創建一個FileOutputStream物件,構造方法中傳入要寫入資料的目的地
FileOutputStream fos = new FileOutputStream("E:\\IdeaProjects\\spring-study\\FileAndIO\\src\\main\\java\\c.txt");
//2.呼叫FileOutputStream物件中的方法write,把資料寫入指定檔案中
//pubilc abstract void write(int b)將指定的位元組寫入此輸出流,
fos.write(97);//a
//3.釋放資源(流會占用記憶體,使用完畢要釋放記憶體,提高程式效率)
fos.close();
}
}

怎么把多個位元組由記憶體中寫入到硬碟中?
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
public class DemosFile9 {
public static void main(String[] args) throws IOException {
//1.創建一個FileOutputStream物件,構造方法中傳入要寫入資料的目的地
FileOutputStream fos = new FileOutputStream(new File("FileAndIO\\src\\main\\java\\d.txt"));
//2.創建一個byte[]陣列,供write(byte[] b)方法使用
byte[] bytes1 = new byte[]{65,66,67,68,69};//ABCDE
byte[] bytes2 = new byte[]{-65,-66,-67,68,69};//烤紻E
/*3.呼叫FileOutputStream物件中的方法write(byte[] b),把多個位元組資料寫入指定檔案中
void write(byte[] b)
將 b.length 個位元組從指定的 byte 陣列寫入此輸出流,
注意:
如果寫入的第一個位元組是正數(0~127),那么顯示的時候會查詢ASCII表
如果寫的第一個位元組是負數,那么第一個位元組和第二個位元組會組成一個中文顯示,查詢系統默認碼表(GBK)*/
fos.write(bytes1);
fos.write(bytes2);
/*void write(byte[] b, int off, int len)
將指定 byte 陣列中從偏移量 off 開始的 len 個位元組寫入此輸出流,
int off:陣列的開始索引
int len:寫幾個位元組
*/
fos.write(bytes1,0,3);//ABC
fos.write(bytes1,0,2);//AB
fos.write(bytes1,1,4);//BCDE
// fos.write(bytes1,1,5);//IndexOutOfBoundsException索引越界例外(上面三行代碼仍然運行成功)
// fos.write(bytes1,0,3);//由于上面一行代碼IndexOutOfBoundsException索引越界例外,所以下面的代碼就不執行了
//寫入字串的方法:byte[] getBytes() 把字串轉換成為位元組陣列
byte[] bytes3 = "你好世界".getBytes();
System.out.println(Arrays.toString(bytes3));
fos.write(bytes3);
/*在檔案中顯示100,需要寫3個位元組
fos.write(49);
fos.write(48);
fos.write(48);*/
//4.釋放資源(流會占用記憶體,使用完畢要釋放記憶體,提高程式效率)
fos.close();
}
}

位元組輸出流的續寫和換行
給檔案續寫:使用兩個引數的構造方法
- FileOutputStream(File file, boolean append)
創建一個向指定 File 物件表示的檔案中寫入資料的檔案輸出流, - FileOutputStream(String name, boolean append)
創建一個向具有指定 name 的檔案中寫入資料的輸出檔案流,
引數:
? String name,File file:寫入資料的目的地
? boolean append:追加寫開關;回傳值:true->創建物件不會覆寫原檔案,繼續在檔案的末尾追加寫資料
? false->創建一個新檔案,覆寫原檔案
import java.io.FileOutputStream;
import java.io.IOException;
/*
* 給檔案續寫:使用兩個引數的構造方法
* FileOutputStream(File file, boolean append)
創建一個向指定 File 物件表示的檔案中寫入資料的檔案輸出流,
* FileOutputStream(String name, boolean append)
創建一個向具有指定 name 的檔案中寫入資料的輸出檔案流,
* 引數:
String name,File file:寫入資料的目的地
boolean append:追加寫開關;
* 回傳值:true->創建物件不會覆寫原檔案,繼續在檔案的末尾追加寫資料
false->創建一個新檔案,覆寫原檔案
* */
public class DemosFile10 {
public static void main(String[] args) throws IOException {
//1.創建一個FileOutputStream物件,構造方法中傳入要寫入資料的目的地
FileOutputStream fos = new FileOutputStream("FileAndIO\\src\\main\\java\\d.txt",true);
for (int i = 0; i < 10; i++) {
fos.write("\r\n".getBytes());//每次寫入資料并換行
fos.write("追寫的資料".getBytes());
}
fos.close();
}
}

方法摘要
int available()
回傳此輸入流下一個方法呼叫可以不受阻塞地從此輸入流讀取(或跳過)的估計位元組數,
void close()
關閉此輸入流并釋放與該流關聯的所有系統資源,
void mark(int readlimit)
在此輸入流中標記當前的位置,
boolean markSupported()
測驗此輸入流是否支持 mark 和 reset 方法,
abstract int read()
從輸入流中讀取資料的下一個位元組,
int read(byte[] b)
從輸入流中讀取一定數量的位元組,并將其存盤在緩沖區陣列 b 中,
int read(byte[] b, int off, int len)
將輸入流中最多 len 個資料位元組讀入 byte 陣列,
void reset()
將此流重新定位到最后一次對此輸入流呼叫 mark 方法時的位置,
long skip(long n)
跳過和丟棄此輸入流中資料的 n 個位元組,
位元組輸入流FileInputStream類
構造方法摘要
FileInputStream(File file)
通過打開一個到實際檔案的連接來創建一個 FileInputStream,該檔案通過檔案系統中的 File 物件 file 指定,
FileInputStream(FileDescriptor fdObj)
通過使用檔案描述符 fdObj 創建一個 FileInputStream,該檔案描述符表示到檔案系統中某個實際檔案的現有連接,
FileInputStream(String name)
通過打開一個到實際檔案的連接來創建一個 FileInputStream,該檔案通過檔案系統中的路徑名 name 指定,
引數:是讀取檔案的資料源
- String name:檔案的路徑
- File file:檔案
構造方法的作用:
- 會創建一個FileInputStream物件
- 會把FileInputStream物件指向構造方法中要讀取的檔案
位元組輸入流的使用步驟--3步(重點)
1.創建一個FileInputStream物件,構造方法中系結要讀取的資料源
2.使用FileInputStream物件中的方法read,讀取檔案
3.釋放資源
import java.io.FileInputStream;
import java.io.IOException;
/*java.io.InputStream 位元組輸入流
* 此抽象類是位元組輸入流的所以類的父類(超類)
*
* 定義了所有子類共性的方法:
* int read()從輸入流中讀取資料的下一個位元組
* int read(byte[] b)從輸入流中讀取一定數量的位元組,并將其存盤在緩沖區陣列 b 中
* void close() 關閉此輸入流并釋放與該流關聯的所有資源
*
* java.io.FileInputStream extends InputStream
* FileInputStream:檔案位元組輸入流
* 作用:把硬碟檔案中的資料,讀取到記憶體中使用
*
* 構造方法
* 構造方法摘要
FileInputStream(String name) 通過打開一個到實際檔案的連接來創建一個 FileInputStream,該檔案通過檔案系統中的路徑名 name 指定,
FileInputStream(File file) 通過打開一個到實際檔案的連接來創建一個 FileInputStream,該檔案通過檔案系統中的 File 物件 file 指定,
引數:是讀取檔案的資料源
* String name:檔案的路徑
* File file:檔案
構造方法的作用:
* 1.會創建一個FileInputStream物件
* 2.會把FileInputStream物件指向構造方法中要讀取的檔案
讀取資料的原理(硬碟->記憶體)
java程式-->JVM-->OS-->OS讀取資料的方法-->讀取檔案
位元組輸入流的使用步驟--3步(重點)
1.創建一個FileInputStream物件,構造方法中系結要讀取的資料源
2.使用FileInputStream物件中的方法read,讀取檔案
3.釋放資源
* */
public class DemosFile11 {
public static void main(String[] args) throws IOException {
//1.創建一個FileOutputStream物件,構造方法中傳入要寫入資料的目的地
FileInputStream fos = new FileInputStream("FileAndIO\\src\\main\\java\\c.txt");
int len = fos.read();
System.out.println(len);//65
len = fos.read();
System.out.println(len);//66
len = fos.read();
System.out.println(len);//67
len = fos.read();
System.out.println(len);//-1 表示讀取結束,檔案中資料讀完了
len = fos.read();
System.out.println(len);//資料讀完之后再讀一次,還是-1
//釋放資源
fos.close();
}
}

代碼優化:
import java.io.FileInputStream;
import java.io.IOException;
/*java.io.InputStream 位元組輸入流
* 此抽象類是位元組輸入流的所以類的父類(超類)
*
* 定義了所有子類共性的方法:
* int read()從輸入流中讀取資料的下一個位元組
* int read(byte[] b)從輸入流中讀取一定數量的位元組,并將其存盤在緩沖區陣列 b 中
* void close() 關閉此輸入流并釋放與該流關聯的所有資源
*
* java.io.FileInputStream extends InputStream
* FileInputStream:檔案位元組輸入流
* 作用:把硬碟檔案中的資料,讀取到記憶體中使用
*
* 構造方法
* 構造方法摘要
FileInputStream(String name) 通過打開一個到實際檔案的連接來創建一個 FileInputStream,該檔案通過檔案系統中的路徑名 name 指定,
FileInputStream(File file) 通過打開一個到實際檔案的連接來創建一個 FileInputStream,該檔案通過檔案系統中的 File 物件 file 指定,
引數:是讀取檔案的資料源
* String name:檔案的路徑
* File file:檔案
構造方法的作用:
* 1.會創建一個FileInputStream物件
* 2.會把FileInputStream物件指向構造方法中要讀取的檔案
讀取資料的原理(硬碟->記憶體)
java程式-->JVM-->OS-->OS讀取資料的方法-->讀取檔案
位元組輸入流的使用步驟--3步(重點)
1.創建一個FileInputStream物件,構造方法中系結要讀取的資料源
2.使用FileInputStream物件中的方法read,讀取檔案
3.釋放資源
* */
public class DemosFile11 {
public static void main(String[] args) throws IOException {
//1.創建一個FileInputStream物件,構造方法中系結要讀取的資料源
FileInputStream fos = new FileInputStream("FileAndIO\\src\\main\\java\\c.txt");
/*int len = fos.read();
System.out.println(len);//65
len = fos.read();
System.out.println(len);//66
len = fos.read();
System.out.println(len);//67
len = fos.read();
System.out.println(len);//-1 表示讀取結束,檔案中資料讀完了
len = fos.read();
System.out.println(len);//資料讀完之后再讀一次,還是-1
*/
/*上面的代碼都是重復代碼,可以用回圈陳述句來優化代碼*/
//因為不知道讀取多少次擦磁能讀完檔案,所以用while回圈
int len = 0;
while ((len = fos.read())!=-1){
System.out.print((char) len);//把讀到的結果轉換成字符表示出來
}
//釋放資源
fos.close();
}
}
位元組輸入流一次讀取多個位元組

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
/*位元組輸入流一次讀取多個位元組的方法:
int read(byte[] b)從輸入流中讀取一定數量的位元組,并將其存盤在緩沖區陣列b中,
明確兩件事情:
1.方法的引數byte[]的作用?
2.方法回傳值int是什么?
* */
public class DemosFile12 {
public static void main(String[] args) throws IOException {
//1.創建一個FileInputStream物件,構造方法中系結要讀取的資料源
FileInputStream fis = new FileInputStream("FileAndIO\\src\\main\\java\\c.txt");
//2.使用FileInputStream物件中的read(byte[] b)方法讀取檔案
//int read(byte[] b)從輸入流中讀取一定數量的位元組,并將其存盤在緩沖區陣列b中
/*byte[] bytes = new byte[2];
int len = fis.read(bytes);
System.out.println(len);//2
System.out.println(Arrays.toString(bytes));//[65, 66]
System.out.println(new String(bytes));//AB
len = fis.read(bytes);
System.out.println(len);//2
System.out.println(new String(bytes));//CD
len = fis.read(bytes);
System.out.println(len);//1
System.out.println(new String(bytes));//ED
len = fis.read(bytes);
System.out.println(len);//-1
System.out.println(new String(bytes));//ED
len = fis.read(bytes);
System.out.println(len);//-1
System.out.println(new String(bytes));//ED
*/
//回圈優化以上代碼,因為不知道讀取多少次擦磁能讀完檔案,所以用while回圈
byte[] bytes = new byte[1024];
int len = 0;//記錄每次讀取的有效位元組的個數
while ((len = fis.read(bytes))!=-1){
System.out.println(new String(bytes));//這種轉換字串的方法會多很多空格,推薦使用下面一種方法
System.out.println(new String(bytes,0,len));
}
//釋放資源
fis.close();
}
}

位元組流完成復制檔案操作
原理:一讀一寫
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class DemosFileCopy {
public static void main(String[] args) throws IOException {
long start = System.currentTimeMillis();//開始計時(用來計算程式執行時間)
//注意:讀的檔案是什么格式,寫的目的地就要是什么格式的檔案,寫的目的地不能是檔案夾
FileInputStream fis = new FileInputStream("C:\\Users\\98398\\Desktop\\我的截圖\\wallhaven-rddz6w.jpg");
FileOutputStream fos = new FileOutputStream("E:\\IdeaProjects\\spring-study\\FileAndIO\\src\\main\\java\\abc.txt\\2.jpg",true);
int len = 0;
byte[] bytes = new byte[10240];
while ((len = fis.read(bytes))!=-1){
fos.write(bytes,0,len);
}
//釋放資源(先關閉寫的,后關閉讀的)
fos.close();
fis.close();
long end = System.currentTimeMillis();//結束計時(用來計算程式執行時間)
System.out.println("復制圖片耗時"+(end-start)+"ms");
}
}
字符流Reader
3個共性成員方法
- int read() 讀取單個字符
- int read(char[] cbuf) 將字符讀入陣列
- void close() 關閉該流并釋放與之關聯的所有資源
java.lang.Object
繼承者 java.io.Reader
繼承者 java.io.InputStreamReader
繼承者 java.io.FileReader
構造方法
FileReader(File file)
在給定從中讀取資料的 File 的情況下創建一個新 FileReader,
FileReader(FileDescriptor fd)
在給定從中讀取資料的 FileDescriptor 的情況下創建一個新 FileReader,
FileReader(String fileName)
在給定從中讀取資料的檔案名的情況下創建一個新 FileReader,
字符流讀取硬碟中資料
字符輸入流FileReader
使用步驟
- 創建一個FileReader物件,構造方法中系結要讀取的資料源
- 使用FileReader物件中的方法read讀取檔案
- 釋放資源
import java.io.FileReader;
import java.io.IOException;
public class DemosFile13 {
public static void main(String[] args) throws IOException {
//1.創建一個FileReader物件,構造方法中系結要讀取的資料源
FileReader fr = new FileReader("FileAndIO\\src\\main\\java\\abc.txt\\a.txt");
//2.使用FileReader物件中的read()方法讀取檔案【一次讀取一個字符】
/* int len = 0;
while ((len = fr.read())!=-1){
System.out.println((char) len);
}*/
//一次讀取多個字符:int read(char[] cbuf) 將字符讀入陣列【一次讀取多個字符】
int len = 0;
char[] chars = new char[1024];
while ((len = fr.read(chars))!=-1){
System.out.println(new String(chars,0,len));
}
//3.釋放資源
fr.close();
}
}
字符輸出流Writer
方法摘要
Writer append(char c)
將指定字符添加到此 writer,
Writer append(CharSequence csq)
將指定字符序列添加到此 writer,
Writer append(CharSequence csq, int start, int end)
將指定字符序列的子序列添加到此 writer.Appendable,
abstract void close()
關閉此流,但要先重繪它,
abstract void flush()
重繪該流的緩沖,
void write(char[] cbuf)
寫入字符陣列,
abstract void write(char[] cbuf, int off, int len)
寫入字符陣列的某一部分,
void write(int c)
寫入單個字符,
void write(String str)
寫入字串,
void write(String str, int off, int len)
寫入字串的某一部,
檔案字符輸出流FileWriter
java.lang.Object
繼承者 java.io.Writer
繼承者 java.io.OutputStreamWriter
繼承者 java.io.FileWriter
構造方法摘要
FileWriter(File file)
根據給定的 File 物件構造一個 FileWriter 物件,
FileWriter(File file, boolean append)
根據給定的 File 物件構造一個 FileWriter 物件,
FileWriter(FileDescriptor fd)
構造與某個檔案描述符相關聯的 FileWriter 物件,
FileWriter(String fileName)
根據給定的檔案名構造一個 FileWriter 物件,
FileWriter(String fileName, boolean append)
根據給定的檔案名以及指示是否附加寫入資料的 boolean 值來構造 FileWriter 物件,
1.FileWriter(File file)
根據給定的 File 物件構造一個 FileWriter 物件,
2.FileWriter(String fileName)
根據給定的檔案名構造一個 FileWriter 物件,
引數:
- String fileName:檔案的路徑
- File file:是一個檔案
構造方法的作用
- 創建一個FileWriter物件
- 會根據構造方法中傳遞的檔案/檔案的路徑,創建一個檔案
- 會把FileWriter物件指向創建好的檔案
字符輸出流Writer的使用步驟
- 創建FileWriter物件,構造方法中系結要寫入資料的目的地
- 使用FileWriter中的方法Writer,把資料寫入到記憶體緩沖區中【有一個把字符轉換為位元組的程序】【跟位元組輸出流直接把資料寫入檔案中有所不同】
- 使用FileWriter中的方法flush,把記憶體緩沖區中的資料,重繪到檔案中
- 釋放資源【會先把記憶體緩沖區中的資料重繪到檔案中】
寫入單個字符
import java.io.FileWriter;
import java.io.IOException;
public class DemosFile13 {
public static void main(String[] args) throws IOException {
//1.創建一個FileWriter物件,構造方法中系結要讀取的資料源
FileWriter fw = new FileWriter("FileAndIO\\src\\main\\java\\abc.txt\\e.txt");
//2.使用FileWriter中的方法Writer,把資料寫入到記憶體緩沖區中【有一個==把字符轉換為位元組==的程序】【跟位元組輸出流直接把資料寫入檔案中有所不同】
fw.write(97);//a
//3.使用FileWriter中的方法flush,把記憶體緩沖區中的資料,重繪到檔案中
fw.flush();//【重繪緩沖區】
//4.釋放資源 l
fw.close();
}
}
寫入多個字符【字符輸出流寫資料得其它方法】
import java.io.FileWriter;
import java.io.IOException;
/*
void write(char[] cbuf)
寫入字符陣列,
abstract void write(char[] cbuf, int off, int len)
寫入字符陣列的某一部分,
void write(int c)
寫入單個字符,
void write(String str)
寫入字串,
void write(String str, int off, int len)
寫入字串的某一部分,
*/
public class DemosFile13 {
public static void main(String[] args) throws IOException {
//1.創建一個FileWriter物件,構造方法中系結要讀取的資料源
FileWriter fw = new FileWriter("FileAndIO\\src\\main\\java\\abc.txt\\e.txt");
//2.使用FileWriter中的方法Writer,把資料寫入到記憶體緩沖區中【有一個==把字符轉換為位元組==的程序】【跟位元組輸出流直接把資料寫入檔案中有所不同】
//void write(char[] cbuf)
char[] chars = {'a', 'b', 'c', 'd', 'e', 'f', 'g'};
fw.write(chars);
//3.使用FileWriter中的方法flush,把記憶體緩沖區中的資料,重繪到檔案中【重繪緩沖區】
fw.flush();//abcdefg
//重繪緩沖區后,還可以繼續寫入資料
fw.write(chars);//abcdefg
//abstract void write(char[] cbuf, int off, int len)寫入字符陣列的某一部分,
fw.write(chars,2,2);//cd
//void write(String str)寫入字串,
fw.write("我是字串");
//void write(String str, int off, int len)寫入字串的某一部分,
fw.write("我是字串我是字串",3,4);//符串我是
//4.釋放資源
fw.close();
}
}
字符輸出流的續寫和換行
- FileWriter(File file, boolean append)
根據給定的 File 物件構造一個 FileWriter 物件, - FileWriter(String fileName, boolean append)
根據給定的檔案名以及指示是否附加寫入資料的 boolean 值來構造 FileWriter 物件,
File file和String fileName->表示寫入資料的目的地
boolean appebd表示續寫開關:true->不會創建新的檔案覆寫,支持續寫 false->創建新的檔案覆寫
import java.io.FileWriter;
import java.io.IOException;
/*
void write(char[] cbuf)
寫入字符陣列,
abstract void write(char[] cbuf, int off, int len)
寫入字符陣列的某一部分,
void write(int c)
寫入單個字符,
void write(String str)
寫入字串,
void write(String str, int off, int len)
寫入字串的某一部分,
*/
public class DemosFile13 {
public static void main(String[] args) throws IOException {
//1.創建一個FileWriter物件,構造方法中系結要讀取的資料源
FileWriter fw = new FileWriter("FileAndIO\\src\\main\\java\\abc.txt\\e.txt",true);
for (int i = 0; i < 10; i++) {
fw.write("\r\n");//每次寫入資料并換行
fw.write("字符輸出流的續寫與換行");
}
fw.close();
}
}
IO例外的處理
在JDK1.7之前:
import java.io.FileWriter;
import java.io.IOException;
/*
在jdk1.7之前使用try catch finally 處理流中的例外
格式:
try{
可能會產生例外的代碼
}catch(例外類變數 變數名){
例外的處理邏輯
}finally{
一定會執行的代碼
資源釋放
}
*/
public class DemosFile13 {
public static void main(String[] args){
//擴大fw的作用域,讓finally可以使用fw
//變數在定義的時候可以沒有值,但是再使用的時候必須有值
//fw = new FileWriter("FileAndIO\\src\\main\\java\\abc.txt\\e.txt",true);這行代碼如果執行失敗,fw就會沒有值,fw.close();就會報錯
FileWriter fw = null;
//1.創建一個FileWriter物件,構造方法中系結要讀取的資料源
try {
fw = new FileWriter("WileAndIO\\src\\main\\java\\abc.txt\\e.txt",true);//故意寫個錯誤路徑
for (int i = 0; i < 10; i++) {
fw.write("\r\n");//每次寫入資料并換行
fw.write("字符輸出流的續寫與換行");
}
}catch (IOException e){
System.out.println(e);
}finally {
//創建fw物件失敗了,fw的值還是null,而null是不能呼叫方法的,所以fw.close();會拋出空指標例外
//所以需要增加一個判斷,不是null再把資源釋放
if (fw!=null){
try {
//fw.close();發發宣告拋出了IOException例外物件,所以我們必須處理這個例外物件,要么throw要么try catch
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
JDK7的新特性
import java.io.FileWriter;
import java.io.IOException;
/*
在JDK7的新特性
在try的后邊可以增加一個(),在括號中可以定義流物件
那么這個流物件的作用域就在try中有效
try中的代碼執行完畢會自動把流物件釋放,不用寫finally
格式:
try(定義流物件;定義流物件;...){
可能會產生例外的代碼
}catch(例外類變數 變數名){
例外的處理邏輯
}
*/
public class DemosFile13 {
public static void main(String[] args){
try(FileWriter fw = new FileWriter("WileAndIO\\src\\main\\java\\abc.txt\\e.txt",true)/*故意寫個錯誤路徑*/){
for (int i = 0; i < 10; i++) {
fw.write("\r\n");//每次寫入資料并換行
fw.write("字符輸出流的續寫與換行");
}
}catch (IOException e){
System.out.println(e);
}
}
}
JDK9的新特性
import java.io.FileWriter;
import java.io.IOException;
/*
在JDK9的新特性
在try的前邊可以定義一個流物件,在try的后面的()中可以直接引入流物件的名稱(變數名)
try中的代碼執行完畢會自動把流物件釋放,不用寫finally不用寫fw.close
格式:
A a = new A();
B b = new B();
try(a,b){
可能會產生例外的代碼
}catch(例外類變數 變數名){
例外的處理邏輯
}
*/
public class DemosFile13 {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("FileAndIO\\src\\main\\java\\abc.txt\\e.txt",true);
try(fw){
for (int i = 0; i < 10; i++) {
fw.write("\r\n");//每次寫入資料并換行
fw.write("字符輸出流的續寫與換行");
}
}catch (IOException e){
System.out.println(e);
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/282694.html
標籤:其他
