First, what are the classifications of streams in Java?
1. Classify by flow direction: input flow and output flow
Programs can write data with output flow direction files and read data from files with input flow
For keyboard, only input stream; For screen, only output stream
2. By Read Type: Byte Stream and Character Stream
Byte stream: Binary, which handles all files in bytes
Character stream: Text file, only plain text can be processed
3. Classify by source: Node Flow and Filter Flow
Node stream: Direct operation of the stream corresponding to the target device (file stream, byte array stream, standard input/output stream, etc.)
Filter Stream: Inherit stream with Filter keyword, the program operates node stream through filter stream, which is more flexible and convenient
Two, what are the subclasses of byte streams InputStream and OutputStream? Examples of scenarios are provided. What are the corresponding character streams?
Byte stream InputStream:
java.io.InputStream
java.io.FileInputStream //File input stream, corresponding to java.io.InputstreamReader
java.io.PipedInputStream //Pipeline input stream, corresponding to java.io.Pipedreader
java.io.ObjectInputStream //Object Input Stream, used to serialize problems
java.io.ByteArrayInputStream //byte input stream, corresponding to java.io.CharArrayReader
java.io.SequenceInputStream //Sequence Input Stream
java.io.FilterInputStream //Filter input streams contain some other input streams that are used as their basic data source and may transform data or provide other functionality along the way.
java.io.DataInputStream
java.io.BufferedInputStream
java.io.PushbackInputStream
Byte stream OutputStream:
java.io.OutputStream
java.io.FileOutputStream //File output stream, corresponding to java.io.FileReader
java.io.PipedOutputStream //Pipeline output stream, corresponding to java.io.PipedReader
java.io.ObjectOutputStream //Object Output Stream
java.io.ByteArrayOutputStream //byte output stream, corresponding to java.io.CharReader
java.io.FilterOutputStream //Filter Output Stream
java.io.DataOutputStream
java.io.BufferedOutputStream
java.io.PrintStream
Third, what is the conversion between byte stream and character stream? What support does Java provide for this?
Support provided by Java:
- Input byte stream to character stream: InputStreamReader
- Output character stream to byte stream: OutputStreamWriter or PrintWriter
// Byte Stream->Character Stream InputStreamReader ins = new InputStreamReader(System.in); InputStreamReader ins = new InputStreamReader(new FileInputStream("test.txt")); // Character Stream->Byte Stream OutputStreamWriter outs = new OutputStreamWriter(new FileOutputStream("test.txt"));
Fourth, what is the use of filter streams (assembly of streams) in Java? Please give examples of commonly used filter streams.
Role: Cache effect for assembling expensive read and write node streams such as file disks, network devices, terminals, etc., to improve read and write performance
Common filter streams
Use of filter stream BufferedReader: for caching character streams, which can be read line by line
import java.io.*; public class inDataSortMaxMinIn { public static void main(String args[]) { try{ BufferedReader keyin = new BufferedReader(new InputStreamReader(System.in)); // Assembly of streams String c1; int i=0; int[] e = new int[10]; while(i<10){ try{ c1 = keyin.readLine(); e[i] = Integer.parseInt(c1); i++; } catch(NumberFormatException ee){ System.out.println("Please enter the correct number!"); } } } catch(Exception e){ System.out.println("System Error"); } } }
Filter Streams DataInputStream and DataOutputStream, which can write and read Java basic data types from byte streams, are independent of machine specific data types and facilitate data storage and recovery
import java.io.*; public class DataStream { public static void main(String[] args)throws Exception{ try { DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("test.txt"))); dos.writeInt(3);//Write Integer dos.writeDouble(3.14);//Write Floating Point dos.writeUTF("hello");//Write string dos.close(); DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream("test.txt"))); System.out.println(dis.readInt()); //Read integer, output 3 System.out.println(dis.readDouble()); //Read floating point, output 3.14 System.out.println(dis.readUTF()); //Read string, output hello dis.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } } }
Fifth, what is object serialization and deserialization? What support does Java provide for this? (
Serialization converts an object that implements the Seriallizable interface into a sequence of bytes, which can then be completely restored to the original object, also known as deserialization.
Java supports this with ObjectInputStream and ObjectOutputStream classes
Example:
import java.io.*; public class Student implements Serializable { // Serialized Classes int number=1; String name; Student(int number,String n1) { this.number = number; this.name = n1; } void save(String fname) { try{ FileOutputStream fout = new FileOutputStream(fname); ObjectOutputStream out = new ObjectOutputStream(fout); out.writeObject(this); // Object serialization out.close(); } catch (FileNotFoundException fe){} catch (IOException ioe){} } void display(String fname) { try{ FileInputStream fin = new FileInputStream(fname); ObjectInputStream in = new ObjectInputStream(fin); Student u1 = (Student)in.readObject(); // Object Deserialization System.out.println(u1.getClass().getName()+" "+ u1.getClass().getInterfaces()[0]); System.out.println(" "+u1.number+" "+u1.name); in.close(); } catch (FileNotFoundException fe){} catch (IOException ioe){} catch (ClassNotFoundException ioe) {} } public static void main(String arg[]) { String fname = "Student.obj"; // file name Student s1 = new Student(1,"Wang"); s1.save(fname); s1.display(fname); // 1 wang } }
Sixth, what does the File class for Java mean? What's the use?
Definition: File is a class that is an abstract representation of the path name of a file (or directory).
Role: 1, directly process files and file system 2, can use constructors to generate File objects
Seven, what support does Java provide for reading and writing files? (
1. An abstract representation of the path name of a File file (or directory)
2.FileDescriptor represents a file description of an open file.
3. FileFilter & FilenameFilter interface, lists the files that meet the criteria for:
File.list(FilenameFilter fnf)
File.listFiles(FileFilter ff)
FileDialog.setFilenameFilter(FilenameFilter fnf)
FileDialog is java. Classes in AWT packages.
4.FileInputStream reads files in byte stream order.
5.FileReader reads files in character stream order.
6.FileOutputStream writes files in byte stream order.
7.FileWriter writes files in character stream order.
8.RandomAccessFile provides random access to files.