Collection combined with IO stream-Properties

Properties

Indicates that it can be read from the stream or loaded into the stream.
Inherited Hashtable

1. Traverse

package com.itheima.demo07.Prop;

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

public class Demo01Properties {
    
    
    public static void main(String[] args) {
    
    
        Properties properties = new Properties();
        properties.setProperty("filename", "a.txt");
        properties.setProperty("length", "209385038");
        properties.setProperty("location", "D:\\a.txt");
        System.out.println(properties);
        /*遍历打印属性集*/
        Set<String> strings = properties.stringPropertyNames();
        for (String s : strings) {
    
    
            System.out.println(properties.getProperty(s));
        }
    }
}

operation result

{location=D:\a.txt, filename=a.txt, length=209385038}
D:\a.txt
a.txt
209385038

2. Collection -> Hard Disk Store

Corresponding to the character stream FileWriter, write data out of the memory
Corresponding to the byte stream FileOutputStream, output data from the memory

public void store(Writer writer, String comments)
        throws IOException
        {
    
    
            store0((writer instanceof BufferedWriter)?(BufferedWriter)writer
                            : new BufferedWriter(writer),
                    comments,
                    false);
        }
public void store(OutputStream out, String comments)
        throws IOException
    {
    
    
        store0(new BufferedWriter(new OutputStreamWriter(out, "8859_1")),
               comments,
               true);
    }

The first type: character output stream, which can be written in Chinese.
The second type: byte output stream, which cannot be written in Chinese.

2.1 Character stream FileWriter to write files

Before running

//使用字符流
package com.itheima.demo07.Prop;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Properties;

public class Demo02Properties {
    
    
    public static void main(String[] args) throws IOException {
    
    
        Properties properties = new Properties();
        properties.setProperty("filename", "a.txt");
        properties.setProperty("length", "209385038");
        properties.setProperty("location", "D:\\a.txt");
        //创建一个字符流
        FileWriter fw = new FileWriter("h.txt");
        properties.store(fw, "sava date");
        fw.close();
    }
}

operation result
After running
Before running

2.2 Byte stream FileOutputStream output from memory

//使用字节流
package com.itheima.demo07.Prop;

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

public class Demo03Properties {
    
    
    public static void main(String[] args) throws IOException {
    
    
        Properties properties = new Properties();
        properties.setProperty("filename", "a.txt");
        properties.setProperty("length", "209385038");
        properties.setProperty("location", "D:\\a.txt");
        //创建一个字节流
        FileOutputStream fos = new FileOutputStream("h.txt");
        properties.store(fos, "sava date");
        fos.close();
    }
}

operation result
After running

3. Hard Disk -> Collective load

Corresponding to the character stream, FileReadread the data from the memory
Corresponding to the byte stream FileInputStream, input the data from the memory

public synchronized void load(Reader reader) throws IOException {
    
    
        load0(new LineReader(reader));
    }
public synchronized void load(InputStream inStream) throws IOException {
    
    
        load0(new LineReader(inStream));
    }

The first type: character input stream, can write Chinese.
The second type: byte input stream, can not write Chinese

3.1 Character stream FileRead to read files

Before running

package com.itheima.demo07.Prop;

import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;

public class Demo04Properties {
    
    
    public static void main(String[] args) throws IOException {
    
    
        Properties properties = new Properties();
        FileReader fr = new FileReader("h.txt");
        System.out.println(properties);
        properties.load(fr);
        System.out.println(properties);
        Set<String> strings = properties.stringPropertyNames();
        for (String s : strings) {
    
    
            System.out.println(properties.getProperty(s));
        }
    }
}

operation result

{}
{location=D:\a.txt, filename=a.txt, length=209385038}
D:\a.txt
a.txt
209385038

3.2 Byte stream FileInputStream input to memory

Before running

package com.itheima.demo07.Prop;

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

public class Demo05Properties {
    
    
    public static void main(String[] args) throws IOException {
    
    
        Properties properties = new Properties();
        FileInputStream fis = new FileInputStream("h.txt");
        System.out.println(properties);
        properties.load(fis);
        System.out.println(properties);
        Set<String> strings = properties.stringPropertyNames();
        for (String s : strings) {
    
    
            System.out.println(properties.getProperty(s));
        }
    }
}

operation result

{}
{location=D:\a.txt, filename=a.txt, length=209385038}
D:\a.txt
a.txt
209385038

tip: The key-value pairs that are connected in the file can be connected with an equal sign and a space.
In the file, it can be commented with #. The
default key-value pair is read as a string, no need to add ""

Guess you like

Origin blog.csdn.net/weixin_45966880/article/details/113826030