java study notes - special operation flow

1. Standard input and output streams

There are two static member variables in the System class:
public static final InputStream in: standard input stream. Usually this stream corresponds to keyboard input or another input source specified by the host environment or user
public static final PrintStream out: Standard output stream. Usually the stream corresponds to display output or another output destination specified by the host environment or by the user

//把字节流转换为字符流 :用转换流
	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
	System.out.println("请输入一个字符串:");
	String line = br.readLine();
	System.out.println("你输入的字符串是:"+line);

It is too troublesome to implement keyboard input data by yourself, all java provides a class for us to use
Scanner sc = new Scanner(System.in);

//		PrintStream ps = System.out;
//		ps.println("hello");
//		ps.print(10);
		
		//System.out的本质是一个字节输出流
		System.out.println("hello");
		System.out.print("100");

Second, the print flow

Classification:
Byte Print Stream: PrintStream
Character Print Stream: PrintWriter
Print Stream Features:
Only responsible for outputting data, not responsible for reading data
Has its own unique method
Byte print stream: PrintStream(String fileName): Created using the specified file name new print stream

PrintStream ps = new PrintStream("ps.txt");
//使用特有方法写数据
ps.print(97);
ps.println(98);
ps.close();

Character print stream:
PrintWriter(String fileName) //Create a new PrintWriter with the specified filename without automatic flushing
PrintWriter(Writer out,boolean autoFlush)//Create a new PrintWriter -out: character output stream- autoFlush: a boolean value, if true, the println, print, or format method will flush the output buffer

		PrintWriter pw = new PrintWriter("pw.txt");
		
		pw.write("hello\r\n");
		pw.write("world\r\n");
		pw.flush();
		
		pw.println("hello");
		pw.println("world");
		pw.flush();
PrintWriter pw = new PrintWriter(new PrintWriter("pw.txt"),true);
pw.println("hello");

3. Object serialization flow

Object serialization: is to save the object to the disk, or transfer the object in the network.
This mechanism is to use a byte sequence to represent an object, the byte sequence contains: the type of the object, the data of the object and the properties stored in the object
After the byte sequence is written to the file, it is equivalent to persisting the information of an object in the file. Conversely
, the byte sequence can also be read back from the file, reconstruct the object, and deserialize it.
Object serialization stream :ObjectOutputStream
constructor:
ObjectOutputStream (OutputStream out): A method of creating an ObjectOutputStream
serialized object that writes to the specified OutputStream:
void writeObject(Object obj): Writes the specified object to ObjectOutputStream
Note: If an object wants to be serialized, The class to which the object belongs must implement the Serializable interface
. The Serializable interface is a marker interface. To implement this interface, you do not need to override any methods.

Object deserialization stream: ObjectInputStream
constructor:
ObjectInputStream (InputStream in):
The method to create an ObjectInputStream deserialized object read from the specified InputStream:
Object readObject(): Read an object from ObjectInputStream

4. Properties

It is a collection class of Map system
Properties can be saved to the stream or loaded from the stream

//		Properties<String,String> prop = new Properties<String,String>();
		Properties pro = new Properties();
		pro.put("it001", "张三");
		pro.put("it002", "李四");
		pro.put("it003", "王五");
		
		Set<Object> keyset = pro.keySet();
		for(Object key : keyset) {
    
    
			Object value = pro.get(key);
			System.out.println(key+","+value);
		}

4.1, Properties as a unique method of collection:

Object setProperty(String key, String value): Set the key and value of the collection, both of which are of type String, and the bottom layer calls the Hashtable method
String getProperty(String key): Use the specified key in this property list to search for properties
Set stringPropertyNames(): from This property list returns an unmodifiable set of keys, where the keys and their corresponding values ​​are strings

Properties pro = new Properties();

//Object setProperty(String key,String value):设置集合的键和值,都是String类型,底层调用Hashtable方法
pro.setProperty("it001", "张三");
pro.setProperty("it002", "李四");

//Set<String> stringPropertyNames() :从该属性列表中返回一个不可修改的键集,其中键及其对应的值是字符串
Set<String> names = pro.stringPropertyNames();
for(String key : names) {
    
    
	String value = pro.getProperty(key);
	System.out.println(key+","+value);
}

4.2. The method of combining Properties and IO streams:

void load(InputStream inStream) //Read property list (key and element pair) from input byte stream)
void store(OutputStream out, String comments)//Write this property list (key and element pair) to this Properties table , write the output byte stream in a format suitable for using the load(InputStream) method

		Properties pro = new Properties();
//		pro.setProperty("it001", "张三");
//		pro.setProperty("it002", "李四");
//		pro.setProperty("it003", "王五");			
//		FileWriter fw = new FileWriter("fw.txt");
//		pro.store(fw, null);
//		fw.close();
		
		
		
		FileReader fr = new FileReader("fw.txt");
		pro.load(fr);
		fr.close();
		System.out.println(pro);

Guess you like

Origin blog.csdn.net/weixin_45573296/article/details/123019923