day22 Java学习 IO流(序列流)

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()第二个参数是用来描述文件列表的,如果不描述可以传NULL
         System.out.println("添加后:"+p);
    }
例子
原文地址:https://www.cnblogs.com/feng0001/p/10965452.html