【Java中级】(三)IO
生活随笔
收集整理的這篇文章主要介紹了
【Java中级】(三)IO
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1. 流分為字節流和字符流2. 字節流下面常用的又有數據流和對象流3. 字符流下面常用的又有緩存流
文件對象文件和文件夾都用File表示//file path : 文件的絕對路徑或相對路徑File file=new File("file path");注:文件路徑可能不存在File的常用方法自行查閱API流什么是流(Stream),流就是一系列的數據當不同的介質之間有數據交互的時候,JAVA就使用流來實現。數據源可以是文件,還可以是數據庫,網絡甚至是其他的程序比如讀取文件的數據到程序中,站在程序的角度來看,就叫做輸入流輸入流: InputStream輸出流:OutputStream3.1、字節流,關閉流的方式以字節的形式讀取和寫入數據所有的數據存放在計算機中都是以數字的形式存放的。 所以字母就需要轉換為數字才能夠存放。比如A就對應的數字65,a對應的數字97. 不同的字母和符號對應不同的數字,就是一張碼表。ASCII是這樣的一種碼表。 字包含簡單的英文字母,符號,數字等等。 不包含中文,德文,俄語等復雜的。
3.1.1、輸入字節流InputStream是字節輸入流,同時也是抽象類,只提供方法的生命,不提供方法的實現。FileInputStream是InputStream的子類以FIleInputStream為例進行文件讀取:package stream;import java.io.File;import java.io.FileInputStream;import java.io.IOException;public class TestStream {public static void main(String[] args) {try {//準備文件lol.txt其中的內容是AB,對應的ASCII分別是65 66File f =new File("d:/lol.txt");//創建基于文件的輸入流FileInputStream fis =new FileInputStream(f);//創建字節數組,其長度就是文件的長度byte[] all =new byte[(int) f.length()];//以字節流的形式讀取文件所有內容fis.read(all);for (byte b : all) {//打印出來是65 66System.out.println(b);}//每次使用完流,都應該進行關閉fis.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}3.1.2、輸出字節流OutputStream是字節輸出流,同時也是抽象類,只提供方法聲明,不提供方法的具體實現。FileOutputStream 是OutputStream子類,以FileOutputStream 為例向文件寫出數據注: 如果文件d:/lol2.txt不存在,寫出操作會自動創建該文件。但是如果是文件 d:/xyz/lol2.txt,而目錄xyz又不存在,會拋出異常package stream;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;public class TestStream {public static void main(String[] args) {try {// 準備文件lol2.txt其中的內容是空的File f = new File("d:/lol2.txt");// 準備長度是2的字節數組,用88,89初始化,其對應的字符分別是X,Ybyte data[] = { 88, 89 };// 創建基于文件的輸出流FileOutputStream fos = new FileOutputStream(f);// 把數據寫入到輸出流fos.write(data);// 關閉輸出流fos.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}3.1.3、在try中關閉在try的作用域里關閉文件輸入流,在前面的示例中都是使用這種方式,這樣做有一個弊端;如果文件不存在,或者讀取的時候出現問題而拋出異常,那么就不會執行這一行關閉流的代碼,存在巨大的資源占用隱患。 不推薦使用package stream;import java.io.File;import java.io.FileInputStream;import java.io.IOException;public class TestStream {public static void main(String[] args) {try {File f = new File("d:/lol.txt");FileInputStream fis = new FileInputStream(f);byte[] all = new byte[(int) f.length()];fis.read(all);for (byte b : all) {System.out.println(b);}// 在try 里關閉流fis.close();} catch (IOException e) {e.printStackTrace();}}}3.1.4、在finally中關閉這是標準的關閉流的方式1. 首先把流的引用聲明在try的外面,如果聲明在try里面,其作用域無法抵達finally.2. 在finally關閉之前,要先判斷該引用是否為空3. 關閉的時候,需要再一次進行try catch處理這是標準的嚴謹的關閉流的方式,但是看上去很繁瑣,所以寫不重要的或者測試代碼的時候,都會采用上面的有隱患try的方式,因為不麻煩~package stream;import java.io.File;import java.io.FileInputStream;import java.io.IOException;public class TestStream {public static void main(String[] args) {File f = new File("d:/lol.txt");FileInputStream fis = null;try {fis = new FileInputStream(f);byte[] all = new byte[(int) f.length()];fis.read(all);for (byte b : all) {System.out.println(b);}} catch (IOException e) {e.printStackTrace();} finally {// 在finally 里關閉流if (null != fis)try {fis.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}3.1.5、使用try()的方式把流定義在try()里,try,catch或者finally結束的時候,會自動關閉這種編寫代碼的方式叫做 try-with-resources, 這是從JDK7開始支持的技術所有的流,都實現了一個接口叫做 AutoCloseable,任何類實現了這個接口,都可以在try()中進行實例化。 并且在try, catch, finally結束的時候自動關閉,回收相關資源。package stream;import java.io.File;import java.io.FileInputStream;import java.io.IOException;public class TestStream {public static void main(String[] args) {File f = new File("d:/lol.txt");//把流定義在try()里,try,catch或者finally結束的時候,會自動關閉try (FileInputStream fis = new FileInputStream(f)) {byte[] all = new byte[(int) f.length()];fis.read(all);for (byte b : all) {System.out.println(b);}} catch (IOException e) {e.printStackTrace();}}}綜合實例
3.2、字符流,中文編碼問題Reader是字符輸入流Writer是字符輸出流專門用于字符形式讀取和寫入數據3.2.1、使用字符流讀取文件FileReader是Reader子類,以FileReader為例進行文件讀取package stream;import java.io.File;import java.io.FileReader;import java.io.IOException;public class TestStream {public static void main(String[] args) {// 準備文件lol.txt其中的內容是ABFile f = new File("d:/lol.txt");// 創建基于文件的Readertry (FileReader fr = new FileReader(f)) {// 創建字符數組,其長度就是文件的長度char[] all = new char[(int) f.length()];// 以字符流的形式讀取文件所有內容fr.read(all);for (char b : all) {// 打印出來是A BSystem.out.println(b);}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}3.2.2、使用字符流把字符串寫到文件FileWriter是Writer的子類,以FileWriter為例把字符串寫到文件package stream;import java.io.File;import java.io.FileWriter;import java.io.IOException;public class TestStream {public static void main(String[] args) {// 準備文件lol2.txtFile f = new File("d:/lol2.txt");// 創建基于文件的Writertry (FileWriter fr = new FileWriter(f)) {// 以字符流的形式把數據寫入到文件中String data="abcdefg1234567890";char[] cs = data.toCharArray();fr.write(cs);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}3.2.3、常見編碼工作后經常接觸的編碼方式有如下幾種:ISO-8859-1 ASCII 數字和西歐字母GBK GB2312 BIG5 中文UNICODE (統一碼,萬國碼)
其中ISO-8859-1 包含 ASCIIGB2312 是簡體中文,BIG5是繁體中文,GBK同時包含簡體和繁體以及日文。UNICODE 包括了所有的文字,無論中文,英文,藏文,法文,世界所有的文字都包含其中3.2.4、UNICODE和UTFUNICODE因為要存放所有的數據,所以完全按照UNICODE的方式來存儲數據,就會有很大的浪費在這種情況下,就出現了UNICODE的各種減肥子編碼,比如UTF-8對數字和字符就使用一個字節,而對漢字就使用3個字節,從而達到了減肥還能保證健康的效果
3.2.5、Java采用的Unicode寫在.java源代碼中的漢字,在執行之后,都會變成JVM中的字符。而這些中文字符采用的編碼方式,都是使用UNICODE.3.3、緩存流以介質是硬盤為例,字節流和字符流的弊端:在每一次讀寫的時候,都會訪問硬盤。 如果讀寫的頻率比較高的時候,其性能表現不佳。
為了解決以上弊端,采用緩存流。緩存流在讀取的時候,會一次性讀較多的數據到緩存中,以后每一次的讀取,都是在緩存中訪問,直到緩存中的數據讀取完畢,再到硬盤中區讀取。
就好比吃飯,不用緩存就是每吃一口都到鍋里去鏟。用緩存就是先把飯盛到碗里,碗里的吃完了,再到鍋里去鏟
緩存流在寫入數據的時候,會先把數據寫入到緩存區,直到緩存區達到一定的量,才把這些數據,一起寫入到硬盤中去。按照這種操作模式,就不會像字節流,字符流那樣每寫一個字節都訪問硬盤,從而減少了IO操作3.3.1、使用緩存流讀取數據緩存字符輸入流 BufferedReader 可以一次讀取一行數據package stream;import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.IOException;public class TestStream {public static void main(String[] args) {// 準備文件lol.txt其中的內容是// garen kill teemo// teemo revive after 1 minutes// teemo try to garen, but killed againFile f = new File("d:/lol.txt");// 創建文件字符流// 緩存流必須建立在一個存在的流的基礎上try (FileReader fr = new FileReader(f);BufferedReader br = new BufferedReader(fr);){while (true) {// 一次讀一行String line = br.readLine();if (null == line)break;System.out.println(line);}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}3.3.2、使用緩存流寫出數據PrintWriter 緩存字符輸出流, 可以一次寫出一行數據package stream;import java.io.File;import java.io.FileWriter;import java.io.IOException;import java.io.PrintWriter;public class TestStream {public static void main(String[] args) {// 向文件lol2.txt中寫入三行語句File f = new File("d:/lol2.txt");try (// 創建文件字符流FileWriter fw = new FileWriter(f);// 緩存流必須建立在一個存在的流的基礎上 PrintWriter pw = new PrintWriter(fw); ) {pw.println("garen kill teemo");pw.println("teemo revive after 1 minutes");pw.println("teemo try to garen, but killed again");} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}3.3.3、flush有的時候,需要立即把數據寫入到硬盤,而不是等緩存滿了才寫出去。 這時候就需要用到flushpackage stream;import java.io.File;import java.io.FileWriter;import java.io.IOException;import java.io.PrintWriter;public class TestStream {public static void main(String[] args) {//向文件lol2.txt中寫入三行語句File f =new File("d:/lol2.txt");//創建文件字符流//緩存流必須建立在一個存在的流的基礎上try(FileWriter fr = new FileWriter(f);PrintWriter pw = new PrintWriter(fr);) {pw.println("garen kill teemo");//強制把緩存中的數據寫入硬盤,無論緩存是否已滿pw.flush(); pw.println("teemo revive after 1 minutes");pw.flush();pw.println("teemo try to garen, but killed again");pw.flush();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
文件對象文件和文件夾都用File表示//file path : 文件的絕對路徑或相對路徑File file=new File("file path");注:文件路徑可能不存在File的常用方法自行查閱API流什么是流(Stream),流就是一系列的數據當不同的介質之間有數據交互的時候,JAVA就使用流來實現。數據源可以是文件,還可以是數據庫,網絡甚至是其他的程序比如讀取文件的數據到程序中,站在程序的角度來看,就叫做輸入流輸入流: InputStream輸出流:OutputStream3.1、字節流,關閉流的方式以字節的形式讀取和寫入數據所有的數據存放在計算機中都是以數字的形式存放的。 所以字母就需要轉換為數字才能夠存放。比如A就對應的數字65,a對應的數字97. 不同的字母和符號對應不同的數字,就是一張碼表。ASCII是這樣的一種碼表。 字包含簡單的英文字母,符號,數字等等。 不包含中文,德文,俄語等復雜的。
3.1.1、輸入字節流InputStream是字節輸入流,同時也是抽象類,只提供方法的生命,不提供方法的實現。FileInputStream是InputStream的子類以FIleInputStream為例進行文件讀取:package stream;import java.io.File;import java.io.FileInputStream;import java.io.IOException;public class TestStream {public static void main(String[] args) {try {//準備文件lol.txt其中的內容是AB,對應的ASCII分別是65 66File f =new File("d:/lol.txt");//創建基于文件的輸入流FileInputStream fis =new FileInputStream(f);//創建字節數組,其長度就是文件的長度byte[] all =new byte[(int) f.length()];//以字節流的形式讀取文件所有內容fis.read(all);for (byte b : all) {//打印出來是65 66System.out.println(b);}//每次使用完流,都應該進行關閉fis.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}3.1.2、輸出字節流OutputStream是字節輸出流,同時也是抽象類,只提供方法聲明,不提供方法的具體實現。FileOutputStream 是OutputStream子類,以FileOutputStream 為例向文件寫出數據注: 如果文件d:/lol2.txt不存在,寫出操作會自動創建該文件。但是如果是文件 d:/xyz/lol2.txt,而目錄xyz又不存在,會拋出異常package stream;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;public class TestStream {public static void main(String[] args) {try {// 準備文件lol2.txt其中的內容是空的File f = new File("d:/lol2.txt");// 準備長度是2的字節數組,用88,89初始化,其對應的字符分別是X,Ybyte data[] = { 88, 89 };// 創建基于文件的輸出流FileOutputStream fos = new FileOutputStream(f);// 把數據寫入到輸出流fos.write(data);// 關閉輸出流fos.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}3.1.3、在try中關閉在try的作用域里關閉文件輸入流,在前面的示例中都是使用這種方式,這樣做有一個弊端;如果文件不存在,或者讀取的時候出現問題而拋出異常,那么就不會執行這一行關閉流的代碼,存在巨大的資源占用隱患。 不推薦使用package stream;import java.io.File;import java.io.FileInputStream;import java.io.IOException;public class TestStream {public static void main(String[] args) {try {File f = new File("d:/lol.txt");FileInputStream fis = new FileInputStream(f);byte[] all = new byte[(int) f.length()];fis.read(all);for (byte b : all) {System.out.println(b);}// 在try 里關閉流fis.close();} catch (IOException e) {e.printStackTrace();}}}3.1.4、在finally中關閉這是標準的關閉流的方式1. 首先把流的引用聲明在try的外面,如果聲明在try里面,其作用域無法抵達finally.2. 在finally關閉之前,要先判斷該引用是否為空3. 關閉的時候,需要再一次進行try catch處理這是標準的嚴謹的關閉流的方式,但是看上去很繁瑣,所以寫不重要的或者測試代碼的時候,都會采用上面的有隱患try的方式,因為不麻煩~package stream;import java.io.File;import java.io.FileInputStream;import java.io.IOException;public class TestStream {public static void main(String[] args) {File f = new File("d:/lol.txt");FileInputStream fis = null;try {fis = new FileInputStream(f);byte[] all = new byte[(int) f.length()];fis.read(all);for (byte b : all) {System.out.println(b);}} catch (IOException e) {e.printStackTrace();} finally {// 在finally 里關閉流if (null != fis)try {fis.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}3.1.5、使用try()的方式把流定義在try()里,try,catch或者finally結束的時候,會自動關閉這種編寫代碼的方式叫做 try-with-resources, 這是從JDK7開始支持的技術所有的流,都實現了一個接口叫做 AutoCloseable,任何類實現了這個接口,都可以在try()中進行實例化。 并且在try, catch, finally結束的時候自動關閉,回收相關資源。package stream;import java.io.File;import java.io.FileInputStream;import java.io.IOException;public class TestStream {public static void main(String[] args) {File f = new File("d:/lol.txt");//把流定義在try()里,try,catch或者finally結束的時候,會自動關閉try (FileInputStream fis = new FileInputStream(f)) {byte[] all = new byte[(int) f.length()];fis.read(all);for (byte b : all) {System.out.println(b);}} catch (IOException e) {e.printStackTrace();}}}綜合實例
3.2、字符流,中文編碼問題Reader是字符輸入流Writer是字符輸出流專門用于字符形式讀取和寫入數據3.2.1、使用字符流讀取文件FileReader是Reader子類,以FileReader為例進行文件讀取package stream;import java.io.File;import java.io.FileReader;import java.io.IOException;public class TestStream {public static void main(String[] args) {// 準備文件lol.txt其中的內容是ABFile f = new File("d:/lol.txt");// 創建基于文件的Readertry (FileReader fr = new FileReader(f)) {// 創建字符數組,其長度就是文件的長度char[] all = new char[(int) f.length()];// 以字符流的形式讀取文件所有內容fr.read(all);for (char b : all) {// 打印出來是A BSystem.out.println(b);}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}3.2.2、使用字符流把字符串寫到文件FileWriter是Writer的子類,以FileWriter為例把字符串寫到文件package stream;import java.io.File;import java.io.FileWriter;import java.io.IOException;public class TestStream {public static void main(String[] args) {// 準備文件lol2.txtFile f = new File("d:/lol2.txt");// 創建基于文件的Writertry (FileWriter fr = new FileWriter(f)) {// 以字符流的形式把數據寫入到文件中String data="abcdefg1234567890";char[] cs = data.toCharArray();fr.write(cs);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}3.2.3、常見編碼工作后經常接觸的編碼方式有如下幾種:ISO-8859-1 ASCII 數字和西歐字母GBK GB2312 BIG5 中文UNICODE (統一碼,萬國碼)
其中ISO-8859-1 包含 ASCIIGB2312 是簡體中文,BIG5是繁體中文,GBK同時包含簡體和繁體以及日文。UNICODE 包括了所有的文字,無論中文,英文,藏文,法文,世界所有的文字都包含其中3.2.4、UNICODE和UTFUNICODE因為要存放所有的數據,所以完全按照UNICODE的方式來存儲數據,就會有很大的浪費在這種情況下,就出現了UNICODE的各種減肥子編碼,比如UTF-8對數字和字符就使用一個字節,而對漢字就使用3個字節,從而達到了減肥還能保證健康的效果
3.2.5、Java采用的Unicode寫在.java源代碼中的漢字,在執行之后,都會變成JVM中的字符。而這些中文字符采用的編碼方式,都是使用UNICODE.3.3、緩存流以介質是硬盤為例,字節流和字符流的弊端:在每一次讀寫的時候,都會訪問硬盤。 如果讀寫的頻率比較高的時候,其性能表現不佳。
為了解決以上弊端,采用緩存流。緩存流在讀取的時候,會一次性讀較多的數據到緩存中,以后每一次的讀取,都是在緩存中訪問,直到緩存中的數據讀取完畢,再到硬盤中區讀取。
就好比吃飯,不用緩存就是每吃一口都到鍋里去鏟。用緩存就是先把飯盛到碗里,碗里的吃完了,再到鍋里去鏟
緩存流在寫入數據的時候,會先把數據寫入到緩存區,直到緩存區達到一定的量,才把這些數據,一起寫入到硬盤中去。按照這種操作模式,就不會像字節流,字符流那樣每寫一個字節都訪問硬盤,從而減少了IO操作3.3.1、使用緩存流讀取數據緩存字符輸入流 BufferedReader 可以一次讀取一行數據package stream;import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.IOException;public class TestStream {public static void main(String[] args) {// 準備文件lol.txt其中的內容是// garen kill teemo// teemo revive after 1 minutes// teemo try to garen, but killed againFile f = new File("d:/lol.txt");// 創建文件字符流// 緩存流必須建立在一個存在的流的基礎上try (FileReader fr = new FileReader(f);BufferedReader br = new BufferedReader(fr);){while (true) {// 一次讀一行String line = br.readLine();if (null == line)break;System.out.println(line);}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}3.3.2、使用緩存流寫出數據PrintWriter 緩存字符輸出流, 可以一次寫出一行數據package stream;import java.io.File;import java.io.FileWriter;import java.io.IOException;import java.io.PrintWriter;public class TestStream {public static void main(String[] args) {// 向文件lol2.txt中寫入三行語句File f = new File("d:/lol2.txt");try (// 創建文件字符流FileWriter fw = new FileWriter(f);// 緩存流必須建立在一個存在的流的基礎上 PrintWriter pw = new PrintWriter(fw); ) {pw.println("garen kill teemo");pw.println("teemo revive after 1 minutes");pw.println("teemo try to garen, but killed again");} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}3.3.3、flush有的時候,需要立即把數據寫入到硬盤,而不是等緩存滿了才寫出去。 這時候就需要用到flushpackage stream;import java.io.File;import java.io.FileWriter;import java.io.IOException;import java.io.PrintWriter;public class TestStream {public static void main(String[] args) {//向文件lol2.txt中寫入三行語句File f =new File("d:/lol2.txt");//創建文件字符流//緩存流必須建立在一個存在的流的基礎上try(FileWriter fr = new FileWriter(f);PrintWriter pw = new PrintWriter(fr);) {pw.println("garen kill teemo");//強制把緩存中的數據寫入硬盤,無論緩存是否已滿pw.flush(); pw.println("teemo revive after 1 minutes");pw.flush();pw.println("teemo try to garen, but killed again");pw.flush();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
轉載于:https://www.cnblogs.com/haxianhe/p/9271009.html
總結
以上是生活随笔為你收集整理的【Java中级】(三)IO的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JS_arguments
- 下一篇: Java 403 forbidden错误