(Memories from last season:
File class
RandomAccessFile class---gt; file read and write operations
Main points of knowledge this season:
1. Byte: InputStream, OutputStream
2. Character stream: Reader, Writer
byte and character streams
InputStream and OutputStream
Why do you have to use subclasses to instantiate InputStream and OutputStream now?
Check JDK documentation
First terminate the injection of data into the file:
OutputStream definition:
publicabstractclassOutputStream
extendsObject
implementsCloseable,Flushable
OutputStream is an abstract class, if it is an abstract class, its subclass must be used to instantiate the target.
If you want to keep to the file, you will use FileOutputStream.
Just use these two classes to end the intent of injecting a string into the file.
FileOutputStream structure method:
publicFileOutputStream(Filefile) throwsFileNotFoundException: Create a file output stream with the target of the File class
· publicFileOutputStream(Filefile, booleanappend) throwsFileNotFoundException: Create a file output stream with the purpose of the File class, and the final append parameter indicates whether to append content to the file, and the default is to append.
publicFileOutputStream(Stringname) throwsFileNotFoundException: The way to directly inject the file and perform the output operation, in fact, it automatically includes a target of the File class, which is automatically instantiated
publicFileOutputStream(Stringname, booleanappend) throwsFileNotFoundException
To inject content into the output stream:
publicvoidwrite(byteb) throwsIOException
Now the content to be injected is a string, but the write method can only receive a byte array, and the string must be turned into a character array.
redking.blog.51cto/attachment/200902/25/27212_1235571051l3sz.png
redking.blog.51cto/attachment/200902/25/27212_1235571053aF1K.png
The above program is an output stream of bytes, because the final result is to turn the string into a byte array.
When appending, write a control symbol directly behind, the content is trueut=newFileOutputStream(f,true);
Take a look at the effect:
Now let's be more flexible ~ input from the outside
Is it possible to read the contents of the file through the program?
InputStream--gt; bytes must be byte arrays
The contents of the file are all bytes
Methods in InputStream:
To change the byte array into a string, you can directly find the structure method of String.
Look at the effect: due to the development of byte space, when the content in the file is read and the data cannot be read in the remaining space, it will report an error and output the ? symbol.
The following manages the problem of reading error reports~
Take a look at the effect, now the question marks that made mistakes are gone, they are all replaced with spaces~
Next, we think of a way to remove the spaces~ Define a count to calculate how many bits have been read in total
Take a look at the effect:
If you use the read() method in the InputStream class to read directly, you must read them one by one. When the file is read (the return value of read is -1, it indicates that the file is read), then exit the loop, and then use String to convert the byte array. becomes a string output.
Use another read method, pass in a byte array, and then the class will automatically assign the content to the byte array.
Let's see if the modified effect is the same~
In the operation of bytes, it can be found that everything must be standardized by byte array.
Character streams: operations that directly support characters
Reader, Writer
Use Writer to inject content into a file:
Let's see if the function has injected data~
It is very similar to the previous OutputStream in use, only now you can directly manipulate strings.
Use of Reader:
What are the differences between OutputStream and Writer and InputStream and Reader?
Bytes are not used in the buffer, they are directly operated on the file
·The character stream uses the buffer, and all the content must be placed in the buffer first, and then can be injected into the file.
OutputStream
Look at the effect: the file stream is not closed after all, and the content can still be output
Now, what if the program is replaced by a stream of characters?
It is found that if it is not closed, the content cannot be output. Since the content is all in the buffer, the close() method was called before, so the buffer will be automatically emptied.
Mandatory finalization of the output of the buffered content can be used directly.
publicabstractvoidflush() throwsIOException
Take a look at the effect:
Verify that the byte does not use the buffer, and the character stream uses the buffer~~~
Summarize
Practice questions (explain in the next season~~~~)
Master the basic operations of the four classes:
InputStream, OutputStream, Reader, Writer, respectively, the operation subclasses of the four files: FileXxx and the like.
####################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################)