字节流:

  输入和输出:1.参照物都是java程序来惨遭

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

        2.Input输入,持久化上的数据----》内存

        3.Output输出,内存---》硬盘

  字节输出流:

    OutputStream:

      定义:流按照方向可以分为输入和输出流,字节流可以操作任何数据,字符流只能操作纯字符数据。

      IO流父类:OutputStream和InputStream

  IO流程序书写步骤:

       1.先导包

       2.进行异常处理

       3.释放资源

  方法:

      1.void close();

      2.Write(byte[ ]   b);Write(byte[ ],int off,int len );

    代码:

字节流 随笔 第1张
public class FileOutputStreamDemo { public static void main(String[] args) throws IOException { //步骤1创建流 子类对象 绑定数据目的 FileOutputStream fos= new FileOutputStream("c:\\aaa.txt"); //2 调用write() 方法 写一个字节 fos.write(97); //2.1 写字节数组 byte[] b={65,66,67,68}; fos.write(b); // 2.2 写字节数组的一部分 fos.write(b, 1, 2); //2.3写字符串 getBytes() 字符串转字节 fos.write("hello world".getBytes()); // 3 close 关闭资源  fos.close(); } }
字节流 随笔 第2张

  FileOutputStream(文件输出流):

    文件的续写和换行符号:

字节流 随笔 第3张
  /* \r\n换行 */ public static void main(String[] args) throws IOException { File file = new File("c:\\b.txt"); FileOutputStream fos = new FileOutputStream(file,true); fos.write("hello\r\n".getBytes()); fos.write("world".getBytes()); fos.close(); }
字节流 随笔 第4张

  字节输入流:

    InputStream: abstract int  read();读取下一个字节,返回-1读取文件结束。

    方法:read(byte[ ] b);close();

      代码;

字节流 随笔 第5张
public class FileInputStreamDemo { public static void main(String[] args) { try { FileInputStream fis = new FileInputStream("c:\\aaa.txt"); int len =0; while((len=fis.read())!=-1){ System.out.print((char)len); } } catch (IOException e) { // TODO Auto-generated catch block  e.printStackTrace(); } } }  
字节流 随笔 第6张

      字节数组读取:

   
字节流 随笔 第7张  
public static void main(String[] args) { try { FileInputStream fis = new FileInputStream("c:\\aaa.txt"); //创建字节数组 byte[] b = new byte[1024]; int len=0; while((len=fis.read(b))!=-1){ //字节数组转字符串 System.out.println(new String(b,0,len)); } } catch (Exception e) { // TODO Auto-generated catch block  e.printStackTrace(); } }
字节流 随笔 第8张
 
扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