JAVA IO流(内存流、管道流、打印流)

来源:互联网 发布:淘宝原单男装店铺 编辑:程序博客网 时间:2024/05/22 13:55

1、内存操作流

2、管道流

3、字节、字符转换流

4、字符的编码问题

5、打印流

6、System类对IO的支持

7、对象序列化


内存操作流

FileInputStream 和FileOutputStream的时候所有的操作的目标是文件,那么如果现在假设有一些临时的信息保存在文件之中则肯定很不合理,因为操作的最后还要把文件再删除掉,所以此时在IO就提供了一个内存的操作流,通过内存操作流输入和输出的目标是内存。

使用ByteArrayOutputStream ByteArrayInputStream完成内存的操作流。

*在内存操作流中所有的输入和输出都是以内存为操作的源头

*ByteArrayOutputStream 是用于从内存向程序输出的。

*ByteArrayInputStream 是用于从程序向内存写入的。

ByteArrayInputStream的构造方法:public ByteArrayInputStream(byte[] buf)

*表示把内容向内存中写入。

ByteArrayOutputStream来讲,其基本的作用就是与OutputStream一样,一个个的读取数据。


范例:使用内存操作流,完成一个字符串由小写字母变成大写字母的操作

package com.demo.io;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;public class ByteArrayIPSDemo {public static void main(String args[]){//定义字符串String dataStr = "hello world";//将内容保存在内存输入流中ByteArrayInputStream bInputStream = new ByteArrayInputStream(dataStr.getBytes());//实例化内存输出流ByteArrayOutputStream bOutputStream = new ByteArrayOutputStream();int temp = 0;while((temp=bInputStream.read())!=-1){//当文件没有到结尾的时候 依次读取//接收字符char c = (char) temp;//从内存向程序输出的bOutputStream.write(Character.toUpperCase(c));}//取出内存输出的内容System.out.println(new String(bOutputStream.toString()));}}

内存操作在Java WEB中的 AJAX技术的时候,会结合XML解析和JavaScript、AJAX完成一些动态效果的时候使用。

String newStr = bOutputStream.toString();//取出内存输出的内容 

此段代码非常重要,表示内存数据的取出。


管道流(了解)

管道流就是进行两个线程间通讯的。使用PipedOutputStream和PipedInputStream两个类完成,但是这两个类使用的时候基本上都跟OutputStream和InputStream类似,唯一区别的死在于连接管道的操作上。

public void connect(PipedInputStream snk)throws IOException


范例:进行管道流的操作

package com.demo.io;import java.io.IOException;import java.io.PipedInputStream;import java.io.PipedOutputStream;public class PipedStreamDemo {public static void main(String[] args) throws IOException {Send send = new Send();Receive receive = new Receive();//进行管道连接send.getPipedOutputStream().connect(receive.getPipedInputStream());//启动线程new Thread(send).start();//启动线程new Thread(receive).start();}}/******************************************* * 发送数据的线程 */class Send implements Runnable{private PipedOutputStream pOutputStream=null;public Send() {pOutputStream = new PipedOutputStream();}public PipedOutputStream getPipedOutputStream(){return this.pOutputStream;}public void run() {//要发送的数据String str = "hello world";try {//发送this.pOutputStream.write(str.getBytes());//关闭this.pOutputStream.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}/******************************************* * 接收数据的线程 */class Receive implements Runnable{private PipedInputStream pInputStream = null;public Receive() {pInputStream = new PipedInputStream();}public PipedInputStream getPipedInputStream(){return pInputStream;}public void run() {//接收内容byte b[] = new byte[1024];int len=0;try {//内容读取len=this.pInputStream.read(b);//关闭this.pInputStream.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println(new String(b,0,len));}}


打印流(重点!!!)

使用OutputStream可以完成数据的输出,但是现在如果有一个float型数据好输出吗?

也就是说虽然现在提供了输出流的操作类,但是这个类本身的输出的支持功能并不是十分的强大,

所以,如果现在想要进行更方便的输出操作,则可以使用打印流。

打印流分为两种: PrintStream(字节)、PrintWriter(字符)。


观察打印流的定义:

public class PrintStream extends FilterOutputStream implements Appendable,Closeable

 PrintStream 是OutputStream的子类,继续观察其构造方法:

public PrintStream(OutputStream out)

在此方法中要接收OutputStream子类的引用。

实际上 PrintStream属于装饰。也就是说根据实例化PrintStream类对象的不同,输出的位置也不同。


范例:使用PrintStream向文件输出

package com.demo.io;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.PrintStream;public class PrintDemo {public static void main(String args[]) throws FileNotFoundException{File file = new File("g:"+File.separator+"printDemo.txt");//true 为可以追加PrintStream printStream = new PrintStream(new FileOutputStream(file,true));printStream.print("第一个数据:");printStream.println(true);printStream.print("第二个数据:");printStream.println(1.33f);printStream.print("第三个数据:");printStream.print(1111);printStream.println();printStream.println();printStream.close();}}
得出结论,使用打印流最为方便,所以建议以后在输出的时候就使用打印流完成。

在JDK 1.5之后对打印流进行了更新,可以使用格式化输出。提供了以下的方法:

public PrintStream printf(String format,Object args)

可以设置格式和多个参数。


范例:使用PrintStream进行格式化的输出


package com.demo.io;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.PrintStream;public class PrintDemo2 {public static void main(String[] args) throws FileNotFoundException {File file = new File("g:"+File.separator+"printDemo2.txt");PrintStream printStream = new PrintStream(new FileOutputStream(file));String name = "杨跃跃";int age = 24;float pay = 1000000.91f;char sex = '女';printStream.printf("姓名:%s\r\n年龄:%d\r\n工资:%7.2f\r\n性别:%c",name,age,pay,sex);printStream.close();}}

打印流中一定要始终记住以下原则,根据实例化其子类的不同,完成的打印输出功能也不同。






原创粉丝点击