用途:IO工具类(将内容写到流中)
使用场景
IO工具类只是辅助流的读写,并不负责关闭流。原因是流可能被多次读写,读写关闭后容易造成问题。
(将内容写到流中)
(将内容写到流中)
(将内容写到流中)
项目引用
此博文的依据:hutool-5.6.5版本源码
<dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-core</artifactId> <version>5.6.5</version> </dependency>
File dest = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/writeTest6.txt") ; OutputStream outputStream = null; ObjectInputStream inputStream = null; FileInputStream fis = null; try { boolean isCloseOut = false; StudentDto student = new StudentDto(); student.setName("小虚竹"); student.setAge(18); student.setsNo("nb9527"); StudentDto student1 = new StudentDto(); student1.setName("张三"); student1.setAge(18); student1.setsNo("nb007"); outputStream = new ObjectOutputStream(new FileOutputStream(dest)); IoUtil.writeObjects(outputStream,isCloseOut,student,student1); IoUtil.close(outputStream); //创建序列化流对象 fis = new FileInputStream(dest); inputStream = new ObjectInputStream(fis); //读取 Object obj =null; //用available判断是否达到了文件末尾 while(fis.available()>0) { obj=inputStream.readObject(); System.out.println(obj); } }catch (Exception e) { //抛出一个运行时异常(直接停止掉程序) throw new RuntimeException("运行时异常",e); } finally { IoUtil.close(outputStream); IoUtil.close(inputStream); IoUtil.close(fis); }