找回密码
 立即注册
theOutputStreamInputStreamFile | 企业管理 2022-09-23 87 0star收藏 版权: . 保留作者信息 . 禁止商业使用 . 禁止修改作品
上季内容回忆:
File类
RandomAccessFile类---gt;文件读、写操作
本季主要知识点:
1、字节:InputStream、OutputStream
2、字符流:Reader、Writer
字节和字符流
InputStream和OutputStream
为什么如今非要用子类去实例化InputStream和OutputStream呢?
查JDK文档
先终结向文件中注入数据:
OutputStream定义:
publicabstractclassOutputStream
extendsObject
implementsCloseable,Flushable
OutputStream是一个抽象类,假如是抽象类,则必定要用其子类进行目标的实例化。
假如要向文件中保留,则要使用FileOutputStream。
就用这两个类终结向文件中注入一个字符串的意图。
FileOutputStream结构方法:
·publicFileOutputStream(Filefile)throwsFileNotFoundException:以File类的目标创建文件输出流
·publicFileOutputStream(Filefile,booleanappend)throwsFileNotFoundException:以File类的目标创建文件输出流,在终究的append参数表明的是是不是在文件后边追加内容,默许是追加的。
·publicFileOutputStream(Stringname)throwsFileNotFoundException:直接注入文件的途径,进行输出操作,实际上里边就自动包括了一个File类的目标,自动进行实例化
·publicFileOutputStream(Stringname,booleanappend)throwsFileNotFoundException
向输出流中注入内容:
publicvoidwrite(byteb)throwsIOException
如今要注入的内容是一个字符串,但是write方法接收的只能是一个byte数组,必须把字符串变为字符数组才干够。
redking.blog.51cto/attachment/200902/25/27212_1235571051l3sz.png
redking.blog.51cto/attachment/200902/25/27212_1235571053aF1K.png
以上的程序为字节的输出流,由于终究成果都是把字符串变为了byte数组了。
追加的时候就直接在后边写上一个控制的符号,内容为trueut=newFileOutputStream(f,true);
看下作用:
如今我们灵活点哈~由外部输入
能否通过程序把在文件中的内容读进来呢?
InputStream--gt;字节必定是byte数组
文件中的内容都是字节
InputStream中的方法:
把byte数组变为字符串就直接找String的结构方法。
看下作用:因拓荒byte空间,当读完文件中内容后剩下空间读不到数据就报错输出?符号。
下面管理读取报错问题哈~
看下作用,如今犯错的问号没有了,都换成空格了哈~
下面我们想方法将空格去掉哈~定义一个计数,计算究竟总共读了多少位
看下作用:
假如直接使用InputStream类中的read()方法读取,则必须一个一个读,当文件读究竟(read的返回值为-1时就表明文件读究竟),则退出循环,之后用String把byte数组变为字符串输出。
使用别的一个read方法,传入一个byte数组,之后由类自动把内容赋给byte数组。
看下修改后的作用是不是相同哈~
在字节的操作中,可以发现一切都必须以byte数组为规范。
字符流:直接支持字符的操作
Reader、Writer
使用Writer向文件中注入内容:
看下作用有没注入数据哈~
与之前的OutputStream在使用上是非常类似的,仅仅如今可以直接操作字符串了。
Reader的使用:


用于OutputStream与Writer及InputStream与Reader究竟有那些区别呢?
·字节是没有使用到缓冲区的,是直接操作文件的
·字符流是使用到了缓冲区,一切的内容要先放在缓冲区中,之后才干向文件中注入。
OutputStream

看下作用:文件流终究没有关闭,内容也仍然可以输出

如今把程序换成字符流,又会如何?

发现假如不关闭,则内容是无法输出的,由于内容全面都在缓冲区中,之前是由于调用了close()方法,所以会自动把缓冲区给清空掉。

可以直接使用强制性的把缓冲内容输出的操作终结。
publicabstractvoidflush()throwsIOException

看下作用:

证实字节不使用缓冲区,字符流使用到缓冲区~~~
总结
练习题(下季解说哈~~~)

掌握四个类的基本操作:
InputStream、OutputStream、Reader、Writer,分别的四个文件的操作子类:FileXxx之类。
############################################

(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.
####################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################)

[下载]10473082603.rar




上一篇:[零基础学JAVA]Java SE应用部分-27.Java IO操作(01)
下一篇:[零基础学JAVA]Java SE应用部分-29.Java IO操作(03)