[企业管理] [零基础学JAVA]Java SE应用部分-29.Java IO操作(03)

[复制链接]
发表于 2022-9-23 10:47:36
版权声明:自创著作,谢绝转发!否则将追查法律责任。
上季内容回忆:
字节:InputStream、OutputStream
字符流:Reader、Writer
上季练习题:
已然是文件的拷贝,肯定要使用字节,所以建立一个字节的输入流再建立一个字节的输出流,边读边写。
看下作用:
IO操作即是研究五个类一个接口
·File类
·OutputStream、InputStream
·Reader、Writer
·目标序列化
管道流
理解:管道流是管两个线程间通信使的。
publicvoidconnect(PipedOutputStreamsrc)throwsIOException
把输出与输入连接在一起。
importjava.io.
;
//设置两个线程,一个发送另一个接收
classSendimplementsRunnable{
privatePipedOutputStreamout=null;
//在结构方法中实例化此目标
publicSend(){
this.out=newPipedOutputStream();
}
publicvoidrun(){
//启动线程,启动线程时直接发送
Stringstr=HelloWorld~~~;
try{
this.out.write(str.getBytes());
}catch(Exceptione){}
}
//有必要把管道输出流返回去
publicPipedOutputStreamgetOut(){
returnthis.out;
}
}
classReceiveimplementsRunnable{
privatePipedInputStreaminput=null;
publicReceive(){
this.input=newPipedInputStream();
}
publicvoidrun(){
//启动线程,接收内容
byteb=newbyte;
intlen=0;
try{
len=this.input.read(b);
this.input.close();
System.out.println(newString(b,0,len));
}catch(Exceptione){}
}
publicPipedInputStreamgetInput(){
returnthis.input;
}
}
publicclassIODemo01{
publicstaticvoidmain(Stringargs){
Sends=newSend();
Receiver=newReceive();
//要把输出连接到输入上去哈~
try{
r.getInput().connect(s.getOut());
}catch(Exceptione){}
newThread(s).start();
newThread(r).start();
}
}
看下作用:
ByteArrayInputStream
内存的输入、输出流
之前解说的很多内容都是把内容直接输出到文件中去了,那么如今把内容输入到内存中,直接从内存中输出。
看下有没变成大写哈~
应该按以下的方法理解:
·ByteArrayInputStream():理解为内存要读取
·ByteArrayOutputStream():从内存中输出
输入输出方法
System.out:
-PrintStream是一个打印流,表示提供了更好的打印操作。
PrintStream有那些好处呢?
PrintStream是OutputStream的子类
实际上在IO中发挥了一种设计模式——装修模式。
·发如今OutputStream中的功能尽管可以输出,可是输出的进程不太好操作,由于所有的内容都要向byte数组转换。
PrintStream的基本原理
PrintStream中提供了比OutputStream中更多的打印功能,可以输出任意的数据,也可以对输出的数据进行换行的管理。
看下作用:在屏幕上换行是\n就可以了,在文件中换行是\r\n。
如今已然现已认识了PrintStream是OutputStream的目标,那么问?是否可以通过OutputStream终结向屏幕上输出的功能呢?可以使用目标多态性哈~~~
PrintStream是一个字节的打印流,与它相似的还有一个PrintWirter字符的打印流,建议使用字节打印流是比较方便。
总结
1、System.out与OutputStream的联系
2、PrintStream实际上是JAVA装修模式的使用,具有比父类提供更多更方便的功能
####################################################

(Copyright statement: self-created works, please do not forward! Otherwise, legal responsibility will be pursued.
Memories from last season:
Bytes: InputStream, OutputStream
Character stream: Reader, Writer
Practice questions from last quarter:
It is already a copy of the file, and bytes must be used, so create a byte input stream and then create a byte output stream, and write while reading.
Take a look at the effect:
IO operation is to study five classes and one interface
·File class
·OutputStream, InputStream
·Reader, Writer
· Target serialization
pipeline flow
Understand: The pipeline flow is the communication between the two threads.
publicvoidconnect(PipedOutputStreamsrc) throwsIOException
Connect the output to the input.
importjava.io.
;
//Set up two threads, one to send and the other to receive
classSendimplementsRunnable{
privatePipedOutputStreamout=null;
//Instantiate this target in the struct method
publicSend(){
this.out=newPipedOutputStream();
}
publicvoidrun(){
//Start the thread, send directly when the thread is started
Stringstr=HelloWorld~~~;
try{
this.out.write(str.getBytes());
}catch(Exceptione){}
}
//It is necessary to return the pipe output stream
publicPipedOutputStreamgetOut(){
returnthis.out;
}
}
classReceiveimplementsRunnable{
privatePipedInputStreaminput=null;
publicReceive(){
this.input=newPipedInputStream();
}
publicvoidrun(){
//Start the thread and receive the content
byteb=newbyte;
intlen=0;
try{
len=this.input.read(b);
this.input.close();
System.out.println(newString(b,0,len));
}catch(Exceptione){}
}
publicPipedInputStreamgetInput(){
returnthis.input;
}
}
publicclassIODemo01{
publicstaticvoidmain(Stringargs){
Sends=newSend();
Receiver=newReceive();
//Connect the output to the input~
try{
r.getInput().connect(s.getOut());
}catch(Exceptione){}
newThread(s).start();
newThread(r).start();
}
}
Take a look at the effect:
ByteArrayInputStream
Memory input and output streams
A lot of the content explained before is to output the content directly to the file, so now the content is input into the memory and output directly from the memory.
See if it's capitalized~
It should be understood as follows:
ByteArrayInputStream(): understood as memory to be read
ByteArrayOutputStream(): output from memory
I/O method
System.out:
-PrintStream is a print stream that provides better printing operations.
What are the benefits of PrintStream?
PrintStream is a subclass of OutputStream
In fact, there is a design pattern in IO—the decoration pattern.
·It is found that although the function in OutputStream can be output, the output process is not easy to operate, because all the content must be converted to byte array.
Fundamentals of PrintStream
PrintStream provides more printing functions than OutputStream, can output arbitrary data, and can also manage the output data to wrap.
Look at the effect: the newline on the screen is \n, and the newline in the file is \r\n.
Now that you have recognized that PrintStream is the target of OutputStream, then ask? Is it possible to terminate the function of outputting to the screen through OutputStream? You can use target polymorphism ~~~
PrintStream is a byte print stream, and similar to it there is a PrintWirter character print stream, it is more convenient to use a byte print stream.
Summarize
1. The connection between System.out and OutputStream
2. PrintStream is actually the use of JAVA decoration mode, which provides more and more convenient functions than the parent class
################################################## ##)

[下载]10473631175.rar




上一篇:[零基础学JAVA]Java SE应用部分-28.Java IO操作(02)
下一篇:[零基础学JAVA]Java SE应用部分-30.Java IO操作(04)

使用道具 举报

Archiver|手机版|小黑屋|吾爱开源 |网站地图

Copyright 2011 - 2012 Lnqq.NET.All Rights Reserved( ICP备案粤ICP备14042591号-1粤ICP14042591号 )

关于本站 - 版权申明 - 侵删联系 - Ln Studio! - 广告联系

本站资源来自互联网,仅供用户测试使用,相关版权归原作者所有

快速回复 返回顶部 返回列表