Java基础二十一

时间:2021-02-10 01:33:53

1 操作基本数据类型的流

1.1 概述

  • 数据输出流允许应用程序以适当的方式将基本类型的流写入到输出流中,然后,应用程序可以使用数据输入流将数据读入。

1.2 应用

  • 示例:
package com.xuweiwei;

import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class DataOutputStreamTest {
    public static void main(String[] args) throws IOException {
        DataOutputStream dos = new DataOutputStream(new FileOutputStream("dos.txt"));

        dos.writeByte(2);
        dos.writeShort(2);
        dos.writeInt(2);
        dos.writeLong(2);

        dos.writeBoolean(true);
        dos.writeChar('a');

        dos.writeFloat(3.14f);
        dos.writeDouble(3.14);


        dos.close();

    }
}

 

  • 示例:
package com.xuweiwei;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;

public class DataInputStreamTest {
    public static void main(String[] args) throws IOException {
        DataInputStream dis = new DataInputStream(new FileInputStream("dos.txt"));

        byte b = dis.readByte();
        System.out.println("b = " + b);

        short i = dis.readShort();
        System.out.println("i = " + i);

        int i1 = dis.readInt();
        System.out.println("i1 = " + i1);

        long l = dis.readLong();
        System.out.println("l = " + l);

        char c = dis.readChar();
        System.out.println("c = " + c);

        float v = dis.readFloat();
        System.out.println("v = " + v);

        double v1 = dis.readDouble();
        System.out.println("v1 = " + v1);


        dis.close();


    }
}

 

2 内存操作流

2.1 内存操作流的概述

  • 内存操作流一般用于处理临时信息,因为临时信息不用保存,使用后就可以删除了。

2.2 内存操作流的应用

  • 示例:
package com.xuweiwei;


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * 内存操作流:用于处理临时存储信息的,程序结束,数据就从内存中消失。
 * 字节数组:
 *      ByteArrayOutputStream
 *      ByteArrayInputStream
 * 字符数组:
 *      CharArrayReader
 *      CharArrayWriter
 * 字符串:
 *      StringReader
 *      StringWriter
 */
public class ByteArrayStream {
    public static void main(String[] args) throws IOException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        bos.write("hello".getBytes());

        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());

        byte[] buffer = new byte[1024];
        int len = 0;
        while(-1 != (len = bis.read(buffer))){
            System.out.println(new String(buffer,0,len));
        }



    }
}

 

3 打印流

3.1 打印流的概述

  • 字节流打印流,PrintStream
  • 字符打印流,PrintWriter

3.2 打印流的特点

  • 只能操作母的地,不能操作数据。
  • 可与操作任意数据类型的数据。
  • 如果启动了自动刷新,能够自动刷新。
  • 打印流可以直接操作文本文件的。

3.3 打印流的应用

  • 示例:
package com.xuweiwei;

import java.io.FileNotFoundException;
import java.io.PrintWriter;

public class PrintWriterDemo {
    public static void main(String[] args) throws FileNotFoundException {
        PrintWriter pw = new PrintWriter("pw.txt");

        pw.print(1);
        pw.print('a');
        pw.print(3.14f);
        pw.print(3.14);
        pw.print(false);

        pw.println();
        pw.println(1);
        pw.println('a');
        pw.println(3.14f);


        pw.close();


    }
}

 

4 随机访问文件流

4.1 随机访问文件流的概述

  • RandomAccessFile。
  • RandomAccessFile不属于任何IO流体系,其父类是Object类。此类的示例支持对访问文件的读取和写入。

4.2 RandomAccessFile的构造方法

  • 构造方法:创建读取和写入的随机访问文件流,通过mode参数指定
public RandomAccessFile(File file,String mode) throws FileNotFoundException

 

  • 构造方法:创建读取和写入的随机访问文件流,通过mode参数指定
public RandomAccessFile(String name,String mode) throws FileNotFoundException

4.3 RandomAccessFile的应用

  • 示例:
package com.xuweiwei;


import java.io.IOException;
import java.io.RandomAccessFile;

