Java IO其他流

1、IO流(序列流)(了解)

  • 1.什么是序列流
    • 序列流可以把多個字節(jié)輸入流整合成一個, 從序列流中讀取數(shù)據(jù)時, 將從被整合的第一個流開始讀, 讀完一個之后繼續(xù)讀第二個, 以此類推.
  • 2.使用方式
    • 整合兩個: SequenceInputStream(InputStream, InputStream)
    •   FileInputStream fis1 = new FileInputStream("a.txt");            //創(chuàng)建輸入流對象,關(guān)聯(lián)a.txt
        FileInputStream fis2 = new FileInputStream("b.txt");            //創(chuàng)建輸入流對象,關(guān)聯(lián)b.txt
        SequenceInputStream sis = new SequenceInputStream(fis1, fis2);    //將兩個流整合成一個流
        FileOutputStream fos = new FileOutputStream("c.txt");            //創(chuàng)建輸出流對象,關(guān)聯(lián)c.txt
        
        int b;
        while((b = sis.read()) != -1) {                                    //用整合后的讀
            fos.write(b);                                                //寫到指定文件上
        }
        
        sis.close();
        fos.close(); 
      

2寥枝、IO流(序列流整合多個)(了解)

  • 整合多個: SequenceInputStream(Enumeration)
  •   FileInputStream fis1 = new FileInputStream("a.txt");    //創(chuàng)建輸入流對象,關(guān)聯(lián)a.txt
      FileInputStream fis2 = new FileInputStream("b.txt");    //創(chuàng)建輸入流對象,關(guān)聯(lián)b.txt
      FileInputStream fis3 = new FileInputStream("c.txt");    //創(chuàng)建輸入流對象,關(guān)聯(lián)c.txt
      Vector<InputStream> v = new Vector<>();                    //創(chuàng)建vector集合對象
      v.add(fis1);                                            //將流對象添加
      v.add(fis2);
      v.add(fis3);
      Enumeration<InputStream> en = v.elements();                //獲取枚舉引用
      SequenceInputStream sis = new SequenceInputStream(en);    //傳遞給SequenceInputStream構(gòu)造
      FileOutputStream fos = new FileOutputStream("d.txt");
      int b;
      while((b = sis.read()) != -1) {
          fos.write(b);
      }
    
      sis.close();
      fos.close();
    

3芒涡、IO流(內(nèi)存輸出流*****)(掌握)

  • 1.什么是內(nèi)存輸出流
    • 該輸出流可以向內(nèi)存中寫數(shù)據(jù), 把內(nèi)存當(dāng)作一個緩沖區(qū), 寫出之后可以一次性獲取出所有數(shù)據(jù)
  • 2.使用方式
    • 創(chuàng)建對象: new ByteArrayOutputStream()
    • 寫出數(shù)據(jù): write(int), write(byte[])
    • 獲取數(shù)據(jù): toByteArray()
    •   FileInputStream fis = new FileInputStream("a.txt");
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int b;
        while((b = fis.read()) != -1) {
            baos.write(b);
        }
        
        //byte[] newArr = baos.toByteArray();                //將內(nèi)存緩沖區(qū)中所有的字節(jié)存儲在newArr中
        //System.out.println(new String(newArr));
        System.out.println(baos);
        fis.close();
      

4稀蟋、IO流(內(nèi)存輸出流之黑馬面試題)(掌握)

  • 定義一個文件輸入流,調(diào)用read(byte[] b)方法,將a.txt文件中的內(nèi)容打印出來(byte數(shù)組大小限制為5)
  •       FileInputStream fis = new FileInputStream("a.txt");                //創(chuàng)建字節(jié)輸入流,關(guān)聯(lián)a.txt
          ByteArrayOutputStream baos = new ByteArrayOutputStream();        //創(chuàng)建內(nèi)存輸出流
          byte[] arr = new byte[5];                                        //創(chuàng)建字節(jié)數(shù)組,大小為5
          int len;
          while((len = fis.read(arr)) != -1) {                            //將文件上的數(shù)據(jù)讀到字節(jié)數(shù)組中
              baos.write(arr, 0, len);                                    //將字節(jié)數(shù)組的數(shù)據(jù)寫到內(nèi)存緩沖區(qū)中
          }
          System.out.println(baos);                                        //將內(nèi)存緩沖區(qū)的內(nèi)容轉(zhuǎn)換為字符串打印
          fis.close();
    

5、IO流(對象操作流ObjecOutputStream)(了解)

  • 1.什么是對象操作流
    • 該流可以將一個對象寫出, 或者讀取一個對象到程序中. 也就是執(zhí)行了序列化和反序列化的操作.
  • 2.使用方式
    • 寫出: new ObjectOutputStream(OutputStream), writeObject()

        public class Demo3_ObjectOutputStream {
      
            /**
             * @param args
             * @throws IOException 
             * 將對象寫出,序列化
             */
            public static void main(String[] args) throws IOException {
                Person p1 = new Person("張三", 23);
                Person p2 = new Person("李四", 24);
        //        FileOutputStream fos = new FileOutputStream("e.txt");
        //        fos.write(p1);
        //        FileWriter fw = new FileWriter("e.txt");
        //        fw.write(p1);
                //無論是字節(jié)輸出流,還是字符輸出流都不能直接寫出對象
                ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("e.txt"));//創(chuàng)建對象輸出流
                oos.writeObject(p1);
                oos.writeObject(p2);
                oos.close();
            }
        
        }
      

