1. IO stream (Sequence stream)
- 1. What is a sequence stream
- The sequence stream can integrate multiple byte input streams into one, reading from the sequence stream, starting with the first stream being consolidated, reading one after the second, and so on.
2. How to use
- Integration of two: Sequenceinputstream (InputStream, InputStream)
FileInputStream fis1 = new FileInputStream("a.txt"); //创建输入流对象,关联a.txtFileInputStream fis2 = new FileInputStream("b.txt"); //创建输入流对象,关联b.txtSequenceInputStream sis = new SequenceInputStream(fis1, fis2); //将两个流整合成一个流FileOutputStream fos = new FileOutputStream("c.txt"); //创建输出流对象,关联c.txtint b;while((b = sis.read()) != -1) { //用整合后的读 fos.write(b);
2, IO Stream (Sequence Flow consolidation multiple)
- Consolidate multiple: Sequenceinputstream (enumeration)
FileInputStream fis1 = new FileInputStream("a.txt"); //创建输入流对象,关联a.txtFileInputStream fis2 = new FileInputStream("b.txt"); //创建输入流对象,关联b.txtFileInputStream fis3 = new FileInputStream("c.txt"); //创建输入流对象,关联c.txtVector<InputStream> v = new Vector<>(); //创建vector集合对象v.add(fis1); //将流对象添加v.add(fis2);v.add(fis3);Enumeration<InputStream> en = v.elements(); //获取枚举引用SequenceInputStream sis = new SequenceInputStream(en); //传递给SequenceInputStream构造FileOutputStream fos = new FileOutputStream("d.txt");int b;while((b = sis.read()) != -1) { fos.write(b);}sis.close();fos.close();
3. Io stream (Memory output stream * * * * *)
- 1. What is a memory output stream
- The output stream can write data to memory, memory as a buffer, write out once you can get all the data at once
2. How to use
4, IO stream (memory output stream of Black Horse interview questions)
- Define a file input stream and call the Read (byte[] b) method to print the contents of the A.txt file (byte array size limit is 5)
FileInputStream fis = new FileInputStream("a.txt"); //创建字节输入流,关联a.txt ByteArrayOutputStream baos = new ByteArrayOutputStream(); //创建内存输出流 byte[] arr = new byte[5]; //创建字节数组,大小为5 int len; while((len = fis.read(arr)) != -1) { //将文件上的数据读到字节数组中 baos.write(arr, 0, len); //将字节数组的数据写到内存缓冲区中 } System.out.println(baos); //将内存缓冲区的内容转换为字符串打印 fis.close();
5. Io Stream (Object operation Flow objecoutputstream)
- 1. What is an object manipulation flow
- The stream can write out an object, or read an object into the program. That is, the operation of serialization and deserialization is performed.
2. How to use
Written: New ObjectOutputStream (OutputStream), WriteObject ()
public class Demo3_ObjectOutputStream { /** * @param args * @throws IOException * 将对象写出,序列化 */ public static void main(String[] args) throws IOException { Person p1 = new Person("张三", 23); Person p2 = new Person("李四", 24);// FileOutputStream fos = new FileOutputStream("e.txt");// fos.write(p1);// FileWriter fw = new FileWriter("e.txt");// fw.write(p1); //无论是字节输出流,还是字符输出流都不能直接写出对象 ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("e.txt"));//创建对象输出流 oos.writeObject(p1); oos.writeObject(p2); oos.close(); }}
6. Io Stream (Object operation Flow ObjectInputStream)
7. Io Stream (object operation flow optimization)
* Write objects stored in the collection
Person p1 = new Person("张三", 23);Person p2 = new Person("李四", 24);Person p3 = new Person("马哥", 18);Person p4 = new Person("辉哥", 20);ArrayList<Person> list = new ArrayList<>();list.add(p1);list.add(p2);list.add(p3);list.add(p4);ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("f.txt"));oos.writeObject(list); //写出集合对象oos.close();
The read is a collection object
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("f.txt")); ArrayList<Person> list = (ArrayList<Person>)ois.readObject(); //泛型在运行期会被擦除,索引运行期相当于没有泛型 //想去掉黄色可以加注解 @SuppressWarnings("unchecked") for (Person person : list) { System.out.println(person); }ois.close();
8. IO stream (plus ID number)
- Attention
- The object to be written must implement the serializable interface to be serialized
- No need to add ID number
9. Io Stream (Overview and features of the print stream)
10. Io Stream (Overview of standard input and output streams and output statements)
11. IO Stream (Modify standard input and output stream copy picture)
System.setIn(new FileInputStream("IO图片.png")); //改变标准输入流 System.setOut(new PrintStream("copy.png")); //改变标准输出流 InputStream is = System.in; //获取标准输入流 PrintStream ps = System.out; //获取标准输出流 int len; byte[] arr = new byte[1024 * 8]; while((len = is.read(arr)) != -1) { ps.write(arr, 0, len); } is.close(); ps.close();
11, Io Stream (two ways to achieve keyboard input)
- The ReadLine method of A:bufferedreader.
- BufferedReader br = new BufferedReader (new InputStreamReader (system.in));
- B:scanner
12. Io Stream (Overview of random access streams and read and write data)
A: Overview of random access streams
- Randomaccessfile Overview
- The Randomaccessfile class does not belong to a stream and is a subclass of the object class. But it incorporates the functions of InputStream and OutputStream.
- Supports read and write to random access files.
B:read (), write (), Seek ()
13. IO Stream (data input/output stream)
- 1. What is a data input/output stream
- DataInputStream, DataOutputStream can read and write data according to the basic data type size
- For example, write a number on a long size that is 8 bytes when it is written out. Reads can also be read by a long type, read 8 bytes at a time.
2. How to use
DataOutputStream (OutputStream), Writeint (), Writelong ()
DataOutputStream dos = new DataOutputStream(new FileOutputStream("b.txt"));dos.writeInt(997);dos.writeInt(998);dos.writeInt(999);dos.close();
DataInputStream (InputStream), ReadInt (), Readlong ()
DataInputStream dis = new DataInputStream(new FileInputStream("b.txt"));int x = dis.readInt();int y = dis.readInt();int z = dis.readInt();System.out.println(x);System.out.println(y);System.out.println(z);dis.close();
14. Io Stream (Overview of the properties and use as a map collection)
- Overview of A:properties
- The Properties class represents a persistent set of attributes.
- Properties can be saved in a stream or loaded from a stream.
- Each key and its corresponding value in the property list is a string.
- B: Case Demo
- Properties as the use of the map collection
15. IO Stream (special function of properties use)
- Special features of A:properties
- Public Object setProperty (String key,string value)
- public string GetProperty (string key)
- Public enumeration Stringpropertynames ()
- B: Case Demo
- Special features of properties
16. IO Stream (Load () and store () function of properties)
- A:properties load () and store () functions
- B: Case Demo
- The load () and store () functions of the properties
Java EE Basics (22)/io stream