• JAVA基础——IO流字节流


    在Java中把不同的输入输出源(键盘、文件、网路连接)抽象表述为“流”。

    1.输入流、输出流

    .字节输入流通过FileInputStream和来操作

    字节输出流通过FileOutputStream来操作

    2.字节流、字符流

    字节流通过InputStream和OutputStream来操作,数据单元是8位的字节

    字符流通过Reader和Writer来操作,数据单元是16位的字符

    3.节点流、处理流

    处理流可以包装节点流进行数据传输,通过处理流,Java程序无需理会输入输出节点是磁盘、网络还是其他输入输出设备,程序只要将节点包装成处理流,就可以使用相同的输入、输出代码来读不通的输入输出设备的数据。


    文件字节输出(写)

    方法一:

    public class 文件输入输出流 {
     public static void main(String[] args) throws Exception {

     
      //1、确定目标文件
      File file = new File("D:\ccc.txt");
      FileOutputStream fos = null;
      //2、获取流
      try {
       fos = new FileOutputStream(file,true);
     //3、获取目标数据
      String str = "hello world!!!";
      //4、通过流提供的一些列方法 将目标数据 写入目标文件
      fos.write(str.getBytes());
      
     int num = 1/0;//除0异常
     } catch (Exception e) {
       // TODO Auto-generated catch block
     e.printStackTrace();
    }finally{
       //finally无论是否发生异常 finally中的代码 都会执行

       //5、关闭流
        try {
        fos.close();
        } catch (IOException e) {
         // TODO Auto-generated catch block
      e.printStackTrace();
        }
      }
      
      /**
       *方法二:
       */
      //1、确定目标文件
      File file = new File("D:\ccc.txt");
      //2、确定流
      FileOutputStream fos = new FileOutputStream(file);
      //3、确定数据
      fos.write(97);

    //4、关闭流
      fos.close();
      
      /**
       *方法三:
       */
    //1、确定目标文件
     File file = new File("D:\ccc.txt");
     //2、确定流
      FileOutputStream fos = new FileOutputStream(file);
    //3、确定数据
     String str = "hello world!!!";
     //4、写入文件
    //  /*参数一:目标数组
    //   * 参数二:从哪里开始写
    //   * 参数三:一共写入多少个
    //   * write(b, off, len);
    //   */
      fos.write(str.getBytes(), 2, 5);

       //5、关闭流
      fos.close();
      
      


    从文件中将数据读取出来  

    方法一:

    public class 文件输入输出流 {  public static void main(String[] args) throws Exception {

    //1、确定目标文件

      File file = new File("D:\ccc.txt");
      //2、获取流对象
     try {
      FileInputStream fis = new FileInputStream(file);
     //3、读取数据
     byte[] b = new byte[1024];//用于接受数据
      fis.read(b);
     //4、打印数据
    //   /**
    //    * 参数一:目标构造数组
    //    * 参数二:从哪里开始构造
    //    * 参数三:构造的长度
    //    * String(byte[] bytes, int offset, int length)
    //    * */
      System.out.println(new String(b,0,(int)file.length()));
      } catch (Exception e) {
       // TODO Auto-generated catch block
      e.printStackTrace();
      }
      
      /**
       * 方法二:
       */
     //1、确定目标文件
      File file = new File("D:\ccc.txt");
      //2、确定流
      FileInputStream fis = new FileInputStream(file);
      //3、开始读
      byte[] b = new byte[1024];
      fis.read(b, 0, 3);

    //4、关闭流
     fis.close();
      
     System.out.println(new String(b));
      
      /**
       * 方法三:
       */
    //1、确定目标文件
     File file = new File("D:\ccc.txt");
      //2、确定流
      FileInputStream fis = new FileInputStream(file);
    //3、开始读数据
      /*
       * 注意:之前的读取方法 我们都是将数据 直接读到一个byte数组中
       * read():从文件中一个一个的读出来 
       * 如果有数据 读出来的是 ,每个字母对应的ascll码值
       * 如果读到最后 返回-1
       * 利用循环读出
       */
      StringBuffer sb = new StringBuffer();//用于拼接
      int temp = 0;//用于临时存储每个字节
      
      while((temp = fis.read())!= -1){
       sb.append((char)temp);
      }

    //4、关闭流
      fis.close();
      
      System.out.println(sb.toString());

  • 相关阅读:
    生成水印图片
    Java 生成图片-字体杂色去除
    java比较两个日期大小
    JS--添加option
    使用JS刷新showModalDialog窗口
    日期格式转换(String->Date)
    根据当前日期及出生日期,计算当前年龄(function)
    PHP的错误和异常处理
    IntelliJ IDEA15,PhpStorm10,WebStorm11激活破解
    apache相关配置
  • 原文地址:https://www.cnblogs.com/huan-guo/p/8510260.html
Copyright © 2020-2023  润新知