1. What are the classifications of streams in Java?
1) From the flow direction, the flow is generally divided into input flow and output flow
Input stream: for example, System.in is an input stream of InputStream type
Output stream: for example, System.out is an output stream of PrintStream type
2) . in terms of reading type, it is generally divided into byte stream and character stream
Byte stream: for example, System.in is an InputStream type byte stream
Character stream: for example, new InputStreamReader(System.in) is a character stream object
3) . flow from the source: it is divided into node flow and filter flow
Node flow: directly operate the flow corresponding to the target device
Such as file stream, standard input / output stream
Filter flow: inherit the flow with the keyword filter
It is used to wrap the operation node flow, which is convenient for reading and writing various types of data
It is divided into byte stream and character stream. The main differences are:
1) . when the byte stream is read, a byte is returned when a byte is read; The character stream uses the byte stream to read one or more bytes (the number of bytes corresponding to Chinese is two and 3 bytes in UTF-8 code table). First, check the specified coding table and return the found characters.
2) Byte stream can process all types of data, such as pictures, MP3, AVI video files, while character stream can only process character data. As long as you are dealing with plain text data, you should give priority to using character stream. In addition, you should use byte stream.
2. What are the subclasses of byte stream InputStream and OutputStream? Please give an example to illustrate its usage scenario. What are the corresponding character streams?
1) Subclass of. InputStream
FileInputStream file stream
PipedInputStream pipeline input stream to read the pipeline content. Multi thread communication with PipedOutputStream
ObjectInputStream is used to recover serialized objects
ByteArrayInputStream contains a memory buffer from which bytes are fetched.
SequenceInputStream is a logical concatenation of multiple input streams, read from the first input stream to the last input stream
FilterInputStream is a filter stream. It can read and write data, and can also perform special processing on data
2) Subclass of. OutputStream
FileOutputStream file stream
PipedOutputStream pipeline flow
ObjectOutputStream serializes the object and writes it to the specified place
ByteArrayOutputStream builds a bridge between byte arrays and streams
SequenceOutputStream is a logical concatenation of multiple input streams
FilterInputStream is a filter stream. It can read and write data, and can also perform special processing on data
3. What is the conversion between byte stream and character stream? What support does Java provide for this?
1) InputStreamReader(InputStream in) is the construction method of inputstreamReader to convert input bytes into character streams
InputStreamReader ins = new InputStreamReader(System.in); InputStreamReader ins = new InputStreamReader(new FileInputStream("test.txt"));
2) . the construction method of OutputStreamWriter or PrintWriter is used when the output character flows into a byte stream: OutputStreamWriter outputs = new OutputStreamWriter (New fileoutputstream ("test.txt"));
4. What is the function of filtering flow (assembly of flow) in Java? Please give an example of a commonly used filter stream.
Function: cache function, which is used to assemble node streams with high read and write overhead such as file disks, network devices and terminals, so as to improve the read and write performance
1) Use of. BufferedReader: used to cache 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)); 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");
2) . DataInputStream and DataOutputStream Java basic data types can be written and read from the byte stream, independent of the specific data types of the machine, which is convenient for storing and recovering data
5. What is object serialization and deserialization? What support does Java provide for this?
Concept of serialization and deserialization: serialization converts the object that implements the serializable interface into a byte sequence, and can completely restore the byte sequence to the original object in the future. The latter is also called deserialization
The purpose of serialization is to facilitate media storage and network transmission.
Support: use ObjectInputStream class and ObjectOutputStream class
6. What does the java File class represent? What does it do?
The File class refers to the files and directories in the system (directories are special files)
Function: you can operate files or directories in Java programs through File class. The File class can only be used to operate files or directories (including creating, deleting, renaming files and directories), but cannot be used to access the contents of files. If you need to access the contents of the File, you need to use input / output streams.
7. What support does Java provide for file reading and writing?
read file
import java.io.*; public class OpenFile { public static void main(String args[]) throws IOException { try { //Create file input stream object FileInputStream rf = new FileInputStream("OpenFile.java"); int n=512,c=0; byte buffer[] = new byte[n]; while ((c=rf.read(buffer,0,n))!=-1 ) //Read input stream { System.out.print(new String(buffer,0,c)); } rf.close(); //Close input stream } catch (IOException ioe) { System.out.println(ioe);} catch (Exception e) { System.out.println(e);} } }
write file
import java.io.*; public class Write1 { public static void main(String args[]) { try { System.out.print("Input: "); int count,n=512; byte buffer[] = new byte[n]; count = System.in.read(buffer); //Read standard input stream FileOutputStream wf = new FileOutputStream("Write1.txt"); //Create file output stream object wf.write(buffer,0,count); //Write output stream wf.close(); //Close output stream System.out.println("Save to Write1.txt!"); } catch (IOException ioe) { System.out.println(ioe);} catch (Exception e) { System.out.println(e);} } }