Book: "Java Core Technology Vol. two advanced features 9"
Time: 2017.9.4-17:13
1. Flow
Input stream: An object from which a sequence of bytes can be read; abstract class (InputStream)
Output stream: An object to which a sequence of bytes can be written; abstract class (OutputStream)
Source of the stream: file, network connection, memory block.
Byte-oriented, so it is inconvenient to process Unicode information (multiple bytes per character)
-->reader & Writer extends Stream (implements two byte characters)
InputStream Method:
abstract int Read (): reads a byte and returns the read-in byte, which returns 1 at the end. The subclass needs to override this method.
abstract int write (int b): writes out a byte to a location.
Note: These two methods block the thread at execution time until the read-in or write-out is complete.
Available: Check the current number of bytes that can be read in
Flush: Outputs the stream of the buffer.
After reading and writing, you must call the Close method to close the stream, or if it flows more, it will run out of operating system resources.
FileInputStream & FileOutputStream can provide input and output streams attached to a disk file. The parameter is a file name or full pathname.
Use System.getproperty ("User.dir") to get the current path
System. out. println (System.getproperty ("user.dir")); -D:\WorkSpace\Eclipse\IOTest
The full path we need to get the operating system operator, such as under Windows:
System. out. println (File.separator); -/
You can also use the double backslash notation: c:\\windows\\123.txt
A basic read-file program:
ImportJava.io.FileInputStream; Public classIotest { Public Static voidMain (string[] args) {Try{FileInputStream FileInputStream=NewFileInputStream ("D:\\workspace\\eclipse\\iotest\\123.txt"); inti =fileinputstream.available (); for(intj=0;j<i;j++) {System.out.printf ("%c", Fileinputstream.read ()); } fileinputstream.close (); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); } }}
Java Core Technology Volume II (i)