JAVA study notes 10-attribute set, buffer stream, conversion stream, serialization, print stream

Attribute set

1.Properties class The
Properties class represents a persistent set of properties. Properties can be saved in the stream or loaded from the stream. The
Properties collection is a unique collection that combines with the IO stream.
You can use the store method in the Properties collection to persist the temporary data in the collection and write it to the hard disk for storage.
You can use Properties The method load in the collection reads the file (key-value pair) saved in the hard disk into the collection and uses it
. Each key and its corresponding value in the attribute list is a string.
The Properties collection is a two-column collection. The key and value are strings by default.
Construction method:
public Properties(): Create an empty property list.
Basic storage method:
public Object setProperty(String key, String value): save a pair of properties
public String getProperty(String key): Use the key specified in this property list to search for property values
public Set stringPropertyNames(): A collection of the names of all keys

public class Demo01 {
    
    
    public static void main(String[] args) {
    
    
        show01();
    }
    //使用Properties 集合存储数据,遍历取出Properties集合中的数据
    private static void show01() {
    
    
        //创建Properties集合对象
        Properties prop=new Properties();
        //使用setProperty往集合中添加数据
        prop.setProperty("Kobe","24");
        prop.setProperty("James","23");
        prop.setProperty("Curry","30");
        //使用stringPropertyNames把Properties集合中的键取出存储到一个set集合中
        Set<String> set=prop.stringPropertyNames();
        //遍历set集合,取出Properties集合中的每一个键
        for (String s : set) {
    
    
            //使用getProperty方法通过s获取value
            String value=prop.getProperty(s);
            System.out.println(s+":"+value);
        }
    }
}

store method
You can use the store method in the Properties collection to persistently write the temporary data in the collection to the hard disk for storage

public class Demo02 {
    
    
    public static void main(String[] args) throws IOException {
    
    
        show01();
    }
    //使用Properties集合中的方法store,把集合中的临时数据,持久化写入硬盘
    private static void show01() throws IOException {
    
    
        //创建Properties集合对象,添加数据
        Properties prop=new Properties();
        prop.setProperty("Kobe","24");
        prop.setProperty("James","23");
        prop.setProperty("Curry","30");
        //创建字节输出流/字符输出流对象,构造方法中绑定要输出的目的地
        FileWriter fw=new FileWriter("F:\\JAVA\\File\\text1.txt");
        //使用Properties中的方法store,把集合中的临时数据,永久写入到硬盘中存储
        prop.store(fw,"save data");
        //释放资源
        fw.close();
    }
}

Load method
You can use the method load in the Properties collection to read the file (key-value pair) saved in the hard disk to the file in the collection that uses the
stored key-value pair. The connection symbol between the key and the value can use =, space (Other symbols) In
the file storing key-value pairs, you can use # to comment, and the comment content will not be read
. In the file storing key-value pairs, the key and value are both strings by default

public class Demo03 {
    
    
    public static void main(String[] args) throws IOException {
    
    
        show01();
    }

    private static void show01() throws IOException {
    
    
        //创建Properties集合对象
        Properties prop=new Properties();
        //使用Properties集合对象中的load读取保存键值对的文件
        prop.load(new FileReader("F:\\JAVA\\File\\text1.txt"));
        //遍历Properties
        Set<String> set=prop.stringPropertyNames();
        for (String s : set) {
    
    
            String value = prop.getProperty(s);
            System.out.println(s+":"+value);
        }
    }
}

Buffer stream

1. Byte buffered stream
public BufferedInputStream(InputStream in): Create a new buffered input stream, add a buffer (array) to the basic byte input stream to improve the read data of the basic byte input stream
public BufferedOutputStream(OutputStream out): Creates a new buffered output stream
of bytes buffered output stream:
public BufferedOutputStream (OutputStream out): Creates a new buffered output stream can pass
delivery FileOutputStream, a buffered stream buffer will increase FileOutputStream, write FileOutputStream increase of Input efficiency

