IO stream (other streams)

Other streams

Sequence flow

1, What is sequence flow

  • The sequence stream can integrate multiple byte input streams into one. When reading data from the sequence stream, it will start from the first integrated stream, continue to read the second after reading one, and so on

2, Mode of use

  • Integrate two input streams: SequenceInputstream(InputStream,InputStream)
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.SequenceInputStream;
import java.util.Enumeration;
import java.util.Vector;
import javax.sound.midi.Sequence;

public class Demo1_SequenceInputStream {

	public static void main(String[] args) throws IOException {
		//Integrate two: SequenceInputstream(InputStream,InputStream)
		FileInputStream fis1 = new FileInputStream("a.txt");
		FileInputStream fis2 = new FileInputStream("b.txt");
		SequenceInputStream sis = new SequenceInputStream(fis1,fis2);
		FileOutputStream fos = new FileOutputStream("c.txt");
		
		int b;
		while((b = sis.read()) != -1) {
			fos.write(b);
		}
		sis.close();    //When sis is closed, all the incoming stream objects in the constructor will also be closed
		fos.close();
	}

}
  • Integrate multiple input streams: sequenceinputstream (enumeration <? extends InputStream> e)

Use the elements method of enumeration in the Vector set to obtain the enumeration in the set

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.SequenceInputStream;
import java.util.Enumeration;
import java.util.Vector;
import javax.sound.midi.Sequence;

public class Demo1_SequenceInputStream {

	public static void main(String[] args) throws IOException {
		//Integrate multiple input streams: sequenceinputstream (enumeration <? extends InputStream> e)
		FileInputStream fis1 = new FileInputStream("a.txt");
		FileInputStream fis2 = new FileInputStream("b.txt");
		FileInputStream fis3 = new FileInputStream("c.txt");
		
		Vector<FileInputStream> v = new Vector<>();
		v.add(fis1);
		v.add(fis2);
		v.add(fis3);
		
		Enumeration<FileInputStream> en = v.elements();
		SequenceInputStream sis = new SequenceInputStream(en);
		FileOutputStream fos = new FileOutputStream("d.txt");
		
		int c;
		while((c = sis.read()) != -1) {
			fos.write(c);
		}
		sis.close();
		fos.close();
	}
}

Memory output stream

1, What is a memory output stream?

  • The output stream can write data to the memory, treat the memory as a buffer, and obtain all data at one time after writing

2, Mode of use

  • Create object: new ByteArrayOutputStream()
  • Write data: write(int),write(byte [])
  • Get data: toByteArray(),toString
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Demo2_ByteArrayOutputStream {

	public static void main(String[] args) throws IOException {
		FileInputStream fis = new FileInputStream("e.txt");
		ByteArrayOutputStream baos = new ByteArrayOutputStream();//A memory array that can grow is created in memory
		
		int b;
		while((b = fis.read()) != -1) {
			baos.write(b);                  //Write the read data to memory one by one
		}
		//The first is toByteArray
		//byte[] arr = baos.toByteArray();  // Take out all or the data in the buffer and assign it to the ARR array
		//System.out.println(new String(arr));
		//The second toString
		System.out.println(baos.toString());
		//Convert the contents of the buffer into a string, and you can omit calling the toString method in the output statement
		
		fis.close();
	}

}

Random access flow

1, RandomAccessFile overview

  • RandomAccessFile class is not a stream, but a subclass of Object class, but it combines the functions of InputStream and OutputStream
  • Supports reading and writing of random access files

2, Method

  • read(): read the file
  • write(): write to file
  • seek(): sets the pointer at the specified position
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

public class Demo3_RandomAccessFile {

	public static void main(String[] args) throws IOException {
		RandomAccessFile raf = new RandomAccessFile("g.txt", "rw");
		raf.write(97);
		//int x = raf.read();
		//System.out.println(x);
		raf.seek(10);//Sets the pointer at the specified position
		raf.write(98);//b
		raf.close();
	}

}

Object operation flow (game archive reading)

