• FileOutputStream字节输出流和FileInputStream输入流(切记:out是输出到本地中,in是输入到程序中)这里介绍大文件和小文件的读取方式


    //FileOutputStream

    public class FileOutputStreamDemo {
     /**字节流:适用于任何文件,以字节为单位,进行读写操作
      *字节流操作步骤:
      *1、创建文件对象
      *2、创建字节流
      *3、读写操作
      *4、关闭流
      */
     //字节流(写操作)
     public static void main(String[] args) {

    String messageString = "hello world";
      byte[] bytes = messageString.getBytes();
      //1、创建文件对象
      File file = new File("abc.txt");  
      FileOutputStream fos = null;
      try {
       //2、创建字节流
       fos = new FileOutputStream(file,true);//参数一:file对象(也可以是一个文件路径) 参数二:true表示追加模式 false表示覆盖模式
       //3、写操作
       fos.write(bytes);
       System.out.println("写入成功!");
      } catch (FileNotFoundException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }finally{
       try {
        //4、关闭流
        if(fos!=null){
         fos.close();
        }
       } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
       }
      }

    }

    }

    //FileInputStream

    public class FileInputStreamDemo {
     //字节流(读小文件操作)
    // public static void main(String[] args) {

    //  //1、创建文件对象
    //  File file = new File("abc.txt");
    //  FileInputStream fis = null;
    //  try {
    //   //2、创建字节流
    //   fis = new FileInputStream(file);
    //   byte[] bytes = new byte[(int)file.length()];//创建存储从文件读取到的字节数据
    //   //3、读操作
    //   fis.read(bytes);
    //   String str = new String(bytes);//将字节数组转换为字符串
    //   System.out.println(str);
    //  } catch (FileNotFoundException e) {
    //   e.printStackTrace();
    //  } catch (IOException e) {
    //   e.printStackTrace();
    //  }finally{
    //   try {
    //    //4、关闭流
    //    if(fis!=null){
    //     fis.close();
    //    }
    //   } catch (IOException e) {
    //    e.printStackTrace();
    //   }
    //  }
    // }

    //字节流(读大文件操作)
     public static void main(String[] args) {

    File file = new File("abc.txt");
      FileInputStream fis = null;

      //一般byte数组的大小为512,,1024,2048
      byte[] bytes = new byte[1024];//存储每次读取的字节数据
      int len = 0;//存储每次读取的字节数
      try {
       fis = new FileInputStream(file);
       while((len=fis.read(bytes))!=-1){
        //参数一:字节数组   参数二:从数组的哪个下标开始转换为字符串   参数三:要转换的字节数
        String str = new String(bytes,0,len);
        System.out.println(str);
       }
      } catch (FileNotFoundException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }finally{
       try {
        if(fis!=null){
         fis.close();
        }
       } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
       }
      }

    }

    }

  • 相关阅读:
    mysql 存中文乱码
    解决办法:Message: 对实体 "useUnicode" 的引用必须以 ';' 分隔符结尾
    windows无法安装到这个磁盘选中的磁盘具有MBR分区表解决办法
    Windows快速添加开机启动项
    使用markdown做本地笔记软件/编辑器
    Apache负载均衡配置
    setuptools包的使用
    Git clone报错Someone Could Be Eavesdropping On You Right Now (Man-In-The-Middle Attack)!
    Code Review 的项目
    Pytorch 之Torch
  • 原文地址:https://www.cnblogs.com/danmao/p/3825239.html
Copyright © 2020-2023  润新知