public class RandomAccessFileDemo {
    public static void main(String[] args) throws IOException {
        RandomAccessFile raf = new RandomAccessFile("pw.txt", "rw");

        byte[] buffer = new byte[1024];

        int len = 0;
        while(-1 != (len = raf.read(buffer))){
            System.out.println(new String(buffer,0,len));
        }

        raf.close();

        raf = new RandomAccessFile("raf.txt","rw");

        raf.write(2);
        raf.writeBoolean(true);


        raf.close();



    }
}

 

5 合并流

5.1 合并流概述

  • SequenceInputStream类可以将多个输入流串联在一起,合并为一个输入流,因此,也称为合并流。

5.2 合并流的构造方法

  • 构造方法:将多个输入流合并为一个输入流
public SequenceInputStream(Enumeration<? extends InputStream> e)

 

  • 构造方法:将2个输入流合并为一个输入流
public SequenceInputStream(InputStream s1,InputStream s2)

5.3 合并流的应用

  • 示例:
package com.xuweiwei;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.SequenceInputStream;

public class SequenceInputStreamDemo {
    public static void main(String[] args) throws IOException {
        SequenceInputStream sis = new SequenceInputStream(new FileInputStream("dos.txt"),new FileInputStream("raf.txt"));

        byte [] buffer = new byte[1024];
        int len = 0;
        while(-1 != (len = sis.read(buffer))){
            System.out.println(new String(buffer,0,len));
        }

        sis.close();


    }
}

 

6 序列化流

6.1 序列化流的概述

  • ObjectOutputStreamWriter和ObjectInputStream。
  • 对象序列化是将对象状态转换为可保持或传输的过程。一般的格式是和平台无关的二进制流,可以将这种二进制流持久保存到磁盘上,也可以通过网络将这种二进制流传输到另一个网络节点。
  • 对象的反序列化是指将二进制数据流还原成对象。

6.2 序列化流的应用

  • 示例:
package com.xuweiwei;

import java.io.Serializable;

public class Student implements Serializable {
    private String name;
    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}
package com.xuweiwei;

import java.io.*;

public class ObjectSstreamDemo {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Student student = new Student();
        student.setName("你好");
        student.setAge(20);

        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("oo.txt"));

        oos.writeObject(student);


        oos.close();


        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("oo.txt"));

        Object o = ois.readObject();

        if (o != null) {
            Student s = (Student) o;
            System.out.println("s.getName() = " + s.getName());
            System.out.println("s.getAge() = " + s.getAge());
        }



    }
}

6.3 如何让对象的变量不被序列化?

  • 使用transient关键字声明不需要序列化的成员变量。

 

  • 示例:
package com.xuweiwei;

import java.io.Serializable;

public class Student implements Serializable {
    private String name;
    private transient Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

 

7 Properties类

7.1 Properties类的概述

  • 属性集合类,是一个可以和IO流相结合使用的集合类。
  • 可保存在流中或从流中夹杂。属性列表中的每个键和值都是字符串
  • Properties是HashTable的子类。

7.2 Properties的应用

  • 示例:
package com.xuweiwei;

import java.util.Properties;

public class PropertiesDemo {
    public static void main(String[] args) {
        Properties properties = new Properties();

        properties.setProperty("hello","world");

        String hello = properties.getProperty("hello");
        System.out.println("hello = " + hello);


    }
}

 

  • 示例:
package com.xuweiwei;

import java.util.Properties;
import java.util.Set;

public class PropertiesDemo {
    public static void main(String[] args) {
        Properties properties = new Properties();

        properties.setProperty("hello","world");

        Set<String> keys = properties.stringPropertyNames();

        for (String key : keys) {
            System.out.println(key +" = " + properties.getProperty(key));

        }


    }
}

 

  • 示例:
package com.xuweiwei;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;

public class PropertiesDemo {
    public static void main(String[] args) throws IOException {
        Properties properties = new Properties();

        properties.setProperty("hello", "world");
        properties.setProperty("abc", "abc");

        properties.store(new FileOutputStream("abc.properties"), "属性文件");


        System.out.println("--------------------------------------------");

        properties.load(new FileInputStream("abc.properties"));
        Set<String> keys = properties.stringPropertyNames();
        for (String key : keys) {
            System.out.println(key + " = " + properties.get(key));
        }

    }
}