亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

Java編程基礎(chǔ)23——IO(其他流)&Properties

vvpale / 2320人閱讀

摘要:但它融合了和的功能。支持對隨機(jī)訪問文件的讀取和寫入。的概述和作為集合的使用了解的概述類表示了一個(gè)持久的屬性集。可保存在流中或從流中加載。屬性列表中每個(gè)鍵及其對應(yīng)值都是一個(gè)字符串。

1_序列流(了解)

1.什么是序列流

序列流可以把多個(gè)字節(jié)輸入流整合成一個(gè), 從序列流中讀取數(shù)據(jù)時(shí), 將從被整合的第一個(gè)流開始讀, 讀完一個(gè)之后繼續(xù)讀第二個(gè), 以此類推.

2.使用方式

整合兩個(gè): 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);    //將兩個(gè)流整合成一個(gè)流
            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_序列流整合多個(gè)(了解)

整合多個(gè): 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 v = new Vector<>();                    //創(chuàng)建vector集合對象
        v.add(fis1);                                            //將流對象添加
        v.add(fis2);
        v.add(fis3);
        Enumeration 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_內(nèi)存輸出流*(掌握)

1.什么是內(nèi)存輸出流

該輸出流可以向內(nèi)存中寫數(shù)據(jù), 把內(nèi)存當(dāng)作一個(gè)緩沖區(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_內(nèi)存輸出流之面試題(掌握)

定義一個(gè)文件輸入流,調(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_隨機(jī)訪問流概述和讀寫數(shù)據(jù)(了解)

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

RandomAccessFile概述

RandomAccessFile類不屬于流,是Object類的子類。但它融合了InputStream和OutputStream的功能。

支持對隨機(jī)訪問文件的讀取和寫入。

B:read(),write(),seek()

6_對象操作流ObjecOutputStream)(了解)

1.什么是對象操作流

該流可以將一個(gè)對象寫出, 或者讀取一個(gè)對象到程序中. 也就是執(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();
                }
            
            }
7_對象操作流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();
                }
            
            }
8_對象操作流優(yōu)化)(了解)

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

    Person p1 = new Person("張三", 23);
    Person p2 = new Person("李四", 24);
    Person p3 = new Person("馬哥", 18);
    Person p4 = new Person("輝哥", 20);
    
    ArrayList 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();

讀取到的是一個(gè)集合對象

        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("f.txt"));
            ArrayList list = (ArrayList)ois.readObject();    //泛型在運(yùn)行期會被擦除,索引運(yùn)行期相當(dāng)于沒有泛型
        //想去掉黃色可以加注解            
        @SuppressWarnings("unchecked")
            for (Person person : list) {
                System.out.println(person);
            }
        
        ois.close();
9_加上id號)(了解)

注意

要寫出的對象必須實(shí)現(xiàn)Serializable接口才能被序列化

不用必須加id號

10_數(shù)據(jù)輸入輸出流)(了解)

1.什么是數(shù)據(jù)輸入輸出流

DataInputStream, DataOutputStream可以按照基本數(shù)據(jù)類型大小讀寫數(shù)據(jù)

