国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

黑馬程序員_JavaSE學習總結第22天_IO流4

2019-11-15 00:23:38
字體:
來源:轉載
供稿:網(wǎng)友
黑馬程序員_javaSE學習總結第22天_IO流4

------- android培訓、java培訓、期待與您交流! ----------

22.01 數(shù)據(jù)輸入輸出流的概述和講解

操作基本數(shù)據(jù)類型

public class DataInputStreamextends FilterInputStream implements DataInput

數(shù)據(jù)輸入流允許應用程序以與機器無關方式從底層輸入流中讀取基本 Java 數(shù)據(jù)類型。應用程序可以使用數(shù)據(jù)輸出流寫入稍后由數(shù)據(jù)輸入流讀取的數(shù)據(jù)。

public class DataOutputStreamextends FilterOutputStream implements DataOutput

數(shù)據(jù)輸出流允許應用程序以適當方式將基本 Java 數(shù)據(jù)類型寫入輸出流中。然后,應用程序可以使用數(shù)據(jù)輸入流將數(shù)據(jù)讀入。

例:

 1 public class PRactice  2 { 3     public static void main(String[] args) throws IOException 4     { 5 //        write(); 6         read(); 7     } 8     private static void read() throws IOException 9     {10         // DataInputStream(InputStream in)11         // 創(chuàng)建數(shù)據(jù)輸入流對象12         DataInputStream dis = new DataInputStream(new FileInputStream("D://dos.txt"));13 14         // 讀數(shù)據(jù)15         byte b = dis.readByte();16         short s = dis.readShort();17         int i = dis.readInt();18         long l = dis.readLong();19         float f = dis.readFloat();20         double d = dis.readDouble();21         char c = dis.readChar();22         boolean bb = dis.readBoolean();23 24         // 釋放資源25         dis.close();26 27         System.out.println(b);28         System.out.println(s);29         System.out.println(i);30         System.out.println(l);31         System.out.println(f);32         System.out.println(d);33         System.out.println(c);34         System.out.println(bb);35     }36 37     private static void write() throws IOException 38     {39         // DataOutputStream(OutputStream out)40         // 創(chuàng)建數(shù)據(jù)輸出流對象41         DataOutputStream dos = new DataOutputStream(new FileOutputStream("D://dos.txt"));42 43         // 寫數(shù)據(jù)44         dos.writeByte(10);45         dos.writeShort(100);46         dos.writeInt(1000);47         dos.writeLong(10000);48         dos.writeFloat(12.34F);49         dos.writeDouble(12.56);50         dos.writeChar('a');51         dos.writeBoolean(true);52 53         // 釋放資源54         dos.close();55     }56 }

22.02 內存操作流的概述和講解

內存操作流一般用于處理臨時信息,因為臨時信息不需要保存,使用后就可以刪除

操作字節(jié)數(shù)組

1.public class ByteArrayInputStream extends InputStream

ByteArrayInputStream 包含一個內部緩沖區(qū),該緩沖區(qū)包含從流中讀取的字節(jié)。內部計數(shù)器跟蹤 read 方法要提供的下一個字節(jié)。關閉 ByteArrayInputStream 無效。此類中的方法在關閉此流后仍可被調用,而不會產生任何 IOException。

2.public class ByteArrayOutputStream extends OutputStream

此類實現(xiàn)了一個輸出流,其中的數(shù)據(jù)被寫入一個 byte 數(shù)組。緩沖區(qū)會隨著數(shù)據(jù)的不斷寫入而自動增長??墒褂?toByteArray() 和 toString() 獲取數(shù)據(jù)。

操作字符數(shù)組

1.public class CharArrayReader extends Reader

此類實現(xiàn)一個可用作字符輸入流的字符緩沖區(qū)。

2.public class CharArrayWriter extends Writer

此類實現(xiàn)一個可用作 Writer 的字符緩沖區(qū)。緩沖區(qū)會隨向流中寫入數(shù)據(jù)而自動增長。可使用 toCharArray() 和 toString() 獲取數(shù)據(jù)。

注:在此類上調用 close() 無效,并且在關閉該流后可以調用此類中的各個方法,而不會產生任何 IOException。

操作字符串

1.public class StringReader extends Reader

其源為一個字符串的字符流。

2.public class StringWriter extends Writer

一個字符流,可以用其回收在字符串緩沖區(qū)中的輸出來構造字符串。

關閉 StringWriter 無效。此類中的方法在關閉該流后仍可被調用,而不會產生任何 IOException。

以ByteArrayInputStream舉例

 1 public class Practice  2 { 3     public static void main(String[] args) throws IOException 4     { 5         // 創(chuàng)建對象 6         ByteArrayOutputStream baos = new ByteArrayOutputStream(); 7  8         // 寫數(shù)據(jù) 9         for (int x = 0; x < 10; x++) 10         {11             baos.write(("hello" + x).getBytes());12             baos.write("/r/n".getBytes());13         }14 15         // 釋放資源16         // 通過查看源碼我們知道這里什么都沒做,所以根本需要close()17         // baos.close();18 19         byte[] bys = baos.toByteArray();20 21         // 讀數(shù)據(jù)22         ByteArrayInputStream bais = new ByteArrayInputStream(bys);23 24         int by = 0;25         while ((by = bais.read()) != -1) 26         {27             System.out.print((char) by);28         }29 30         // bais.close();31     }32 }

22.03 打印流的概述和特點

打印流概述

字節(jié)流打印流

public class PrintStream extends FilterOutputStream implements Appendable, Closeable

PrintStream 為其他輸出流添加了功能,使它們能夠方便地打印各種數(shù)據(jù)值表示形式。

字符打印流

public class PrintWriter extends Writer

向文本輸出流打印對象的格式化表示形式。此類實現(xiàn)在 PrintStream 中的所有 print 方法。它不包含用于寫入原始字節(jié)的方法,對于這些字節(jié),程序應該使用未編碼的字節(jié)流進行寫入。

打印流特點:

1.只能操作目的地,不能操作數(shù)據(jù)源

2.可以操作任意類型的數(shù)據(jù)

3.如果啟動了自動刷新,能夠自動刷新

4.可以操作文件的流

22.04 PrintWriter作為Writer的子類使用

例:

 1 public class Practice  2 { 3     public static void main(String[] args) throws IOException 4     { 5         // 作為Writer的子類使用 6         PrintWriter pw = new PrintWriter("D://pw.txt"); 7  8         pw.write("hello"); 9         pw.write("world");10         pw.write("java");11         12         pw.close();13     }14 }

22.05 PrintWriter實現(xiàn)自動刷新和換行

1.可以操作任意類型的數(shù)據(jù)。print()、println()都可以

2.啟動自動刷新

PrintWriter pw = new PrintWriter(new FileWriter("D://pw.txt"), true);

還是應該調用println()的方法才可以,這個時候不僅僅自動刷新了,還實現(xiàn)了數(shù)據(jù)的換行。

println()

其實等價于于:bw.write(); bw.newLine(); bw.flush();

例:

 1 public class Practice  2 { 3     public static void main(String[] args) throws IOException 4     { 5         // 創(chuàng)建打印流對象 6         // PrintWriter pw = new PrintWriter("D://pw.txt"); 7         //啟用自動刷新 8         PrintWriter pw = new PrintWriter(new FileWriter("D://pw.txt"), true); 9 10 11         pw.println("hello");12         pw.println(true);13         pw.println(100);14 15         pw.close();16     }17 }

22.06 打印流改進復制文本文件案例

 1 public class Practice  2 { 3     public static void main(String[] args) throws IOException 4     { 5         // 封裝數(shù)據(jù)源 6         BufferedReader br = new BufferedReader(new FileReader("D://Test.java")); 7         // 封裝目的地 8         PrintWriter pw = new PrintWriter(new FileWriter("D://Copy.java"), true); 9         10         String line = null;11         while((line=br.readLine())!=null)12         {13             pw.println(line);14         }15         pw.close();16         br.close();17     }18 }

22.07 標準輸入輸出流概述和輸出語句的本質

標準輸入輸出流

public static final InputStream in“標準”輸入流。

public static final PrintStream out“標準”輸出流。

System類中的字段:in,out。

它們各代表了系統(tǒng)標準的輸入和輸出設備。默認輸入設備是鍵盤,輸出設備是顯示器。

System.in的類型是InputStream

System.out的類型是PrintStream

也就是說InputStream is = System.in; PrintStream ps = System.out;

System.out.println("helloworld");等價于下面的兩句話

PrintStream ps = System.out;

ps.println("helloworld");

22.08 三種方式實現(xiàn)鍵盤錄入

鍵盤錄入數(shù)據(jù):

1:main方法的args接收參數(shù)

java HelloWorld hello world java

2:Scanner(JDK5以后的)

Scanner sc = new Scanner(System.in);

String s = sc.nextLine();

int x = sc.nextInt()

3:通過字符緩沖流包裝標準輸入流實現(xiàn)

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

例:

 1 public class Practice  2 { 3     public static void main(String[] args) throws IOException 4     { 5         BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 6  7         System.out.println("請輸入一個字符串:"); 8         String line = br.readLine(); 9         System.out.println("你輸入的字符串是:" + line);10 11         System.out.println("請輸入一個整數(shù):");12         // int i = Integer.parseInt(br.readLine());13         line = br.readLine();14         int i = Integer.parseInt(line);15         System.out.println("你輸入的整數(shù)是:" + i);16     }17 }

22.09 輸出語句用字符緩沖流改進

 1 public class Practice  2 { 3     public static void main(String[] args) throws IOException 4     { 5         // OutputStream os = System.out; // 多態(tài) 6         // OutputStreamWriter osw = new OutputStreamWriter(os); 7         // BufferedWriter bw = new BufferedWriter(osw); 8         BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); 9 10         bw.write("hello");11         bw.newLine();12         // bw.flush();13         bw.write("world");14         bw.newLine();15         // bw.flush();16         bw.write("java");17         bw.newLine();18         bw.flush();19         20         bw.close();21     }22 }

22.10 隨機訪問流概述和寫出數(shù)據(jù)

RandomaccessFile概述:RandomAccessFile類不屬于流,是Object類的子類。

但它融合了InputStream和OutputStream的功能。支持對隨機訪問文件的讀取和寫入。

構造方法:

1.public RandomAccessFile(File file,String mode) throws FileNotFoundException

創(chuàng)建從中讀取和向其中寫入(可選)的隨機訪問文件流,該文件由 File 參數(shù)指定。將創(chuàng)建一個新的 FileDescriptor 對象來表示此文件的連接。

2.public RandomAccessFile(String name,String mode) throws FileNotFoundException

創(chuàng)建從中讀取和向其中寫入(可選)的隨機訪問文件流,該文件具有指定名稱。

例:

1 // 創(chuàng)建隨機訪問流對象2 //模式有四種,我們最常用的一種叫"rw",這種方式表示我既可以寫數(shù)據(jù),也可以讀取數(shù)據(jù) 3 RandomAccessFile raf = new RandomAccessFile("D://raf.txt", "rw");4 5 raf.writeInt(100);6 raf.writeChar('a');7 raf.writeUTF("中國");8 9 raf.close();

22.11 隨機訪問流讀取數(shù)據(jù)和操作文件指針

例:

 1 public class Practice  2 { 3     public static void main(String[] args) throws IOException 4     { 5 //        write(); 6         read(); 7     } 8     private static void read() throws IOException  9     {10         // 創(chuàng)建隨機訪問流對象11         RandomAccessFile raf = new RandomAccessFile("D://raf.txt", "rw");12 13         int i = raf.readInt();14         System.out.println(i);15         // 該文件指針可以通過 getFilePointer方法讀取,并通過 seek 方法設置。16         System.out.println("當前文件的指針位置是:" + raf.getFilePointer());//417 18         char ch = raf.readChar();19         System.out.println(ch);20         System.out.println("當前文件的指針位置是:" + raf.getFilePointer());//621 22         String s = raf.readUTF();23         System.out.println(s);24         //從當前文件指針開始讀取前兩個字節(jié)25         System.out.println("當前文件的指針位置是:" + raf.getFilePointer());//1426         System.out.println("-----");27         // 讀取a28         raf.seek(4);29         ch = raf.readChar();30         System.out.println(ch);//a31     }32 33     private static void write() throws IOException 34     {35         // 創(chuàng)建隨機訪問流對象36         //模式有四種,我們最常用的一種叫"rw",這種方式表示我既可以寫數(shù)據(jù),也可以讀取數(shù)據(jù) 37         RandomAccessFile raf = new RandomAccessFile("D://raf.txt", "rw");38 39         raf.writeInt(100);40         raf.writeChar('a');41         raf.writeUTF("中國");42 43         raf.close();44     }45 }

22.12 合并流讀取兩個文件的內容復制到一個文件中

SequenceInputStream概述:SequenceInputStream類可以將多個輸入流串流在一起,合并為一個輸入流,因此,該流也被稱為合并流。

SequenceInputStream的構造方法

public SequenceInputStream(InputStream s1,InputStream s2)

通過記住這兩個參數(shù)來初始化新創(chuàng)建的 SequenceInputStream(將按順序讀取這兩個參數(shù),先讀取 s1,然后讀取 s2),以提供從此 SequenceInputStream 讀取的字節(jié)。

例:

 1 public class Practice  2 { 3     public static void main(String[] args) throws IOException 4     { 5         InputStream s1 = new FileInputStream("D://a.txt"); 6         InputStream s2 = new FileInputStream("D://b.java"); 7         SequenceInputStream sis = new SequenceInputStream(s1, s2); 8         BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D://c.txt")); 9 10         // 讀寫11         byte[] bys = new byte[1024];12         int len = 0;13         while ((len = sis.read(bys)) != -1) 14         {15             bos.write(bys, 0, len);16         }17 18         bos.close();19         sis.close();20     }21 }

22.13 合并流讀取多個文件的內容復制到一個文件中

public SequenceInputStream(Enumeration<? extends InputStream> e)

通過記住參數(shù)來初始化新創(chuàng)建的 SequenceInputStream,該參數(shù)必須是生成運行時類型為 InputStream 對象的 Enumeration 型參數(shù)。將按順序讀取由該枚舉生成的輸入流,以提供從此 SequenceInputStream 讀取的字節(jié)。在用盡枚舉中的每個輸入流之后,將通過調用該流的 close 方法將其關閉。

例:

 1 public class Practice  2 { 3     public static void main(String[] args) throws IOException 4     { 5         Vector<InputStream> v = new Vector<InputStream>(); 6         InputStream s1 = new FileInputStream("D://a.txt"); 7         InputStream s2 = new FileInputStream("D://b.txt"); 8         InputStream s3 = new FileInputStream("D://c.txt"); 9         v.add(s1);10         v.add(s2);11         v.add(s3);12         Enumeration<InputStream> en = v.elements();13         SequenceInputStream sis = new SequenceInputStream(en);14         BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D://d.txt"));15 16         // 讀寫17         byte[] bys = new byte[1024];18         int len = 0;19         while ((len = sis.read(bys)) != -1) 20         {21             bos.write(bys, 0, len);22         }23 24         bos.close();25         sis.close();26     }27 }

22.14 序列化流和反序列化流的概述和使用

序列化流ObjectOutputStream:

ObjectOutputStream 將 Java 對象的基本數(shù)據(jù)類型和圖形寫入 OutputStream

反序列化流ObjectInputStream:

ObjectInputStream 對以前使用 ObjectOutputStream 寫入的基本數(shù)據(jù)和對象進行反序列化。

序列化流:把對象按照流一樣的方式存入文本文件或者在網(wǎng)絡中傳輸。對象 -- 流數(shù)據(jù)(ObjectOutputStream)

反序列化流:把文本文件中的流對象數(shù)據(jù)或者網(wǎng)絡中的流對象數(shù)據(jù)還原成對象。流數(shù)據(jù) -- 對象(ObjectInputStream)

例:

 1 public class Practice  2 { 3     public static void main(String[] args) throws IOException, ClassNotFoundException 4     { 5         //要對對象進行序列化,需要先自定義一個類 6         //序列化數(shù)據(jù)其實就是把對象寫到文本文件 7         //write(); 8         read(); 9     }10     private static void read() throws IOException, ClassNotFoundException11     {12         // 創(chuàng)建反序列化對象13         ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D://oos.txt"));14 15         // 還原對象16         Object obj = ois.readObject();17 18         // 釋放資源19         ois.close();20 21         // 輸出對象22         System.out.println(obj);23     }24 25     private static void write() throws IOException 26     {27         // 創(chuàng)建序列化流對象28         ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D://oos.txt"));29 30         // 創(chuàng)建對象31         Student p = new Student("旺財", 7);32 33         // public final void writeObject(Object obj)34         oos.writeObject(p);35 36         // 釋放資源37         oos.close();38     }39 }

注意:Student類必須實現(xiàn) java.io.Serializable 接口以啟用其序列化功能。未實現(xiàn)此接口的類將無法使其任何狀態(tài)序列化或反序列化。

22.15 如何解決序列化時候的黃色警告線問題

Person類實現(xiàn)了序列化接口,那么它本身也應該有一個標記值。

這個標記值假設是100。

開始:Student.class -- id=100 wirte數(shù)據(jù):oos.txt--id=100 read數(shù)據(jù):oos.txt--id=100

修改了Student類后

現(xiàn)在:Student.class -- id=200 wirte數(shù)據(jù):oos.txt--id=100 read數(shù)據(jù):oos.txt--id=100

當進行反序列化對象時會報錯

在實際開發(fā)中,可能還需要使用以前寫過的數(shù)據(jù),不能每次重新寫入。進行反序列化對象時會報錯原因是因為它們的id值不匹配。每次修改java文件的內容的時候,class文件的id值都會發(fā)生改變。而讀取文件的時候,會和class文件中的id值進行匹配。所以,就會出問題。

這時,可以讓這個id值在java文件中是一個固定的值,這樣,修改文件的時候,這個id值就不會發(fā)生改變

如下所示:

同時黃色警告線也標注此處有隱患,要想解決黃色警告線問題,就可以自動產生一個序列化id值。

而且產生這個值以后,對類進行任何改動,它讀取以前的數(shù)據(jù)是沒有問題的

生成的語句:private static final long serialVersionUID = 2413404678919778643L;

22.16 如何讓對象的成員變量不被序列化

當類中的成員變量不想進行序列化時,可以使用transient關鍵字聲明不需要序列化的成員變量

例:private transient int age;

這時當進行反序列化對象時,age的值是0

22.17 Properties的概述和作為Map集合的使用

Properties 類表示了一個持久的屬性集。Properties 可保存在流中或從流中加載。屬性列表中每個鍵及其對應值都是一個字符串。是Hashtable的子類,說明是一個Map集合

構造方法:

1.public Properties()

創(chuàng)建一個無默認值的空屬性列表。

2.public Properties(Properties defaults)

創(chuàng)建一個帶有指定默認值的空屬性列表。

例:

 1 public class Practice  2 { 3     public static void main(String[] args) throws IOException, ClassNotFoundException 4     { 5         // 作為Map集合的使用 6         // 下面這種用法是錯誤的,該類不是一個泛型類,在使用的時候就不能加泛型 7         // Properties<String, String> prop = new Properties<String, String>(); 8  9         Properties prop = new Properties();10 11         // 添加元素12         prop.put("it002", "hello");13         prop.put("it001", "world");14         prop.put("it003", "java");15 16         // System.out.println("prop:" + prop);17 18         // 遍歷集合19         Set<Object> set = prop.keySet();20         for (Object key : set) 21         {22             Object value = prop.get(key);23             System.out.println(key + "---" + value);24         }25     }26 }

22.18 Properties的特殊功能使用

1.public Object setProperty(String key,String value)

調用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。強制要求為屬性的鍵和值使用字符串。返回值是 Hashtable 調用 put 的結果。

2.public String getProperty(String key)

用指定的鍵在此屬性列表中搜索屬性。如果在此屬性列表中未找到該鍵,則接著遞歸檢查默認屬性列表及其默認值。如果未找到屬性,則此方法返回 null。

3.public Set<String> stringPropertyNames()

返回此屬性列表中的鍵集,其中該鍵及其對應值是字符串,如果在主屬性列表中未找到同名的鍵,則還包括默認屬性列表中不同的鍵。其鍵或值不是 String 類型的屬性被忽略。

例:

 1 public class Practice  2 { 3     public static void main(String[] args) throws IOException, ClassNotFoundException 4     { 5         // 創(chuàng)建集合對象 6         Properties prop = new Properties(); 7  8         // 添加元素 9         prop.setProperty("張三", "30");10         prop.setProperty("李四", "40");11         prop.setProperty("王五", "50");12 13         // 獲取所有的鍵的集合14         Set<String> set = prop.stringPropertyNames();15         for (String key : set) 16         {17             String value = prop.getProperty(key);18             System.out.println(key + "---" + value);19         }20     }21 }

22.19 Properties的load()和store()功能

1.public void load(Reader reader)throws IOException

按簡單的面向行的格式從輸入字符流中讀取屬性列表(鍵和元素對)。

2.public void store(Writer writer,String comments)throws IOException

以適合使用 load(Reader) 方法的格式,將此 Properties 表中的屬性列表(鍵和元素對)寫入輸出字符。

例:

 1 public class Practice  2 { 3     public static void main(String[] args) throws IOException 4     { 5         //myLoad(); 6         myStore(); 7     } 8     private static void myStore() throws IOException  9     {10         // 創(chuàng)建集合對象11         Properties prop = new Properties();12 13         prop.setProperty("小明", "27");14         prop.setProperty("小強", "30");15         prop.setProperty("旺財", "18");16         17         //把集合中的數(shù)據(jù)存儲到文件,集合必須是Properties集合18         Writer w = new FileWriter("D://name.txt");19         prop.store(w, "name");//name表示屬性列表的描述20         w.close();21     }22 23     private static void myLoad() throws IOException 24     {25         Properties prop = new Properties();26 27         // 把文件中的數(shù)據(jù)讀取到集合中,集合必須是Properties集合28         // 注意:這個文件的數(shù)據(jù)必須是鍵值對形式29         Reader r = new FileReader("D://name.txt");30         prop.load(r);31         r.close();32 33         System.out.println("prop:" + prop);34     }35 }

22.20 判斷文件中是否有指定的鍵如果有就修改值的案例

有一個文本文件(name.txt),數(shù)據(jù)是鍵值對形式的,但是不知道內容是什么。請寫一個程序判斷是否有“小紅”這樣的鍵存在,如果有就改變其值為”20”

分析:

A:把文件中的數(shù)據(jù)加載到集合中

B:遍歷集合,獲取得到每一個鍵

C:判斷鍵是否有為"小紅"的,如果有就修改其值為"20"

D:把集合中的數(shù)據(jù)重新存儲到文件中

 1 public class Practice  2 { 3     public static void main(String[] args) throws IOException 4     { 5         // 把文件中的數(shù)據(jù)加載到集合中 6         Properties prop = new Properties(); 7         Reader r = new FileReader("D://name.txt"); 8         prop.load(r); 9         r.close();10 11         // 遍歷集合,獲取得到每一個鍵12         Set<String> set = prop.stringPropertyNames();13         for (String key : set) 14         {15             // 判斷鍵是否有為"小紅"的,如果有就修改其值為"20"16             if ("小紅".equals(key)) 17             {18                 prop.setProperty(key, "20");19                 break;20             }21         }22 23         // 把集合中的數(shù)據(jù)重新存儲到文件中24         Writer w = new FileWriter("D://name.txt");25         prop.store(w, null);26         w.close();27     }28 }

22.21 讓猜數(shù)字小游戲只能玩5次案例

 1 public class Practice  2 { 3     public static void main(String[] args) throws IOException 4     { 5         // 把數(shù)據(jù)加載到集合中 6         Properties prop = new Properties(); 7         Reader r = new FileReader("D://count.txt"); 8         prop.load(r); 9         r.close();10 11         String value = prop.getProperty("count");12         int number = Integer.parseInt(value);13 14         if (number > 5) 15         {16             System.out.println("游戲試玩已結束,請付費");17             System.exit(0);18         } 19         else 20         {21             number++;22             prop.setProperty("count", String.valueOf(number));23             Writer w = new FileWriter("D://count.txt");24             prop.store(w, null);25             w.close();26             System.out.println("開始游戲");27         }28     }29 }

22.22 NIO的介紹和JDK7下NIO的一個案例

NIO其實就是新IO的意思。JDK4出現(xiàn)NIO。

新IO和傳統(tǒng)的IO有相同的目的,都是用于進行輸入輸出的,但新IO使用了不同的方式來處理輸入輸出,采用內存映射文件的方式,將文件或者文件的一段區(qū)域映射到內存中,就可以像訪問內存一樣的來訪問文件了,這種方式效率比舊IO要高很多,但是目前好多地方我們看到的還是舊IO的使用,所以我們仍以舊IO為主,知道NIO即可。

JDK4新IO要了解的類 Buffer(緩沖) Channer(通道)

JDK7的新IO類

1.Path:與平臺無關的路徑。

2.Paths:包含了返回Path的靜態(tài)方法。

  2.1 public staticPathget(URIuri):根據(jù)給定的URI來確定文件路徑。

3.Files:操作文件的工具類。提供了大量的方法,簡單了解如下方法

  3.1 public staticlongcopy(Pathsource, OutputStreamout):復制文件

  3.2 public staticPathwrite(Pathpath, Iterable<? extends CharSequence>lines, Charsetcs, OpenOption...options):把集合的數(shù)據(jù)寫到文件。

例:

 1 //復制文件 2  Files.copy(Paths.get("D://a.txt"), new FileOutputStream("D://aa.txt")); 3 //把集合中的數(shù)據(jù)寫到文件 4 public class Practice  5 { 6     public static void main(String[] args) throws IOException 7     { 8  9         ArrayList<String> array = new ArrayList<String>();10         array.add("hello");11         array.add("world");12         array.add("java");13         //以GBK編碼寫到文件14         Files.write(Paths.get("D://array.txt"), array, Charset.forName("GBK"));15     }16 }


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 萍乡市| 田东县| 利辛县| 阳山县| 清徐县| 南昌市| 石泉县| 祁东县| 韶关市| 长子县| 安岳县| 衡水市| 蛟河市| 宿松县| 定远县| 高邮市| 苏尼特左旗| 民勤县| 城步| 山东| 滨海县| 台中县| 江达县| 峨眉山市| 新河县| 昭平县| 固安县| 宁阳县| 乌拉特前旗| 大同县| 安徽省| 仁化县| 夏河县| SHOW| 邢台市| 黑河市| 彰化县| 富顺县| 汝州市| 阿城市| 离岛区|