1, What is object operation flow

  • The stream can write out an object or read an object into a program, that is, it performs the operations of serialization and deserialization

2, Method of use

1.ObjectOutputStream (serialization)

  • Write out: new ObjectOutputStream(OutputStream),writeObject()

2.ObjectInputStream (deserialization)

  • Read in: new ObjectInputStream(InputStream),readObject()
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import com.heima.bean.Person;

public class Demo4_ObjectOutputStream {

	public static void main(String[] args) throws IOException {
		Person p1 = new Person("Zhang San",23);
		Person p2 = new Person("Li Si",24);//The object to be written out must implement the Serializable interface to be instantiated
		
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("e.txt"));
		oos.writeObject(p1);
		oos.writeObject(p2);
		
		oos.close();
	}

}

public class Demo5_ObjectInputStream {

	public static void main(String[] args) throws IOException, ClassNotFoundException {
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream("e.txt"));
		
		Person p1 = (Person)ois.readObject();
		Person p2 = (Person)ois.readObject();
		//Person p3 = (Person)ois.readObject(); EOFException occurs when the file reads to the end
		
		System.out.println(p1);
		System.out.println(p2);
		
		ois.close();
	}
}

Data input / output stream

1. What is data input / output stream

  • Datainputstream and dataoutputstream can read and write data according to the basic data type
  • For example, write a number according to the long size. When writing, the data accounts for 8 bytes. When reading, you can also read according to the long type, reading 8 bytes at a time

Errors occur when reading numbers using byte streams

	public static void demo1() throws FileNotFoundException, IOException {//Write data
		FileOutputStream fos = new FileOutputStream("h.txt");
		fos.write(997);//00000000 00000000 00000011 11100101 997 of int type
		fos.write(998);//When writing, the first three 8 bits will be cut off 11100101, and when reading, three 8 bits will be added
		fos.write(999);//00000000 00000000 11100101 229 of int type
		
		fos.close();
	}
	public static void demo2() throws FileNotFoundException, IOException {//Read data
		FileInputStream fis = new FileInputStream("h.txt");
		int x = fis.read();
		int y = fis.read();
		int z = fis.read();
		
		System.out.println(x);//229
		System.out.println(y);
		System.out.println(z);
		fis.close();
	}

