[零基础学JAVA]Java SE应用部分-30.Java IO操作(04)
上季内容回忆:System.out--gtrintStream的一个实例化方案,自身具备了向屏幕上打印的功能,以后又讲解了可以使用OutputStream类向屏幕上进行输出的实例。
本季方案:
1、System.in
2、缓冲流
3、方案序列化
System.in对应的是键盘的输入
publicstaticfinalInputStreamin
仅仅如今不是像之前那样是从文件中读了,而是从键盘上读数据了。
看下作用:
以上的程序尽管可以输入内容,可是会存在一个问题:
首要程序拓荒了一段空间,假如如今假设输入的内容大于拓荒的空间,那会怎样呢?
看下作用:
假如要是长度大于设置好的长度,则必定有内容无法读取进来,最好的方法即是给不设置长度,假如有内容就继续读。
看下作用:
采纳判断是不是读究竟的方法终结,的确没有长度的限制了,可是也存在了别的一个问题假如输入中文,则必定呈现乱码?
最好的管理方法是设置一个缓冲区,一切的内容放到缓冲区中一次性读出来。
此缓冲区最好是为字符缓冲区——BufferedReader
publicBufferedReader(Readerin)
此处有必要接收一个Reader的方案,可是System.in实际上是InputStream类的方案
字节与字符流的转换
redking.blog.51cto/attachment/200903/1/27212_1235916759MxZ7.png
Reader和Writer自身使用到了缓冲:Writer--gt;OutputStreamWriter--gt;FileWriter
定论:
·在物理上寄存的必定永远是字节
·读到程序当中才会构成字符流
得出完整的输入数据的代码模式:
如今看下终究作用:
redking.blog.51cto/attachment/200903/1/27212_1235916772NsEi.png
以上为规范的格式,我们如今使用此种格式终结一个功能,输入两个整数以后计算两个数字的相加成果。
字符集
GBK:是包括了简体中文和繁体中文的字库
GB2312:是只包括了简体中文的字库
ISO8859-1:是一个S界的通用编码
在系统中都有一个默许的编码方法。
假如认识当时系统的默许编码呢?接着看下面哈~
默许的编码方法即是GBK,那么问,假如如今系统的编码为GBK,而我使用ISO进行编码,能精确解说出来吗?
字符乱码怎样管理——跟系统设置的编码共同就OK了。
SequenceInputStream类
redking.blog.51cto/attachment/200903/1/27212_1235916830uB4l.png
SequenceInputStream用于文件兼并
redking.blog.51cto/attachment/200903/1/27212_12359168447WXI.png
看下兼并后的作用:
redking.blog.51cto/attachment/200903/1/27212_1235916850DRfb.png
方案序列化
redking.blog.51cto/attachment/200903/1/27212_1235916867IExy.png
实际上使用的是两个类:
·ObjectOutputStream
·ObjectInputStream
Serializable接口的作用:
仅仅一个标识接口,表明你这个类的方案许可被序列化
ObjectOutputStream类的结构方法,接收一个OutputStream类的实例化方案,以表明究竟向那里保存。
publicfinalvoidwriteObject(Objectobj)throwsIOException
ObjectInputStream类的结构方法
publicfinalObjectreadObject()throwsIOException,ClassNotFoundException
redking.blog.51cto/attachment/200903/1/27212_1235916872LwYu.png
redking.blog.51cto/attachment/200903/1/27212_1235916885SC54.png
redking.blog.51cto/attachment/200903/1/27212_1235916899TXg4.png
下面进行反序列化(从文件中恢复方案)
redking.blog.51cto/attachment/200903/1/27212_12359169189i5h.png
看下作用:从文件中读取方案
redking.blog.51cto/attachment/200903/1/27212_1235916920S9NO.png
方案序列化实际上更精确的来说指的是方案可以变为字节。
transient关键词
当不希望类中的某个字段被序列化的时候,使用transient表明这个字段
redking.blog.51cto/attachment/200903/1/27212_1235916930bFOD.png
看下作用:
redking.blog.51cto/attachment/200903/1/27212_1235916932I6F1.png
总结
OutputStream:字节的输出流类
-FileOutputStream
-ObjectOutputStream
-ByteArrayOutputStream
-PipedOutputStream
-PrintStream
InputStream:字节的输入流类
-FileInputStream
-ObjectInputStream
-ByteArrayInputStream
-PipedInputStream
Reader:字符流的输入类
-InputStreamReader
-FileReader
-BufferedReader
Writer:字符流的输出类
-OutputStreamWriter
-FileWriter
-PrintWirter
File类:
Serializable接口
#######################################################
(Memories from last season:
An instantiation scheme of System.out--gtrintStream, which has the function of printing to the screen, and later explained the examples that can be output to the screen using the OutputStream class.
Program this season:
1. System.in
2. Buffer stream
3. Program serialization
System.in corresponds to keyboard input
publicstaticfinalInputStreamin
It's just that instead of reading from the file as before, the data is read from the keyboard.
Take a look at the effect:
Although the above program can input content, there is a problem:
The main program opened up a space, what if the input content is now larger than the space opened?
Take a look at the effect:
If the length is greater than the set length, there must be content that cannot be read in. The best way is to not set the length, and continue reading if there is content.
Take a look at the effect:
There is indeed no limit to the length of adopting the judgment of whether the method of reading the end is the end, but there is also another problem. If you input Chinese, it must appear garbled?
The best way to manage it is to set up a buffer and put everything into the buffer and read it out at once.
This buffer is preferably a character buffer - BufferedReader
publicBufferedReader(Readerin)
It is necessary to receive a Reader scheme here, but System.in is actually a scheme of the InputStream class
Byte to character stream conversion
redking.blog.51cto/attachment/200903/1/27212_1235916759MxZ7.png
Reader and Writer themselves use buffering: Writer--gt;OutputStreamWriter--gt;FileWriter
Conclusion:
Physically registered must always be bytes
·The character stream will be formed when the program is read
Code pattern to derive complete input data:
Now look at the final effect:
redking.blog.51cto/attachment/200903/1/27212_1235916772NsEi.png
The above is the standard format, we now use this format to terminate a function, input two integers and then calculate the addition result of the two numbers.
character set
GBK: is a font library including Simplified Chinese and Traditional Chinese
GB2312: is a font library that only includes simplified Chinese
ISO8859-1: is a universal code for the world
There is a default encoding method in the system.
What if you know the default code of the system at that time? Then look at the following~
The default encoding method is GBK, then ask, if the current system encoding is GBK, and I use ISO encoding, can you explain it accurately?
How to manage the garbled characters - the same as the encoding set by the system is OK.
SequenceInputStream class
redking.blog.51cto/attachment/200903/1/27212_1235916830uB4l.png
SequenceInputStream for file merging
redking.blog.51cto/attachment/200903/1/27212_12359168447WXI.png
Take a look at the effect after the merger:
redking.blog.51cto/attachment/200903/1/27212_1235916850DRfb.png
Scheme serialization
redking.blog.51cto/attachment/200903/1/27212_1235916867IExy.png
There are actually two classes used:
·ObjectOutputStream
·ObjectInputStream
The role of the Serializable interface:
Just an identifying interface, indicating that the scheme of your class is allowed to be serialized
The structure method of the ObjectOutputStream class receives an instantiation scheme of the OutputStream class to indicate exactly where to save.
publicfinalvoidwriteObject(Objectobj) throwsIOException
Structure method of ObjectInputStream class
publicfinalObjectreadObject() throwsIOException, ClassNotFoundException
redking.blog.51cto/attachment/200903/1/27212_1235916872LwYu.png
redking.blog.51cto/attachment/200903/1/27212_1235916885SC54.png
redking.blog.51cto/attachment/200903/1/27212_1235916899TXg4.png
Deserialize below (restore scheme from file)
redking.blog.51cto/attachment/200903/1/27212_12359169189i5h.png
Look at the effect: read the scheme from the file
redking.blog.51cto/attachment/200903/1/27212_1235916920S9NO.png
Schema serialization actually refers more precisely to the fact that the schema can be turned into bytes.
transient keyword
When a field in a class is not expected to be serialized, use transient to indicate this field
redking.blog.51cto/attachment/200903/1/27212_1235916930bFOD.png
Take a look at the effect:
redking.blog.51cto/attachment/200903/1/27212_1235916932I6F1.png
Summarize
OutputStream: byte output stream class
-FileOutputStream
-ObjectOutputStream
-ByteArrayOutputStream
-PipedOutputStream
-PrintStream
InputStream: byte input stream class
-FileInputStream
-ObjectInputStream
-ByteArrayInputStream
-PipedInputStream
Reader: Input class for character streams
-InputStreamReader
-FileReader
-BufferedReader
Writer: Output class for character streams
-OutputStreamWriter
-FileWriter
-PrintWirer
File class:
Serializable interface
################################################## #####)
页:
[1]