22.06_IO流(對象操作流ObjectInputStream)(了解)

  • 讀取: 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();
            }
        
        }
      

22.07_IO流(對象操作流優(yōu)化)(了解)

* 將對象存儲在集合中寫出

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();    //泛型在運(yùn)行期會被擦除,索引運(yùn)行期相當(dāng)于沒有泛型
                                                                          //想去掉黃色可以加注解                    @SuppressWarnings("unchecked")
          for (Person person : list) {
              System.out.println(person);
          }
      
      ois.close();
    

8、_IO流(加上id號)(了解)

  • 注意
    • 要寫出的對象必須實(shí)現(xiàn)Serializable接口才能被序列化
    • 不用必須加id號

9、IO流(打印流的概述和特點(diǎn))(掌握)

  • 1.什么是打印流
    • 該流可以很方便的將對象的toString()結(jié)果輸出, 并且自動加上換行, 而且可以使用自動刷出的模式

    • System.out就是一個PrintStream, 其默認(rèn)向控制臺輸出信息

        PrintStream ps = System.out;
        ps.println(97);                    //其實(shí)底層用的是Integer.toString(x),將x轉(zhuǎn)換為數(shù)字字符串打印
        ps.println("xxx");
        ps.println(new Person("張三", 23));
        Person p = null;
        ps.println(p);                    //如果是null,就返回null,如果不是null,就調(diào)用對象的toString()
      
  • 2.使用方式
    • 打印: print(), println()

    • 自動刷出: PrintWriter(OutputStream out, boolean autoFlush, String encoding)

    • 打印流只操作數(shù)據(jù)目的

        PrintWriter pw = new PrintWriter(new FileOutputStream("g.txt"), true);
        pw.write(97);
        pw.print("大家好");
        pw.println("你好");                //自動刷出,只針對的是println方法
        pw.close();
      

10帆疟、IO流(標(biāo)準(zhǔn)輸入輸出流概述和輸出語句)

  • 1.什么是標(biāo)準(zhǔn)輸入輸出流(掌握)
    • System.in是InputStream, 標(biāo)準(zhǔn)輸入流, 默認(rèn)可以從鍵盤輸入讀取字節(jié)數(shù)據(jù)
    • System.out是PrintStream, 標(biāo)準(zhǔn)輸出流, 默認(rèn)可以向Console中輸出字符和字節(jié)數(shù)據(jù)
  • 2.修改標(biāo)準(zhǔn)輸入輸出流(了解)
    • 修改輸入流: System.setIn(InputStream)
    • 修改輸出流: System.setOut(PrintStream)
    •   System.setIn(new FileInputStream("a.txt"));                //修改標(biāo)準(zhǔn)輸入流
        System.setOut(new PrintStream("b.txt"));                //修改標(biāo)準(zhǔn)輸出流
        
        InputStream in = System.in;                                //獲取標(biāo)準(zhǔn)輸入流
        PrintStream ps = System.out;                            //獲取標(biāo)準(zhǔn)輸出流
        int b;
        while((b = in.read()) != -1) {                            //從a.txt上讀取數(shù)據(jù)
            ps.write(b);                                        //將數(shù)據(jù)寫到b.txt上
        }
        
        in.close();
        ps.close();
      

11、IO流(修改標(biāo)準(zhǔn)輸入輸出流拷貝圖片)(了解)

    System.setIn(new FileInputStream("IO圖片.png"));        //改變標(biāo)準(zhǔn)輸入流
    System.setOut(new PrintStream("copy.png"));         //改變標(biāo)準(zhǔn)輸出流
    
    InputStream is = System.in;                            //獲取標(biāo)準(zhǔn)輸入流
    PrintStream ps = System.out;                        //獲取標(biāo)準(zhǔn)輸出流
    
    int len;
    byte[] arr = new byte[1024 * 8];
    
    while((len = is.read(arr)) != -1) {
        ps.write(arr, 0, len);
    }
    
    is.close();
    ps.close();

12宇立、IO流(兩種方式實(shí)現(xiàn)鍵盤錄入)(了解)

  • A:BufferedReader的readLine方法踪宠。
    • BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  • B:Scanner

13、IO流(隨機(jī)訪問流概述和讀寫數(shù)據(jù))(了解)

  • A:隨機(jī)訪問流概述

    • RandomAccessFile概述
    • RandomAccessFile類不屬于流泄伪,是Object類的子類殴蓬。但它融合了InputStream和OutputStream的功能。
    • 支持對隨機(jī)訪問文件的讀取和寫入蟋滴。
  • B:read(),write(),seek()

