java IO流 5、

    public static void demo1() throws FileNotFoundException, IOException {
        FileInputStream fis1 = new FileInputStream("a.txt");        //建立字節輸入流關聯a.txt
        FileOutputStream fos = new FileOutputStream("c.txt");        //建立字節輸出流關聯c.txt        
        
        int b1;
        while((b1 = fis1.read()) != -1) {                            //不斷的在a.txt上讀取字節
            fos.write(b1);                                            //將讀到的字節寫到c.txt上
        }
        fis1.close();                                                //關閉字節輸入流
        
        FileInputStream fis2 = new FileInputStream("b.txt");
        int b2;
        while((b2 = fis2.read()) != -1) {
            fos.write(b2);
        }
        
        fis2.close();
        fos.close();
    }

FileOutputStream fos = new FileOutputStream("c.txt"); //建立字節輸出流關聯c.txt

fileoutputstream從新開啓會形成文本清空 ,fileinputstream不會形成文本清空
===============================================================================================================================================




序列流

1.什麼是序列流
* 序列流能夠把多個字節輸入流整合成一個, 從序列流中讀取數據時, 將從被整合的第一個流開始讀, 讀完一個以後繼續讀第二個, 以此類推.數組

 

 2.使用方式
* 整合兩個: SequenceInputStream(InputStream, InputStream)spa

* 整合多個: SequenceInputStream(Enumeration)code

 

 
    public static void demo2() throws FileNotFoundException, IOException {
        FileInputStream fis1 = new FileInputStream("a.txt");
        FileInputStream fis2 = new FileInputStream("b.txt");
        SequenceInputStream sis = new SequenceInputStream(fis1, fis2);
        FileOutputStream fos = new FileOutputStream("c.txt");
        
        int b;
        while((b = sis.read()) != -1) {
            fos.write(b);
        }
        
        sis.close();                    //sis在關閉的時候,會將構造方法中傳入的流對象也都關閉
        fos.close();
    }

 


使用枚舉添加多個fileinputstream
    /**
     * @param args
     * 整合兩個輸入流
     * SequenceInputStream(InputStream s1, InputStream s2)
     * 整合多個輸入流
     * SequenceInputStream(Enumeration<? extends InputStream> e)
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        //demo1();
        //demo2();
        FileInputStream fis1 = new FileInputStream("a.txt");
        FileInputStream fis2 = new FileInputStream("b.txt");
        FileInputStream fis3 = new FileInputStream("c.txt");
        
        Vector<FileInputStream> v = new Vector<>();                    //建立集合對象
        v.add(fis1);                                                //將流對象存儲進來
        v.add(fis2);
        v.add(fis3);
        
        Enumeration<FileInputStream> en = v.elements();
        SequenceInputStream sis = new SequenceInputStream(en);        //將枚舉中的輸入流整合成一個
        FileOutputStream fos = new FileOutputStream("d.txt");
        
        int b;
        while((b = sis.read()) != -1) {
            fos.write(b);
        }
        
        sis.close();
        fos.close();
    }
 

 

 

內存輸出流
* 1.什麼是內存輸出流
* 該輸出流能夠向內存中寫數據, 把內存看成一個緩衝區, 寫出以後能夠一次性獲取出全部數據
* 2.使用方式
* 建立對象: new ByteArrayOutputStream()
* 寫出數據: write(int), write(byte[])
* 獲取數據: toByteArray()
*對象

 
    /**
     * @param args
     * ByteArrayOutputStream
     * 內存輸出流
     * 
     * FileInputStream讀取中文的時候出現了亂碼
     * 
     * 解決方案
     * 1,字符流讀取
     * 2,ByteArrayOutputStream
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        //demo1();
        FileInputStream fis = new FileInputStream("e.txt");
        ByteArrayOutputStream baos = new ByteArrayOutputStream();            //在內存中建立了能夠增加的內存數組
        
        int b;
        while((b = fis.read()) != -1) {
            baos.write(b);                                                    //將讀取到的數據逐個寫到內存中
        }
        
        //byte[] arr = baos.toByteArray();                                    //將緩衝區的數據所有獲取出來,並賦值給arr數組
        //System.out.println(new String(arr));
        
        System.out.println(baos.toString());                                //將緩衝區的內容轉換爲了字符串,在輸出語句中能夠省略調用toString方法
        fis.close();
    }

    public static void demo1() throws FileNotFoundException, IOException {
        FileInputStream fis = new FileInputStream("e.txt");
        byte[] arr = new byte[3];
        int len;
        while((len = fis.read(arr)) != -1) {
            System.out.println(new String(arr,0,len));
        }
        
        fis.close();
    }
 

 


===========================================================================================

對象操做流ObjectInputStream

首先對象要先實現
Serializable接口
 
public class person implements  Serializable{

private String name;
private int age;
public person() {
super();

}

 

 
* 讀取: new ObjectInputStream(InputStream), readObject()
    * 
            public class Demo3_ObjectInputStream {

                /**
                 * @param args
                 * @throws IOException 
                 * @throws ClassNotFoundException 
                 * @throws FileNotFoundException 
                 * 讀取對象,反序列化
                 */
                public static void main(String[] args) throws IOException, ClassNotFoundException {
                    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("e.txt"));
                    Person p1 = (Person) ois.readObject();
                    Person p2 = (Person) ois.readObject();
                    System.out.println(p1);
                    System.out.println(p2);
                    ois.close();
                }
            
            }
 

 

* 將對象存儲在集合中寫出
    Person p1 = new Person("張三", 23);
    Person p2 = new Person("李四", 24);
    Person p3 = new Person("馬哥", 18);
    Person p4 = new Person("輝哥", 20);
    
    ArrayList<Person> list = new ArrayList<>();
    list.add(p1);
    list.add(p2);
    list.add(p3);
    list.add(p4);
    
    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("e.txt"));
    oos.writeObject(list);                                    //將對象信息寫入到文本中
    
    oos.close();
 
    ObjectInputStream is = new ObjectInputStream(new FileInputStream("e.txt"));
        ArrayList<person> list = (ArrayList<person>) is.readObject();
        for (person s1 : list) {
            System.out.println(s1);   //將對象信息寫出到控制檯
        }

        is.close();




* 將對象存儲在集合中寫出 Person p1 = new Person("張三", 23); Person p2 = new Person("李四", 24); Person p3 = new Person("馬哥", 18); Person p4 = new Person("輝哥", 20); ArrayList<Person> list = new ArrayList<>(); list.add(p1); list.add(p2); list.add(p3); list.add(p4); ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("f.txt")); oos.writeObject(list); //寫出集合對象 oos.close();


* 讀取到的是一個集合對象 ObjectInputStream ois = new ObjectInputStream(new FileInputStream("f.txt")); ArrayList<Person> list = (ArrayList<Person>)ois.readObject(); //泛型在運行期會被擦除,索引運行期至關於沒有泛型 //想去掉黃色能夠加註解 @SuppressWarnings("unchecked") for (Person person : list) { System.out.println(person); } ois.close();

 



加上id號   好處在於若是類 好比新增  gender屬性,沒有先存檔就直接讀檔   就顯示ID信息不正確
* 注意
* 要寫出的對象必須實現Serializable接口才能被序列化
* 不用必須加id號blog

 

public class Person implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 2L;
    private String name;
    private int age;
相關文章
相關標籤/搜索