例如按Long大小寫出一個(gè)數(shù)字, 寫出時(shí)該數(shù)據(jù)占8字節(jié). 讀取的時(shí)候也可以按照Long類型讀取, 一次讀取8個(gè)字節(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();
11_打印流的概述和特點(diǎn)(掌握)

1.什么是打印流

該流可以很方便的將對象的toString()結(jié)果輸出, 并且自動(dòng)加上換行, 而且可以使用自動(dòng)刷出的模式

System.out就是一個(gè)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()

自動(dòng)刷出: 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("你好");                //自動(dòng)刷出,只針對的是println方法
            pw.close();
12_標(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();
13_修改標(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();
14_兩種方式實(shí)現(xiàn)鍵盤錄入(了解)

A:BufferedReader的readLine方法。

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

B:Scanner

15_Properties的概述和作為Map集合的使用(了解)

A:Properties的概述

Properties 類表示了一個(gè)持久的屬性集。

Properties 可保存在流中或從流中加載。

屬性列表中每個(gè)鍵及其對應(yīng)值都是一個(gè)字符串。

B:案例演示

Properties作為Map集合的使用

16_Properties的特殊功能使用(了解)

A:Properties的特殊功能

public Object setProperty(String key,String value)

public String getProperty(String key)

public Enumeration stringPropertyNames()

B:案例演示

Properties的特殊功能

17_Properties的load()和store()功能(了解)

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

B:案例演示

Properties的load()和store()功能

18_day22總結(jié)

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://www.ezyhdfw.cn/yun/77325.html

相關(guān)文章

  • Java編程基礎(chǔ)22——IO(字符)&amp;字符其他內(nèi)容&amp;遞歸

    摘要:字符流字符流是什么字符流是可以直接讀寫字符的流字符流讀取字符就要先讀取到字節(jié)數(shù)據(jù)然后轉(zhuǎn)為字符如果要寫出字符需要把字符轉(zhuǎn)為字節(jié)再寫出類的方法可以按照字符大小讀取通過項(xiàng)目默認(rèn)的碼表一次讀取一個(gè)字符賦值給將讀到的字符強(qiáng)轉(zhuǎn)后打印字符流類的方法可以 1_字符流FileReader 1.字符流是什么 字符流是可以直接讀寫字符的IO流 字符流讀取字符, 就要先讀取到字節(jié)數(shù)據(jù), 然后轉(zhuǎn)為字符. ...

    BoYang 評論0 收藏0
  • 1、Properties集合 2、序列化與反序列化 3、打印 4、commons-IO

    摘要:集合的特點(diǎn)集合的特點(diǎn)類介紹類表示了一個(gè)持久的屬性集??杀4嬖诹髦谢驈牧髦屑虞d。屬性列表中每個(gè)鍵及其對應(yīng)值都是一個(gè)字符串特點(diǎn)的子類,集合中的方法都可以用。該集合沒有泛型。鍵值可以存儲到集合中,也可以存儲到持久化的設(shè)備硬盤盤光盤上。 01Properties集合的特點(diǎn) * A: Properties集合的特點(diǎn) * a: Properties類介紹 * Propert...

    aboutU 評論0 收藏0
  • 高薪程序員&amp;amp;面試題精講系列22之說說JavaIO,常用哪些IO?

    摘要:一面試題及剖析今日面試題今天壹哥帶各位復(fù)習(xí)一塊可能會令初學(xué)者比較頭疼的內(nèi)容,起碼當(dāng)時(shí)讓我很有些頭疼的內(nèi)容,那就是流。在這里壹哥會從兩部分展開介紹流,即與流。除此之外盡量使用字節(jié)流。關(guān)閉此輸入流并釋放與流相關(guān)聯(lián)的任何系統(tǒng)資源。 一. 面試題及剖析 1. 今日面試題 今天 壹哥 帶各位復(fù)習(xí)一塊可...

    fnngj 評論0 收藏0
  • 《 Kotlin + Spring Boot : 下一代 Java 服務(wù)端開發(fā) 》

    摘要:下一代服務(wù)端開發(fā)下一代服務(wù)端開發(fā)第部門快速開始第章快速開始環(huán)境準(zhǔn)備,,快速上手實(shí)現(xiàn)一個(gè)第章企業(yè)級服務(wù)開發(fā)從到語言的缺點(diǎn)發(fā)展歷程的缺點(diǎn)為什么是產(chǎn)生的背景解決了哪些問題為什么是的發(fā)展歷程容器的配置地獄是什么從到下一代企業(yè)級服務(wù)開發(fā)在移動(dòng)開發(fā)領(lǐng)域 《 Kotlin + Spring Boot : 下一代 Java 服務(wù)端開發(fā) 》 Kotlin + Spring Boot : 下一代 Java...

    springDevBird 評論0 收藏0
  • Java Socket編程之常識網(wǎng)絡(luò)基礎(chǔ)知識

    摘要:如地址端口號組成了所謂的,是網(wǎng)絡(luò)上運(yùn)行的程序之間雙向通信鏈路的終結(jié)點(diǎn),是和的基礎(chǔ)套接字網(wǎng)絡(luò)上具有唯一標(biāo)識的地址和端口組合在一起才能構(gòu)成唯一能識別的標(biāo)識符套接字。 更多物聯(lián)網(wǎng)高并發(fā)編程知識請移步:https://www.yuque.com/shizhiy... Java Socket編程之常識網(wǎng)絡(luò)基礎(chǔ)知識 網(wǎng)絡(luò)基礎(chǔ)知識(參考計(jì)算機(jī)網(wǎng)絡(luò))? 《TCP/IP協(xié)議棧及OSI參考模型詳解》? ...

    Jeff 評論0 收藏0

發(fā)表評論

0條評論

最新活動(dòng)
閱讀需要支付1元查看
<