IO流学习之字节流写入

    • File
      • 构造方法
        • File(String path)
        • File(File parent, String child) 父文件路径,
        • File(String parent, String path) 父文件路径的字符串
      • 常用方法
        • createNewFile() : 创建文件
        • mkdir() :创建单级目录
        • mkdirs(): 创建多级目录
        • String getName():获取文件名
        • File[] listFiles() : 返回该文件夹下所有的文件绝对路径,以数组模式返回,可以增强for遍历。
        • String[] list() : 返回该文件夹下说有文件名,以数组返回
        • String getAbsoluteFile(): 返回该文件的绝对路径
        • String getParent(): 返回该文件的上级目录的路径
        • delete() :删除
        • isFile() :判断是否为文件
    • IO流
      • 输入流
        • 字符流(记事本能打开的文本文件)
        • 字节流(万能流)
          • InputStream
      • 输出流
        • 字符流(记事本能打开的文本文件)
        • 字节流(万能流)
          • OutputStream
            • FileOutputStream写入文件
              • 构造方法
              • new FileOutputStream(file, boolean append)
              • 追加写入时则,将构造方法后面的参数改为true即可。
            • write()以字节为单位一个一个的写入,可以用String的getBytes得到字节数组,写入
            • 每次执行完都要执行close()方法,关闭文件写入流,并释放系统资源
            • 每一次调用最好都进行一次合理的try{}catch{}finally{}规范
            • 如下
            •     
              FileOutputStream fos = null;
              try {
              fos = new FileOutputStream("xxxx", true);
              fos.write("xxxr".getBytes());
              } catch (IOException e) {
              e.printStackTrace();
              }finally {
              if (fos != null) {
              try {
              fos.close();
              } catch (IOException e1) {
              e1.printStackTrace();
              }
              }
              }
    •  
posted @ 2020-09-16 20:38  ZeroDuck  阅读(205)  评论(0)    收藏  举报