public class Demo04 {
    
    
    public static void main(String[] args) throws IOException {
    
    
        //创建FileOutputStream对象,构造方法中绑定要输出的目的地
        FileOutputStream fos=new FileOutputStream("F:\\JAVA\\File\\text1.txt");
        //创建BufferedOutputStream对象,构造方法中传递FileOutputStream对象,提高FileOutputStream对象效率
        BufferedOutputStream bos=new BufferedOutputStream(fos);
        //使用BufferedOutputStream对象中的write,把数据写入到内部缓冲区中
        bos.write("写入数据到缓冲区".getBytes());
        //使用BufferedOutputStream对象中的方法flush,把内部缓冲区中的数据刷新到文件中
        bos.flush();
        //释放资源
        bos.close();
    }
}

Byte buffered input stream:
public BufferedInputStream(InputStream in): Create a new buffered input stream, you can pass
FileInputStream, buffered stream will add a buffer to FileInputStream, improve the writing efficiency of FileInputStream

public class Demo05 {
    
    
    public static void main(String[] args) throws IOException {
    
    
        //创建FileInputStream对象,构造方法中绑定要读取的数据
        FileInputStream fis=new FileInputStream("F:\\JAVA\\File\\text1.txt");
        //创建BufferedInputStream对象,构造方法中传递FileInputStream对象,提高FileInputStream读取效率
        BufferedInputStream bis=new BufferedInputStream(fis);
        //使用BufferedOutputStream对象中的read,读取文件
        int len=0;
        while((len=bis.read())!=-1){
    
    
            System.out.println(len);
        }
        //释放资源
        bis.close();
    }
}

Comparison of file copy efficiency:

public class Demo06 {
    
    
    public static void main(String[] args) throws IOException {
    
    
        long start=System.currentTimeMillis();
        //创建字节缓冲输入流对象,构造方法中传递字节输入流
        BufferedInputStream bis=new BufferedInputStream(new FileInputStream("F:\\JAVA\\File\\picture.jpg"));
        //创建字节缓冲输出流对象,构造方法中传递字节输出流
        BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("F:\\JAVA\\File\\FileCopy\\picture.jpg"));
        //使用字节缓冲输入流对象中的方法read读取文件
        //使用字节缓冲输出流对象中的方法write把读取的数据写入到内部缓冲区中
        int len=0;
        while((len=bis.read())!=-1) {
    
    
            bos.write(len);
        }
        //释放资源
        bos.close();
        bis.close();
        long end=System.currentTimeMillis();
        System.out.println("耗时"+(end-start)+"毫秒");
    }
}

2. Character buffered stream
Character buffered output stream
Construction method:
BufferedWriter (Writer out): Create a buffered character output stream with default size output buffer
Writer out: Character output stream
we can pass FileWriter, buffered stream will add a buffer to FileWriter Area, improve the writing efficiency of FileWriter.
Unique member method:
void newLine(): write a line separator, and obtain different line separators according to different operating systems

public class Demo07 {
    
    
    public static void main(String[] args) throws IOException {
    
    
        //创建一个字符缓冲输出流,构造方法中传递字符输出流
        BufferedWriter bw=new BufferedWriter(new FileWriter("F:\\JAVA\\File\\text1.txt"));
        //调用字符缓冲输出流中的方法write,把数据写入到内存缓冲区中
        for(int i=0;i<10;i++){
    
    
            bw.write("Kobe");
            bw.newLine();
        }
        //调用字符缓冲输出流中的方法flush,把内存缓冲区中的数据刷新到文件中
        bw.flush();
        //释放资源
        bw.close();
    }
}

Character buffered input stream
Construction method:
BufferedReader(Reader in): Create a buffered character input stream with a default size input buffer
Reader in: Character input stream, we can pass FileReader, the buffered stream will add a buffer to FileReader, improve FileReader The reading efficiency of the
unique member method:
String readLine(): Read a text line. Read a row of data

public class Demo08 {
    
    
    public static void main(String[] args) throws IOException {
    
    
        //创建字符缓冲输入流对象,构造方法中传递字符输入流
        BufferedReader br=new BufferedReader(new FileReader("F:\\JAVA\\File\\text1.txt"));
        //使用字符缓冲输入流对象中的方法read/readLine读取文本
        String line;
        while((line=br.readLine())!=null){
    
    
            System.out.println(line);
        }
        br.close();
    }
}

Conversion flow

