| InputStream 所有輸入字節流的基類 抽象類 ————| FileInputStream 讀取文件數據的輸入字節流
使用FileInputStream讀取文件數據的步驟: 1. 找到目標文件 2. 建立數據的輸入通道。 3. 讀取文件中的數據。 4. 關閉 資源.
注意點:read()方法返回的值講解:1、返回值為原本數據數據:read() 讀取一個字節的數據,把讀取的數據返回。如:int content = fileInputStream.read(); 2、返回值為字節數據的個數如果使用read讀取數據傳入字節數組,那么數據是存儲到字節數組中的,而這時候read方法的返回值是表示的是本次讀取了幾個字節數據到字節數組中。while((length = fileInputStream.read(buf))!=-1){ // read方法如果讀取到了文件的末尾,那么會返回-1表示。 System.out.PRint(new String(buf,0,length));}public class inputDemo1 { public static void main(String[] args) throws IOException { } private static void importDemo1() throws FileNotFoundException, IOException { //找到目標文件 File file = new File("/Users/liyunxiang/Desktop/1.txt"); System.out.println(file.exists()); //判斷文件是否存在 //建立數據輸入通道 FileInputStream fileInputStream = new FileInputStream(file); //建立緩沖數組配合循環讀取文件的數據。 int length = 0; //保存每次讀取到的字節個數。 //存儲讀取到的數據 緩沖數組 的長度一般是1024的倍數,因為與計算機的處理單位。 理論上緩沖數組越大,效率越高 byte[] buf = new byte[1024]; // 如果使用read讀取數據傳入字節數組,那么數據是存儲到字節數組中的, //而這時候read方法的返回值是表示的是本次讀取了幾個字節數據到字節數組中。 while((length = fileInputStream.read(buf))!=-1){ // read方法如果讀取到了文件的末尾,那么會返回-1表示。 System.out.print(new String(buf,0,length)); } //關閉資源 fileInputStream.close(); } }問題: 讀取完一個文件的數據的時候,我不關閉資源有什么影響? 答案: 資源文件一旦 使用完畢應該馬上釋放,否則其他的程序無法對該資源文件進行其他 的操作。
輸出字節流:
——–| OutputStream 是所有輸出字節流 的父類。 抽象類 ———–| FileOutStream 向文件輸出數據的輸出字節流。
FileOutputStream如何使用呢? 1. 找到目標文件 2. 建立數據的輸出通道。 3. 把數據轉換成字節數組寫出。 4. 關閉資源
FileOutputStream要注意的細節: 1. 使用FileOutputStream 的時候,如果目標文件不存在,那么會自動創建目標文件對象。 2. 使用FileOutputStream寫數據的時候,如果目標文件已經存在,那么會先清空目標文件中的數據,然后再寫入數據。 3.使用FileOutputStream寫數據的時候, 如果目標文件已經存在,需要在原來數據基礎上追加數據的時候應該使用new FileOutputStream(file,true)構造函數,第二參數為true。 4.使用FileOutputStream的write方法寫數據的時候,雖然接收的是一個int類型的數據,但是真正寫出的只是一個字節的數據,只是 把低八位的二進制數據寫出,其他二十四位數據全部丟棄。 00000000-000000000-00000001-11111111 511 11111111---> -1 public class OutPutDemo { public static void main(String[] args) throws IOException { //找到目標文件 File file = new File("/Users/liyunxiang/Desktop/1.txt"); //建立數據輸出通道 FileOutputStream fileOutputStream = new FileOutputStream(file); //每次只能寫一個字節的數據出去。 fileOutputStream.write('h'); //使用字節數組把數據寫出。 String data = "hello world"; fileOutputStream.write(data.getBytes()); //使用字節數組把數據寫出。,0 從字節數組的指定索引值開始寫, 2:寫出兩個字節。 byte[] buf = data.getBytes(); fileOutputStream.write(buf,0,3); //關閉資源 fileOutputStream.close(); }}需求: 利用輸入輸出流拷貝一張圖片
public class CopyImage { public static void main(String[] args) throws IOException { //找到目標文件 File inFile = new File("F://美女//1.jpg"); File destFile = new File("E://1.jpg"); //建立數據的輸入輸出通道 FileInputStream fileInputStream = new FileInputStream(inFile); FileOutputStream fileOutputStream = new FileOutputStream(destFile); //追加數據.... //每新創建一個FileOutputStream的時候,默認情況下FileOutputStream 的指針是指向了文件的開始的位置。 每寫出一次,指向都會出現相應移動。 //建立緩沖數據,邊讀邊寫 byte[] buf = new byte[1024]; int length = 0 ; while((length = fileInputStream.read(buf))!=-1){ //最后一次只剩下了824個字節 fileOutputStream.write(buf,0,length); //寫出很多次數據,所以就必須要追加。 } //關閉資源 原則: 先開后關,后開先關。 fileOutputStream.close(); fileInputStream.close(); }}緩沖輸入字節流
我們清楚讀取文件數據使用緩沖數組讀取效率更高,sun也知道使用緩沖數組讀取效率更高,那么 這時候sun給我們提供了一個——緩沖輸入字節流對象,讓我們可以更高效率讀取文件。
輸入字節流體系: —-| InputStream 輸入字節流的基類。 抽象 ———-| FileInputStream 讀取文件數據的輸入字節流 ———-| BufferedInputStream 緩沖輸入字節流 緩沖輸入字節流的出現主要是為了提高讀取文件數據的效率。 其實該類內部只不過是維護了一個8kb的字節數組而已。
注意: 凡是緩沖流都不具備讀寫文件的能力。
使用BufferedInputStream的步驟 : 1. 找到目標文件。 2. 建立數據 的輸入通道 3. 建立緩沖 輸入字節流流 4. 關閉資源
public class Demo1 { public static void main(String[] args) throws IOException { readTest2(); } public static void readTest2() throws IOException{ //找到目標文件 File file = new File("F://a.txt"); FileInputStream fileInputStream= new FileInputStream(file); BufferedInputStream bufferedInputStream= new BufferedInputStream(fileInputStream); bufferedInputStream.read(); FileOutputStream fileOutputStream= new FileOutputStream(file); BufferedOutputStream bufferedOutputStream= new BufferedOutputStream(fileOutputStream); fileOutputStream.write(null); //讀取文件數據 int content = 0 ; //疑問二:BufferedInputStream出現 的目的是了提高讀取文件的效率,但是BufferedInputStream的read方法每次讀取一個字節的數據 //而FileInputStreram每次也是讀取一個字節的數據,那么BufferedInputStream效率高從何而來? while((content = fileInputStream.read())!=-1){ System.out.print((char)content); } //關閉資源 bufferedInputStream.close();//調用BufferedInputStream的close方法實際上關閉的是FileinputStream. } //讀取文件的時候我們都是使用緩沖數組讀取。效率會更加高 public static void readTest() throws IOException{ File file = new File("/Users/liyunxiang/Desktop/1.txt"); //建立數組通道 FileInputStream fileInputStream = new FileInputStream(file); //建立緩沖數組讀取數據 byte[] buf = new byte[1024*8]; int length = 0; while((length = fileInputStream.read(buf))!=-1){ System.out.print(new String(buf,0,length)); } //關閉資源 fileInputStream.close(); }}輸出字節流
——–| OutputStream 所有輸出字節流的基類 抽象類 ————| FileOutputStream 向文件 輸出數據 的輸出字節流 ————| Bufferedoutputstream 緩沖輸出字節流 BufferedOutputStream出現的目的是為了提高寫數據的效率。 內部也是維護了一個8kb的字節數組而已。
使用BufferedOutputStream的步驟: 1. 找到目標文件 2. 建立數據的輸出通道
BufferedOutputStream 要注意的細節 1. 使用BufferedOutStream寫數據的時候,它的write方法是是先把數據寫到它內部維護的字節數組中。 2. 使用BufferedOutStream寫數據的時候,它的write方法是是先把數據寫到它內部維護的字節數組中,如果需要把數據真正的寫到硬盤上面,需要 調用flush方法或者是close方法、 或者是內部維護的字節數組已經填滿數據的時候。
public class Demo2 { public static void main(String[] args) throws IOException { //找到目標文件 File file = new File("/Users/liyunxiang/Desktop/1.txt"); //建立數據的輸出通道 FileOutputStream fileOutputStream = new FileOutputStream(file); //建立緩沖輸出字節流對象 BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream); //把數據寫出 bufferedOutputStream.write("hello world".getBytes()); //把緩沖數組中內部的數據寫到硬盤上面。 //bufferedOutputStream.flush(); bufferedOutputStream.close(); }}使用緩沖輸入輸出字節流拷貝一個圖片
public class CopyImage { public static void main(String[] args) throws IOException { //找到目標文件 File inFile = new File("F://美女//1.jpg"); File outFile = new File("E://1.jpg"); //建立數據輸入輸出通道 FileInputStream fileInputStream = new FileInputStream(inFile); FileOutputStream fileOutputStream = new FileOutputStream(outFile); //建立緩沖輸入輸出流 BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream); //邊讀邊寫 int content = 0; // int length = bufferedInputStream.read(buf); 如果傳入了緩沖數組,內容是存儲到緩沖數組中,返回值是存儲到緩沖數組中的字節個數。 while((content = bufferedInputStream.read())!=-1){ // 如果使用read方法沒有傳入緩沖數組,那么返回值是讀取到的內容。 bufferedOutputStream.write(content);// bufferedOutputStream.flush(); } //關閉資源 bufferedInputStream.close(); bufferedOutputStream.close(); }}IO異常 的處理
package cn.itcast.exception;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import javax.management.RuntimeErrorException;/* IO異常 的處理 */public class Demo1 { public static void main(String[] args) { // readTest(); copyImage(); } // 拷貝圖片 public static void copyImage() { FileInputStream fileInputStream = null; FileOutputStream fileOutputStream = null; try { // 找到目標文件 File inFile = new File("F://美女//1.jpg"); File outFile = new File("E://1.jpg"); // 建立輸入輸出通道 fileInputStream = new FileInputStream(inFile); fileOutputStream = new FileOutputStream(outFile); // 建立緩沖數組,邊讀邊寫 byte[] buf = new byte[1024]; int length = 0; while ((length = fileInputStream.read(buf)) != -1) { fileOutputStream.write(buf, 0, length); } } catch (IOException e) { System.out.println("拷貝圖片出錯..."); throw new RuntimeException(e); } finally { // 關閉資源 try { if (fileOutputStream != null) { fileOutputStream.close(); System.out.println("關閉輸出流對象成功..."); } } catch (IOException e) { System.out.println("關閉輸出流資源失敗..."); throw new RuntimeException(e); } finally { if (fileInputStream != null) { try { fileInputStream.close(); System.out.println("關閉輸入流對象成功..."); } catch (IOException e) { System.out.println("關閉輸入流對象失敗..."); throw new RuntimeException(e); } } } } } public static void readTest() { FileInputStream fileInputStream = null; try { // 找到目標文件 File file = new File("F://aaaaa.txt"); // 建立數據輸入通道 fileInputStream = new FileInputStream(file); // 建立緩沖數組讀取數據 byte[] buf = new byte[1024]; int length = 0; while ((length = fileInputStream.read(buf)) != -1) { System.out.print(new String(buf, 0, length)); } } catch (IOException e) { /* * //處理的代碼... 首先你要阻止后面的代碼執行,而且要需要通知調用者這里出錯了... throw new * RuntimeException(e); * //把IOException傳遞給RuntimeException包裝一層,然后再拋出,這樣子做的目的是 * 為了讓調用者使用變得更加靈活。 */ System.out.println("讀取文件資源出錯...."); throw new RuntimeException(e); } finally { try { if (fileInputStream != null) { fileInputStream.close(); System.out.println("關閉資源成功..."); } } catch (IOException e) { System.out.println("關閉資源失敗..."); throw new RuntimeException(e); } } }}輸入字符流
字節流:字節流讀取的是文件中的二進制數據,讀到的數據并不會幫你轉換成你看得懂的字符。
字符流: 字符流會把讀取到的二進制的數據進行對應 的編碼與解碼工作。 字符流 = 字節流 + 編碼(解碼)
輸入字符流: ———-| Reader 輸入字符流的基類 抽象類 ————-| FileReader 讀取文件的輸入字符流。
FileReader的用法: 1. 找到目標文件 2. 建立數據的輸入通道 3. 讀取數據 4. 關閉資源
public class Demo2 { public static void main(String[] args) throws IOException { readTest2(); } //使用緩沖字符數組讀取文件。 public static void readTest2() throws IOException{ //找到目標文件 File file = new File(“/Users/liyunxiang/Desktop/1.txt“); // 建立數據的輸入通道 FileReader fileReader = new FileReader(file); //建立緩沖字符數組讀取文件數據 char[] buf = new char[1024]; int length = 0 ; while((length = fileReader.read(buf))!=-1){ System.out.print(new String(buf,0,length)); } } public static void readTest1() throws IOException{ //找到目標文件 File file = new File("/Users/liyunxiang/Desktop/1.txt"); //建立數據的輸入通道 FileReader fileReader = new FileReader(file); int content = 0 ; while((content = fileReader.read())!=-1){ //每次只會讀取一個字符,效率低。 System.out.print((char)content); } //關閉資源 fileReader.close(); }}輸出字符流
輸出字符流:
——| Writer 輸出字符流的基類。 抽象類 ———–| FileWriter 向文件數據數據的輸出字符流
FileWriter的使用步驟: 1. 找到目標文件。 2. 建立數據輸出通道 3. 寫出數據。 4. 關閉資源
FileWriter要注意的事項: 1. 使用FileWriter寫數據的時候,FileWriter內部是維護了一個1024個字符數組的,寫數據的時候會先寫入到它內部維護的字符數組中,如果需要 把數據真正寫到硬盤上,需要調用flush或者是close方法或者是填滿了內部的字符數組。 2. 使用FileWriter的時候,如果目標文件不存在,那么會自動創建目標文件。 3.使用FileWriter的時候, 如果目標文件已經存在了,那么默認情況會先情況文件中的數據,然后再寫入數據 , 如果需要在原來的基礎上追加數據, 需要使用“new FileWriter(File , boolean)”的構造方法,第二參數為true。
public class Demo1 { public static void main(String[] args) throws IOException { writeTest1(); } public static void writeTest1() throws IOException{ //找到目標文件 File file = new File("F://a.txt"); //建立數據輸出通道 FileWriter fileWriter = new FileWriter(file,true); //準備數據,把數據寫出 String data = "今天天氣非常好!!"; fileWriter.write(data); //字符流具備解碼的功能。 //刷新字符流// fileWriter.flush(); //關閉資源 fileWriter.close(); }}//使用字符流拷貝文件public class Copy { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new FileReader("F://Test.txt")); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("E://Test.exe")); String line=null; while((line = bufferedReader.readLine())!=null){ bufferedWriter.write(line); } bufferedWriter.close(); bufferedReader.close(); }}緩沖輸入字符流
輸入字符流: ——-| Reader 所有輸入字符流的基類。 抽象類 ———-| FileReader 讀取文件字符串的輸入字符流。 ———-| BufferedReader 緩沖輸入字符流 。 緩沖 輸入字符流出現的目的是為了提高讀取文件 的效率和拓展了FileReader的功能。 其實該類內部也是維護了一個字符數組
記住:緩沖流都不具備讀寫文件的能力。
BufferedReader的使用步驟: 1.找到目標文件 2 .建立數據的輸入通道。
public static void readTest1() throws IOException{ //找到目標文件 File file = new File("F://a.txt"); //建立數據的輸入通道。 FileReader fileReader = new FileReader(file); //建立緩沖輸入字符流 BufferedReader bufferedReader = new BufferedReader(fileReader); //讀取數據 /*int content = bufferedReader.read(); //讀到了一個字符。 讀取到的字符肯定也是從Bufferedreader內部的字符數組中獲取的到。所以效率高。 System.out.println((char)content);*/ //使用BUfferedReader拓展的功能,readLine() 一次讀取一行文本,如果讀到了文件的末尾返回null表示。 String line = null; while((line = bufferedReader.readLine())!=null){ // 雖然readLine每次讀取一行數據,但是但會的line是不包含/r/n的、 System.out.println(Arrays.toString("aaa".getBytes())); } //關閉資源 bufferedReader.close(); }}緩沖輸出字符流
輸出字符流 ———-| Writer 所有輸出字符流的基類, 抽象類。 ————— | FileWriter 向文件輸出字符數據的輸出字符流。 —————-| BufferedWriter 緩沖輸出字符流 緩沖輸出字符流作用: 提高FileWriter的寫數據效率與拓展FileWriter的功能。 BufferedWriter內部只不過是提供了一個8192長度的字符數組作為緩沖區而已,拓展了FileWriter的功能。
BufferedWriter如何使用? 1. 找到目標文件 2. 建立數據的輸出通道
public class Demo2 { public static void main(String[] args) throws IOException { //找到目標文件 File file = new File("F://a.txt"); //建立數據的輸出通道 FileWriter fileWriter = new FileWriter(file,true); //建立緩沖輸出流對象 BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); //寫出數據// bufferedWriter.newLine(); //newLine() 換行。 實際上就是想文件輸出/r/n. bufferedWriter.write("/r/n"); bufferedWriter.write("前兩天李克強來蘿崗!!"); //關閉資源 bufferedWriter.flush();// bufferedWriter.close(); }}練習: 緩沖輸入輸出字符流用戶登陸注冊…
public class Login { static Scanner scanner = new Scanner(System.in); public static void main(String[] args) throws IOException { while(true){ System.out.println("請選擇功能: A(注冊) B(登陸)"); String option = scanner.next(); if("a".equalsIgnoreCase(option)){ //注冊 reg(); }else if("b".equalsIgnoreCase(option)){ //登陸 login(); }else{ System.out.println("你的輸入有誤,請重新輸入..."); } } } //登陸 public static void login() throws IOException{ System.out.println("請輸入用戶名:"); String userName = scanner.next(); System.out.println("請 輸入密碼:"); String passWord = scanner.next(); String info = userName+" "+ password; //讀取文件的信息,查看是否有該用戶的信息存在,如果存在則登陸成功。 //建立數據的輸入通道 //建立緩沖輸入字符流 BufferedReader bufferedReader = new BufferedReader(new FileReader("F://users.txt")); String line = null; boolean isLogin = false; // 用于記錄是否登陸成功的標識, 默認是登陸失敗的。 //不斷的讀取文件的內容 while((line = bufferedReader.readLine())!=null){ if(info.equals(line)){ isLogin = true; break; } } if(isLogin){ System.out.println("歡迎"+userName+"登陸成功..."); }else{ System.out.println("不存在該用戶信息,請注冊!!"); } } //注冊 public static void reg() throws IOException{ System.out.println("請輸入用戶名:"); String userName = scanner.next(); System.out.println("請 輸入密碼:"); String password = scanner.next(); String info = userName+" "+ password; //把用戶的注冊的信息寫到文件上 File file = new File("F://users.txt"); FileWriter fileWriter = new FileWriter(file,true); //建立緩沖輸出字符流 BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); //把用戶信息寫出 bufferedWriter.write(info); bufferedWriter.newLine(); //關閉資源 bufferedWriter.close(); }}
新聞熱點
疑難解答