day22 Java學習 IO流(序列流)

IO流(序列流)數組

     序列流:dom

                  * 能夠把多個字節輸入流整合成一個,從序列流中讀取數據時,將從被整合的第一個流開始讀,讀完一個以後繼續讀第二個。ide

    整合方式:優化

                  * Seq uenceInputStream ( InputStream ,InputStream )spa

 

IO流(序列流整合多個)3d

    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流(內存輸出流)code

  內存輸出流:對象

               * 該輸出能夠向內存中寫數據,把內存看成一個緩衝區,寫出以後能夠一次性獲取出全部數據。blog

    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()第二個參數是用來描述文件列表的,若是不描述能夠傳NULL
         System.out.println("添加後:"+p);
    }
例子
相關文章
相關標籤/搜索