1. Character encoding and character set
Character encoding: A set of corresponding rules between natural language characters and binary numbers.
Character set: Code table. A collection of all characters supported by a system, including national characters, punctuation marks, graphic symbols, numbers, etc.
2.
InputStreamReader class InputStreamReader(InputStream in): Create a character stream using the default character set
InputStreamReader(InputStream in,String charsetName): Create a character stream with a specified character set

public class Demo02 {
    
    
    public static void main(String[] args) throws IOException {
    
    
        //创建InputStreamReader对象,构造方法中传递字节输入流和指定的编码表名称
        InputStreamReader isr=new InputStreamReader(new FileInputStream("F:\\JAVA\\File\\text1.txt"),"UTF-8");
        //创建InputStreamReader对象中的方法read读取文件
        int len;
        while((len=isr.read())!=-1){
    
    
            System.out.println((char) len);
        }
        //释放资源
        isr.close();
    }
}

3.
OutputStreamWriter class OutputStreamWriter (OutputStream out): Create a byte output stream using the default character set encoding
OutputStreamWriter (OutputStream out, String charsetName): Create a byte output stream using the specified character set

public class Demo01 {
    
    
    public static void main(String[] args) throws IOException {
    
    
        //创建一个OutputStreamWriter对象,构造方法中传递字节输出流和指定的编码表名称
        OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream("F:\\JAVA\\File\\text1.txt"),"utf-8");
        //使用OutputStreamWriter对象中的write,把字符转换为字节存储缓冲区中
        osw.write("你好");
        //使用OutputStreamWriter对象中的方法flush,把内存缓冲区中的字节刷新到文件中
        osw.flush();
        //释放资源
        osw.close();
    }
}

Serialization

Serialization: Write the object in a stream to a file and save it, called write object, also called serialization and
deserialization: read the object saved in the file in a stream, called read object, also called reverse Serialization During
serialization and deserialization, NotSerializableException will be thrown. There is no serialization exception.
Classes implement the interface to enable the serialization function. Classes that do not implement this interface will serialize or deserialize any state of Wufu
to be serialized The class of serialization and deserialization must implement the Serializable interface, and a mark will be added to the class.
When we serialize and deserialize, we will detect whether the class has this mark.
If there is, we can serialize and deserialize , If not, an exception will be thrown.
1. ObjectOutputStream class
Construction method:
public ObjectOutputStream (OutputStream out): Create a specified OutputStream ObjectOutputStream (byte output stream)
unique member method:
void writeObject (Object obj): will be specified Object write ObjectOutputStream

public class Demo03 {
    
    
    public static void main(String[] args) throws IOException {
    
    
        //创建ObjectOutputStream对象,构造方法中传递字节输出流
        ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("F:\\JAVA\\File\\Person.txt"));
        //使用ObjectOutputStream对象中的方法writeObject,把对象写入到文件中
        oos.writeObject(new Person("Kobe",24));
        //释放资源
        oos.close();
    }
}

2. ObjectInputStream class
Construction method:
public ObjectInputStream(InputStream in): Create a
unique member method of ObjectInputStream read from the specified InputStream :
Object readObject(): Read object from ObjectInputStream

public class Demo04 {
    
    
    public static void main(String[] args) throws IOException, ClassNotFoundException {
    
    
        //使用ObjectInputStream对象,构造方法中传递字节输入流
        ObjectInputStream ois=new ObjectInputStream(new FileInputStream("F:\\JAVA\\File\\Person.txt"));
        //使用ObjectInputStream对象中的方法readObject读取保存对象的文件
        Object o=ois.readObject();
        //释放资源
        ois.close();
        //使用读取出来的对象
        System.out.println(o);
    }
}

transient: transient keyword, modified member variables cannot be serialized

Print stream

PrintStream: Added functions for other output streams, enabling them to easily print various data value representations
Features:
1. Only responsible for data output, not for data reading
2. Unlike other output streams, PrintStream will not throw IOException
3. There are unique methods.
Construction method:
PrintStream(File file): The output destination is a file
PrintStream(OutputStream out): The output destination is a byte output stream
PrintStream(String fileName): The output destination Is a file path

Guess you like

Origin blog.csdn.net/qq_44708714/article/details/107077729