2. Usage

  • DataOutputStream(OutputStream),writeInt(),writeLong()
  • DataInputStream(InputStream),readInt(),readLong()
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Demo6_Data {

	public static void main(String[] args) throws IOException {
		//demo3();
		DataInputStream dis = new DataInputStream(new FileInputStream("h.txt"));
		int x = dis.readInt();
		int y = dis.readInt();
		int z = dis.readInt();
		
		System.out.println(x);//997
		System.out.println(y);
		System.out.println(z);
		dis.close();
	}

	public static void demo3() throws FileNotFoundException, IOException {
		DataOutputStream dos = new DataOutputStream(new FileOutputStream("h.txt"));
		dos.writeInt(997);
		dos.writeInt(998);
		dos.writeInt(999);
		dos.close();
	}

Print stream

1. What is a print stream

  • This stream can easily print the toString() result of the object, and automatically add line breaks, and can use the automatic brush out mode
  • Sysyem.out is a PrintStream, which outputs information to the console by default

2. Usage

  • Print: print(),println()
  • Auto brush out: PrintWriter(OutputStream out,boolean autoFlush,String encoding)
  • The print stream operates only for data purposes

PrintStream and PrintWriter print byte stream and character stream respectively
Operation data purpose only

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import com.heima.bean.Person;

public class Demo7_PrintStream {

	public static void main(String[] args) throws IOException {
		//demo1();
		PrintWriter pw = new PrintWriter(new FileOutputStream("f.txt"),true);
		pw.println(97);        //The auto swipe function is only for the println method
		//pw.write(97);
		//pw.print(97);
		
		//pw.close();
	}

	public static void demo1() {
		System.out.println("aaa");
		PrintStream ps = System.out;//Get dimension output stream
		ps.println(97);     //The bottom layer converts 97 into a string and prints it through Integer.toString()
		ps.write(97);       //Look up the code table, find the corresponding a and print it
		
		Person p1 = new Person("Zhang San", 23);
		ps.println(p1);     //The toString method of pl is called by default
		
		Person p2 = null;
		ps.println(p2);     //Print the reference data type. If it is null, print null. If it is not null, print the toString method
		
		ps.close();
	}

}

Standard input / output stream

1. What is a standard I / O stream

  • System.in is InputStream, a standard input stream. By default, byte data can be read from keyboard input
  • System.out is a PrintStream, a standard output stream. By default, it can output character and byte data to the Console

2. Modify standard I / O stream

  • Modify input stream: System.setIn(InputStream)
  • Modify output stream: System.setOut(PrintStream)
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;

public class Demo8_SystemInOut {

	public static void main(String[] args) throws IOException {
		//demo1();
		System.setIn(new FileInputStream("a.txt")); //Change standard input stream
		System.setOut(new PrintStream("b.txt"));    //Change standard output stream
		
		InputStream is = System.in;  //Get the standard keyboard input stream, point to the keyboard by default, and point to the file after change
		PrintStream ps = System.out; //Get the standard output stream. By default, it points to the console. After changing, it points to the file
		
		int b;
		while((b = is.read()) != -1) {
			ps.write(b);
		}
		System.out.println();        //It's also an output stream. You don't have to turn it off because there is no pipeline associated with the files on the keyboard
		is.close();
		ps.close();
	}

	public static void demo1() throws IOException {
		InputStream is = System.in;
		int x = is.read();
		System.err.println(x);
		
		is.close(); 
		InputStream is2  = System.in;//Because the standard input stream is closed, it makes no sense to create objects again
		int y = is2.read();
		System.out.println(y);
	}

}

Two types of keyboard entry

1. readLine() method of BufferedReader

  • BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

2.Scanner

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

public class Demo9_SystemIn {

	public static void main(String[] args) throws IOException {
		//First keyboard entry
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String line = br.readLine();
		System.out.println(line);
		br.close();
		//The second type of keyboard entry
		Scanner sc = new Scanner(System.in);
		String line1 = sc.nextLine();
		System.out.println(line1);
		sc.close();
	}

}

Properties

1, Overview of Properties (subclass of Hashtable)

  1. The Properties class represents a persistent property value
  2. Properties can be saved in or loaded from a stream
  3. Each key and corresponding value in the attribute list is a string

2, Special features of Properties

  1. public Object setProperty(String key, String value);
  2. public String getProperty(String key);
  3. public Enumeration stringPropertyNames();

3, load() and store() functions of Properties

  1. load(): read file
  2. store(): write file

Case demonstration

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Properties;

public class Demo10_Properties {
	
	public static void main(String[] args) throws FileNotFoundException, IOException {
		//demo1();
		//demo2();
		Properties prop = new Properties();
		System.out.println("Before reading:" + prop);
		prop.load(new FileInputStream("config.properties"));
		
		prop.setProperty("tel", "18912345678");
		prop.store(new FileOutputStream("config.properties"), null);//The second parameter is used to describe the file list. If it is not described, null can be passed
		System.out.println("After reading:" + prop);
	}

	public static void demo2() {
		Properties prop = new Properties();
		prop.setProperty("name", "Zhang San");           //Set keys and values
		prop.setProperty("tel", "18912345678");
		//System.out.println(prop);
		Enumeration<String> en = (Enumeration<String>) prop.propertyNames();
		while(en.hasMoreElements()) {
			String key = en.nextElement();          //Get each key in Properties
			String value = prop.getProperty(key);   //Get value according to key
			System.out.println(key + "=" + value);
		}
	}

	public static void demo1() {
		//Use of Properties as a Map collection
		Properties prop = new Properties();
		prop.put("abc", 123);
		System.out.println(prop);
	}

}

Tags: Java

Posted on Fri, 03 Sep 2021 23:27:00 -0400 by siwelis