java 打印内存_Java之IO流进阶篇:内存流,打印流,对象流

Java中的IO流,即为输入输出流。所谓输入输出流,都是相对于程序而言,程序就是这个参照物。一张图看懂输入输出流:

9f2c2d162629623600b8b8f2df5a08a0.png

输入流抽象基类:InputStream,Reader

输出流抽象基类:OutputStream,Writer

输入输出流子类众多,详情见下图:

5fc65070f773309be7aa21b2cf76bd09.png

1.内存流

用来操作内存

ByteArrayInputStream     内存到程序  不需要关闭  不使用内存资源,内存不够建议不用

ByteArrayOutputStream  程序到内存  不需要关闭  不使用内存资源,内存不够建议不用

eec0bd516abca4cafa23ee44ac25d0c5.png

内存输入流和内存输出流:

首先创建字节数组,一般引用数据类型存放在内存中。显然此时,可以创建字节数组输入流,读入程序当中。

packagecom.test;importjava.io.ByteArrayInputStream;importjava.io.IOException;public classByteArrayInputStreamDemo {public static void main(String[] args) throwsIOException {//TODO Auto-generated method stub//1.创建字节数组(要读取的数据)

byte[] data= {10,15,50,33,12,22};//2.创建流

ByteArrayInputStream bais=newByteArrayInputStream(data);//3.读取//int d;//while((d=bais.read())!=-1) {//System.out.println(d);//}//bais.close();//实际操作中无需关闭流

byte[] buf=new byte[1024];intlen;while((len=bais.read(buf))!=-1) {for (int i = 0; i < len; i++) {

System.out.println(buf[i]);

}

}

}

}

packagecom.test;importjava.io.ByteArrayOutputStream;public classByteArrayOutputStreamDemo {public static voidmain(String[] args) {//TODO Auto-generated method stub//1.创建字节数组输出流对象

ByteArrayOutputStream baos=newByteArrayOutputStream();//2.写入数据

baos.write(12);

baos.write(20);

baos.write(18);

baos.write(32);//3.获取输出流中的字节数据

byte[] data=baos.toByteArray();for (byteb : data) {

System.out.println(b);

}

}

}

使用内存流读取图片:

packagecom.test;importjava.io.ByteArrayInputStream;importjava.io.ByteArrayOutputStream;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;public classReadImg {public static void main(String[] args) throwsIOException {//TODO Auto-generated method stub//1.创建文件字节输入流

FileInputStream fis=new FileInputStream("E:\\xx.png");

ByteArrayOutputStream baos=newByteArrayOutputStream();byte[] buf=new byte[1024];//2.读取数据

intlen;while((len=fis.read(buf))!=-1) {

baos.write(buf,0,len);

}//3.获取图片数据

byte[] imgbyte=baos.toByteArray();

System.out.println(imgbyte.length);//4.创建文件输出流

FileOutputStream fos=new FileOutputStream("E:\\xx1.png");

ByteArrayInputStream bais=newByteArrayInputStream(imgbyte);//5.读取数组

byte[] buf1=new byte[1024];intlen1;while((len1=bais.read(buf1))!=-1) {

fos.write(buf1,0,len1);

}

fis.close();

fos.close();

bais.close();

baos.close();

}

}

2.打印流

PrintSream:操作字节,自动刷新,可设置字符集

PrintWriter :不能操作字节,内部有缓冲区

打印字节流实例:

packagecom.test;importjava.io.BufferedOutputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.PrintStream;public classPrintStreamDemo {//打印流(字节)

public static void main(String[] args) throwsIOException {//TODO Auto-generated method stub//PrintStream ps=new PrintStream("E:\\aaa.txt");

BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("E:\\xx.txt"));

PrintStream ps=new PrintStream(bos,true);

ps.print("123456");

ps.print(true);

ps.println("abcdef");

ps.printf("%d", 200);

ps.printf("%.2f", 3.1415926);

ps.printf("%s", "我爱生活");

ps.printf("%x", 256);

}

}

打印字符流实例:

packagecom.test;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.FileWriter;importjava.io.IOException;importjava.io.PrintWriter;public classPrintWriterDemo {public static void main(String[] args) throwsIOException {//TODO Auto-generated method stub

PrintWriter pw=new PrintWriter(new FileOutputStream("E:\\bbb.txt"));

pw.println("123");

pw.close();

}

}

3.对象序列化与反序列化(对象流)

本例举一个学生类Student.class序列化和反序列化的过程。

创建的学生类:

packagecom.test;importjava.io.Serializable;public class Student implementsSerializable {private static final long serialVersionUID=123L;//序列化与反序列化的唯一标记

private int StuNo;//序列化和访问权限没有关系

String name;//transient int age;//transient 瞬时的,不能序列化瞬时的属性//static String address="北京";//静态变量不能被序列化

publicStudent() {super();//TODO Auto-generated constructor stub

}public Student(intstuNo, String name) {super();

StuNo=stuNo;this.name =name;

}public intgetStuNo() {returnStuNo;

}public void setStuNo(intstuNo) {

StuNo=stuNo;

}publicString getName() {returnname;

}public voidsetName(String name) {this.name =name;

}

}

使用对象类序列化和反序列化学生类:

packagecom.test;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.ObjectInputStream;importjava.io.ObjectOutputStream;public classSerializeDemo {public static void main(String[] args) throwsIOException, ClassNotFoundException {//序列化过程://1.创建序列化对象

Student stu=new Student(1101,"小明");//2.创建文件输出流

FileOutputStream fos=new FileOutputStream("E:\\receive.bin");//3.创建对象流接入输出流

ObjectOutputStream oos=newObjectOutputStream(fos);//4.对象流输出序列化对象保存在文件中

oos.writeObject(stu);

oos.close();//反序列化过程://1.创建对象流

FileInputStream fis=new FileInputStream("E:\\receive.bin") ;

ObjectInputStream ois=newObjectInputStream(fis);//2.反序列化

Student stu1=(Student)ois.readObject();

ois.close();

System.out.println("学号:"+stu1.getStuNo());

System.out.println("姓名:"+stu1.getName());

}

}

控制台输出信息(表明反序列化成功):

05d8f9e3f8c01824b03497bc077730a4.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值