14染厅、IO流(數(shù)據(jù)輸入輸出流)(了解)

  • 1.什么是數(shù)據(jù)輸入輸出流
    • DataInputStream, DataOutputStream可以按照基本數(shù)據(jù)類型大小讀寫數(shù)據(jù)
    • 例如按Long大小寫出一個數(shù)字, 寫出時該數(shù)據(jù)占8字節(jié). 讀取的時候也可以按照Long類型讀取, 一次讀取8個字節(jié).
  • 2.使用方式
    • DataOutputStream(OutputStream), writeInt(), writeLong()

        DataOutputStream dos = new DataOutputStream(new FileOutputStream("b.txt"));
        dos.writeInt(997);
        dos.writeInt(998);
        dos.writeInt(999);
        
        dos.close();
      
    • DataInputStream(InputStream), readInt(), readLong()

        DataInputStream dis = new DataInputStream(new FileInputStream("b.txt"));
        int x = dis.readInt();
        int y = dis.readInt();
        int z = dis.readInt();
        System.out.println(x);
        System.out.println(y);
        System.out.println(z);
        dis.close();
      

15洁灵、流(Properties的概述和作為Map集合的使用)(了解)

  • A:Properties的概述
    • Properties 類表示了一個持久的屬性集蚓土。
    • Properties 可保存在流中或從流中加載。
    • 屬性列表中每個鍵及其對應(yīng)值都是一個字符串丛晦。
  • B:案例演示
    • Properties作為Map集合的使用

16尔苦、IO流(Properties的特殊功能使用)(了解)

  • A:Properties的特殊功能
    • public Object setProperty(String key,String value)
    • public String getProperty(String key)
    • public Enumeration<String> stringPropertyNames()
  • B:案例演示
    • Properties的特殊功能

17涩馆、IO流(Properties的load()和store()功能)(了解)

  • A:Properties的load()和store()功能

  • B:案例演示

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

    `

      Properties pro = new Properties();
    
      pro.load(new FileInputStream("config.properties"));
    
      pro.store(new FileOutputStream("config.properties"),null);
    

    文件名config.properties

      age=12  
    
      tel=13512345678  
    
      userName=zhangsan  
    

`

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末行施,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子魂那,更是在濱河造成了極大的恐慌蛾号,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,000評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件涯雅,死亡現(xiàn)場離奇詭異鲜结,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)活逆,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,745評論 3 399
  • 文/潘曉璐 我一進(jìn)店門精刷,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人蔗候,你說我怎么就攤上這事怒允。” “怎么了锈遥?”我有些...
    開封第一講書人閱讀 168,561評論 0 360
  • 文/不壞的土叔 我叫張陵纫事,是天一觀的道長。 經(jīng)常有香客問我迷殿,道長儿礼,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,782評論 1 298
  • 正文 為了忘掉前任庆寺,我火速辦了婚禮蚊夫,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘懦尝。我一直安慰自己知纷,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,798評論 6 397
  • 文/花漫 我一把揭開白布陵霉。 她就那樣靜靜地躺著琅轧,像睡著了一般。 火紅的嫁衣襯著肌膚如雪踊挠。 梳的紋絲不亂的頭發(fā)上乍桂,一...
    開封第一講書人閱讀 52,394評論 1 310
  • 那天,我揣著相機(jī)與錄音效床,去河邊找鬼睹酌。 笑死,一個胖子當(dāng)著我的面吹牛剩檀,可吹牛的內(nèi)容都是我干的憋沿。 我是一名探鬼主播,決...
    沈念sama閱讀 40,952評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼沪猴,長吁一口氣:“原來是場噩夢啊……” “哼辐啄!你這毒婦竟也來了采章?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,852評論 0 276
  • 序言:老撾萬榮一對情侶失蹤壶辜,失蹤者是張志新(化名)和其女友劉穎悯舟,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體士复,經(jīng)...
    沈念sama閱讀 46,409評論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡图谷,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,483評論 3 341
  • 正文 我和宋清朗相戀三年翩活,在試婚紗的時候發(fā)現(xiàn)自己被綠了阱洪。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,615評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡菠镇,死狀恐怖冗荸,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情利耍,我是刑警寧澤蚌本,帶...
    沈念sama閱讀 36,303評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站隘梨,受9級特大地震影響程癌,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜轴猎,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,979評論 3 334
  • 文/蒙蒙 一嵌莉、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧捻脖,春花似錦锐峭、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,470評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至矛渴,卻和暖如春椎扬,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背具温。 一陣腳步聲響...
    開封第一講書人閱讀 33,571評論 1 272
  • 我被黑心中介騙來泰國打工蚕涤, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人桂躏。 一個月前我還...
    沈念sama閱讀 49,041評論 3 377
  • 正文 我出身青樓钻趋,卻偏偏與公主長得像,于是被迫代替她去往敵國和親剂习。 傳聞我的和親對象是個殘疾皇子蛮位,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,630評論 2 359

推薦閱讀更多精彩內(nèi)容