IO流(序列流)
? ? ?序列流:
? ? ? ? ? ? ? ? ? * 可以把多個字節輸入流整合成一個,從序列流中讀取數據時,將從被整合的第一個流開始讀,讀完一個之后繼續讀第二個。
? ? 整合方式:
? ? ? ? ? ? ? ? ? * Seq uenceInputStream ( InputStream ,InputStream )
?
IO流(序列流整合多個)


public static void main(String[] args) throws IOException {FileInputStream fis1 = new FileInputStream("test1.txt");FileInputStream fis2 = new FileInputStream("test2.txt");FileInputStream fis3 = new FileInputStream("test.txt");Vector<InputStream> v = new Vector<>();v.add(fis1);v.add(fis2);v.add(fis3);Enumeration<InputStream> en = v.elements();SequenceInputStream sis = new SequenceInputStream(en);FileOutputStream fos = new FileOutputStream("list.txt");int b;while ((b = sis.read()) != -1) {fos.write(b);}sis.close();fos.close();}
?
IO流(內存輸出流)
??內存輸出流:
? ? ? ? ? ? ? ?* 該輸出可以向內存中寫數據,把內存當作一個緩沖區,寫出之后可以一次性獲取出所有數據。


public static void main(String[] args) throws IOException {FileInputStream fis1 = new FileInputStream("test1.txt");ByteArrayOutputStream by = new ByteArrayOutputStream(); // 在內存中創建了可以增長的內存數組int b;while ((b = fis1.read()) != -1) { // 將讀到的數據逐個寫到內存中 by.write(b);}System.out.println(by); // 將緩沖區的內容轉換成了字符串 fis1.close();}
?
IO流(隨機訪問流和讀寫數據)
? ?隨機訪問流:
? ? ? ? ? ? ? ? ? ? ? * RandomAccessFile類不屬于流,是Object類的子類,但他融合了InputStream和OutputStream的功能。支持對隨機訪問文件的讀取和寫入。
? ? seek():設置指定位置讀和寫
?
IO流(對象操作流 )
? ?對象操作流?:該流可以將一個對象寫出。或者讀取一個對象到程序中,也就是執行了序列化和反序列化的操作。? ? ? ?
? ?使用方式:
? ? ? ? ? ? ? ?new?ObjectOutputStream(OutputStream),writeObject( )。?
? ? ? ? ? ? ? ?new?ObjectInputStream(OutputStream)? ? 對象輸入流(反序列化)
?
IO流(對象操作流優化)
? ? ?


public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {Person p1 = new Person("張三", 14);Person p2 = new Person("李四", 15);Person p3 = new Person("王五", 16);ArrayList<Person> list = new ArrayList<>(); list.add(p1);list.add(p2);list.add(p3);ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("e.txt"));oos.writeObject(list);//將整個集合一次寫出 oos.close();ObjectInputStream ois = new ObjectInputStream(new FileInputStream("e.txt"));ArrayList<Person> list1 = (ArrayList<Person>) ois.readObject(); //將整個集合一次讀取for (Person person : list1) {System.out.println(person);}ois.close();}
?
IO流(數據輸入輸出流)
? ??數據輸入輸出流:
? ? ? ? ? ? ? *?DataInputStream,DataOutputStream可以按照基本數據類型大小讀寫數據。
? ? 使用方式:


public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {DataOutputStream dos=new DataOutputStream(new FileOutputStream("g.txt"));dos.writeInt(997);dos.writeInt(998);dos.writeInt(999);dos.close();DataInputStream dis=new DataInputStream(new FileInputStream("g.txt"));int a=dis.readInt();int b=dis.readInt();int c=dis.readInt();System.out.println(a);System.out.println(b);System.out.println(c);}
?
?IO流(Properties概述和作為Map集合的使用)
? ? ??Properties的概述:
? ? ? ? ? ? ? ? ? ? ?*?Properties是Hashtable的子類。
? ? ? ? ? ? ? ? ? ? ?*?Properties類表示了一個持久的屬性集。
? ? ? ? ? ? ? ? ? ? ?*?Properties可保存在流中或從流中加載。
? ? ? ? ? ? ? ? ? ? ?* 屬性列表中每個鍵及其對象值都是一個字符串。


public static void main(String[] args) {Properties p= new Properties();p.put("a", 1);System.out.println(p);System.out.println(p.get("a"));}
?
??IO流(Properties的特殊功能使用)
? ? ? * public Object setProperty( String key ,Sring value) :設置鍵和值
? ? ? * public String? getProperty( String key) :根據鍵來獲取值
? ? ? * public?Enumeration <string>? ?:迭代? ??stringPropertyNames():拿到所有的鍵?


public static void main(String[] args) {Properties p= new Properties();p.setProperty("a", "2");p.setProperty("b", "3");Enumeration<String> en= (Enumeration<String>) p.propertyNames();while (en.hasMoreElements()) {String key=en.nextElement();String value=p.getProperty(key);System.out.println(key+"="+value);}}
?
IO流(Properties的load( )和Store ( ))
? ?


public static void main(String[] args) throws FileNotFoundException, IOException {Properties p= new Properties();System.out.println("讀取前:"+p);p.load(new FileInputStream("a.txt")); //load()讀取文件System.out.println("讀取后:"+p);p.setProperty("name", "lisi");p.store(new FileOutputStream("a.txt"), null); //store()第二個參數是用來描述文件列表的,如果不描述可以傳NULLSystem.out.println("添加后:"+p);}
?