zoukankan      html  css  js  c++  java
  • 字节输出流 OutputStream 总结

    字节输出流 OutputStream 总结

    • 简单理解:输出流针对文件来说就是 流的输出 到 文件的写入 ; 输入入流针对文件来说就是 文件的读取 到 流的输入 ;
    • OutputStream 是抽象类,依靠子类不同,同一方法实现的功能也不同。本次总结的是针对文件操作的子类 FileOutputStream
    • 所属包:java -- io
    • 因OutputStream是的抽象类,所以需要通过实例化子类来获取其对象。子类总结如下:
    文件操作子类:FileOutputStream
    其它子类待补充
    
    • 实现接口的观察 public abstract class OutputStream extends Object implements Closeable, Flushable
    实现了两个接口 Closeable  和 Flushable ,其中Closeable 接口中只有一个close抽象方法,Flushable接口中只有一个 flush抽象方法
    但是,但是,但是,OutputStream 本身是抽象类,却定义好了close()和flush() 这两个普通方法,所以,无需关注他的接口
    
    • OutputStream 抽象类一共五个(可以简单理解为三个)方法
    1、关闭操作(普通方法):public void close() throws IOException
    2、刷新操作(普通方法):public void flush() throws IOException
    3、写入整个数组(普通方法):public void write(byte[] b) throws IOException
    4、(抽象方法):public abstract void write(int b) throws IOException
    5、(普通方法):public void write(byte[] b, int off, int len) throws IOException
    

    文件操作子类FileOutputStream分析

    • 先来看构造,构造用于初始化资源操作信息,接受 File 类型参数
    1、创建或覆盖文件:public FileOutputStream(File file) throws FileNotFoundException
    2、是否要对文件内容追加:public FileOutputStream(File file, boolean append) throws FileNotFoundException
    	append 为 true 时 是追加,为 false 时 是创建或覆盖文件
    
    • 再来看方法,FileOutpubStream做为OutputStream子类,有扩充新方法,但是新方法暂时用不上,先以父类五个方法学习为主
    扩充方法暂时不学,留位未来补充
    
    • 第一个方法:close()
    \ 输入输出流做为资源操作类,打开了就一定要关闭。
    OutputStream实例化对象名.close() ;
    从1.7开始,Closeable 又继承了一个AutoCloseable 接口,用于实现自动关闭,细节待补充
    
    • 第二个方法:flush()
    待补充
    

    将String型数据写进文件中

    • 实验第三个方法:write(byte[] b)
    File file = new File("." + File.separator + "123" + File.separator + "ip6" + File.separator + "ttt.txt") ;
    if (!file.getParentFile().isDirectory()) file.mkdirs() ;
    OutputStream out = new FileOutputStream(file,true) ; // 如果每次都要覆写,就传一个参数,file
    String str = "好好学习,天天向上!
    " ;
    byte[] data = str.getBytes() ;   // 先将 String 型数据变为字节数组
    out.write(data) ;
    out.close() ;
    
    • 实验第四个方法:write(int b)
    File file = new File("." + File.separator + "123" + File.separator + "ip6" + File.separator + "ttt.txt") ;
    if (!file.getParentFile().isDirectory()) file.mkdirs() ;
    OutputStream out = new FileOutputStream(file,true) ; // 如果每次都要覆写,就传一个参数,file
    String str = "好好学习,天天向上!
    " ;
    byte[] data = str.getBytes() ;   // 先将 String 型数据变为字节数组
    for (byte x : data) {
    	out.write(x) ;  //别忘了这里是接受的int型数据,严谨一点就可以写成 out.write((int)x) ; 
    }
    out.close() ;
    

    实验第五个方法:write(byte[] b, int off, int len) 第二个参数是起始位置,第三个参数是长度

    File file = new File("." + File.separator + "123" + File.separator + "ip6" + File.separator + "ttt.txt") ;
    if (!file.getParentFile().isDirectory()) file.mkdirs() ;
    OutputStream out = new FileOutputStream(file,true) ; // 如果每次都要覆写,就传一个参数,file
    String str = "好好学习,天天向上!
    " ;
    byte[] data = str.getBytes() ;   // 先将 String 型数据变为字节数组
    out.write(data,3,6) ; 这样写进去的就是“好学”两个字,并且没有了换行
    out.close() ;
    
  • 相关阅读:
    2018年5月29号(堆排序最小顶)
    2018年5月31号(树状数组)
    2018年5月27号(spfa判断负环)
    2018年6月1号(线段树(1))
    2018年5月28号(差分约束)
    Inno Setup入门(十二)——Pascal脚本(1)
    Inno Setup入门(十六)——Inno Setup类参考(2)
    Inno Setup入门(二十一)——Inno Setup类参考(7)
    Inno Setup入门(十)——操作注册表
    Inno Setup入门(十八)——Inno Setup类参考(4)
  • 原文地址:https://www.cnblogs.com/haixianet/p/8696047.html
Copyright © 2011-2022 走看看