Java-I/O流

简介

  • I/O流:java中用于执行输入和输出的操作,I/O流分为输入流和输出,输入流是将文件或者其他输入设备加载到内存中的过程,输出流是将内存中的数据保存到文件或者其他输出设备
  • 分类
    • 字节流:文件通常是由一连串的字节或字符构成的,组成文件的字节序列称为字节流
    • 字符流:组成文件的字符序列称为字符流

创建文件的方式

  • 创建一个java文件对象:要创建对象FIle,我们需要先导入java.io.File包

    1
    File file = new File(文件路径);
  • 根据父目录File对象,在子路径创建一个文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    public static void main(String[] args) throws IOException {
    File parentpath = new File("D:\\idea\\project\\untitled\\src");
    File file = new File(parentpath,"2.txt");
    try{
    file.createNewFile();
    System.out.println("创建成功");
    }catch (IOException e){
    e.printStackTrace();
    }
    }
  • 根据父目录路径,在子路径下生成文件

    1
    new File(String parent, String child)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    public static void main(String[] args) throws IOException {
    String parentpath = "D:\\idea\\project\\untitled\\src";
    String fileName = "3.txt";
    File file = new File(parentpath, fileName);
    try {
    file.createNewFile();
    System.out.println("文件创建成功");
    }catch (IOException e) {
    e.printStackTrace();
    }
    }

获取文件信息

  • 使用file类的方法名进行一些基本信息的获取

  • 常见的方法

    • getName:获取文件名
    • getAbsolutePath:获取文件的绝对路径
    • getParent:获取文件的父级目录
    • length:获取文件的大小
    • isFile:判断是否是文件
    • isDirectory:判断是否是目录
    • exists:判断文件是否存在
  • demo

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    public static void main(String[] args) throws IOException {
    File file = new File("D:\\idea\\project\\untitled\\src\\1.txt");
    System.out.println("文件名:"+file.getName());
    System.out.println("文件的绝对路径:"+file.getAbsolutePath());
    System.out.println("文件的父级目录:"+file.getParent());
    System.out.println("文件的大小:"+file.length());
    System.out.println("这是否是一个文件:"+file.isFile());
    System.out.println("这是否是一个目录:"+file.isDirectory());
    System.out.println("该文件是否存在:"+file.exists());
    }

目录与文件操作

  • delete:删除文件

    1
    2
    3
    4
    5
    6
    public static void main(String[] args) throws IOException {
    File file = new File("D:\\idea\\project\\untitled\\src\\1.txt");
    System.out.println("该文件是否存在:"+file.exists());
    file.delete();
    System.out.println("该文件是否存在:"+file.exists());
    }
  • 目录删除:delete方法可以删除目录,但是只能删除空目录

    1
    2
    3
    4
    5
    6
    public static void main(String[] args) throws IOException {
    File file = new File("D:\\idea\\project\\untitled\\test");
    System.out.println("该目录是否存在:"+file.exists());
    file.delete();
    System.out.println("该文件是否存在:"+file.exists());
    }
  • 创建单级目录:mkdir

    1
    2
    3
    4
    5
    public static void main(String[] args) throws IOException {
    File file = new File("D:\\idea\\project\\untitled\\test");
    file.mkdir();
    System.out.println(file.exists());
    }
  • 创建多级目录:mkdirs

    1
    2
    3
    4
    5
    public static void main(String[] args) throws IOException {
    File file = new File("D:\\idea\\project\\untitled\\test1\\test123");
    file.mkdirs();
    System.out.println(file.exists());
    }

InputStream

  • 简介:InputStream 是I/O流中用于读取字节数据的抽象基类,可以通过子类来创建具体的输入流对象

  • 常见命令执行的payload

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    public static void main(String[] args) throws IOException {
    //启动进程并获取输入流
    InputStream inputStream = Runtime.getRuntime().exec("calc").getInputStream();
    //定义缓冲区
    byte[] cache = new byte[1024];
    //临时存储读取到的数据
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    int len=0;
    //循环读取进程并进行输出
    while ((len = inputStream.read(cache)) != -1) {
    outputStream.write(cache, 0, len);
    }
    System.out.println(outputStream);
    }

FileInputStream

  • 简介:该流用于从文件读取数据

  • 创建一个输入流对象来读取文件

    1
    InputStream inputStream = new FileInputStream("D:\\idea\\project\\untitled\\src\\1.txt");
  • 常见方法

    • read():读取一个字节的数据

      1
      2
      3
      4
      public static void main(String[] args) throws IOException {
      InputStream inputStream = new FileInputStream("D:\\idea\\project\\untitled\\src\\1.txt");
      System.out.println("文件的第一个字节的数据是:"+inputStream.read());
      }
    • read(byte[] b):从输入流中读取字节

      1
      2
      3
      4
      5
      public static void main(String[] args) throws IOException {
      InputStream inputStream = new FileInputStream("D:\\idea\\project\\untitled\\src\\1.txt");
      byte[] buffer = new byte[1024];
      System.out.println("文件的读取的字节数是:"+inputStream.read(buffer,0,buffer.length));
      }

FileOutputStream

  • 简介:该类用来创建一个文件并向文件中写数据

  • 创建一个输出流对象

    1
    OutputStream outputStream = new FileOutputStream("D:\\idea\\project\\untitled\\src\\1.txt");
  • 常见方法

    • wirte(int b):将指定字节写入输出流

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      public static void main(String[] args) throws IOException {
      String filepath = "D:\\idea\\project\\untitled\\src\\1.txt";
      OutputStream outputStream = new FileOutputStream(filepath);
      String content = "Hello World!";
      outputStream.write(content.getBytes());
      byte[] cache = new byte[20];
      int len=0;
      try {
      InputStream inputStream = new FileInputStream(filepath);
      while((len=inputStream.read(cache))!=1){
      System.out.println(new String(cache,0,len));
      }
      }catch (Exception e){
      }
      }
    • write(byte[] b):将字节数组b中的所有字节写入输出流

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      public static void main(String[] args) throws IOException {
      String filepath = "D:\\idea\\project\\untitled\\src\\1.txt";
      OutputStream outputStream = new FileOutputStream(filepath);
      byte[] content= "Hello World!".getBytes();
      outputStream.write(content);
      byte[] cache = new byte[50];
      int len=0;
      try {
      InputStream inputStream = new FileInputStream(filepath);
      while((len=inputStream.read(cache))!=1){
      System.out.println(new String(cache,0,len));
      }
      }catch (Exception e){
      }
      }
    • write(byte[] b, int off, int len):将字节数组b中从偏移量off开始到len个字节写入输出流

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      public static void main(String[] args) throws IOException {
      String filepath = "D:\\idea\\project\\untitled\\src\\1.txt";
      OutputStream outputStream = new FileOutputStream(filepath);
      byte[] content= "Hello World!".getBytes();
      outputStream.write(content, 0, content.length);
      byte[] cache = new byte[50];
      int len=0;
      try {
      InputStream inputStream = new FileInputStream(filepath);
      while((len=inputStream.read(cache))!=1){
      System.out.println(new String(cache,0,len));
      }
      }catch (Exception e){
      }
      }
    • 追加写入

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      public static void main(String[] args) throws IOException {
      String filepath = "D:\\idea\\project\\untitled\\src\\1.txt";
      OutputStream outputStream = new FileOutputStream(filepath, true);
      byte[] content= "Hello World!".getBytes();
      outputStream.write(content, 0, content.length);
      byte[] cache = new byte[50];
      int len=0;
      try {
      InputStream inputStream = new FileInputStream(filepath);
      while((len=inputStream.read(cache))!=1){
      System.out.println(new String(cache,0,len));
      }
      }catch (Exception e){
      }
      }

文件拷贝

  • 从一个文件读取再写道另一个文件,文件要实现创建否则会报错

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    public static void main(String[] args) throws IOException {
    String inpath = "D:\\idea\\project\\untitled\\src\\1.txt";
    String outpath = "D:\\idea\\project\\untitled\\src\\2.txt";
    OutputStream outputStream = null;
    InputStream inputStream = null;
    try{
    outputStream = new FileOutputStream(outpath);
    inputStream = new FileInputStream(inpath);
    byte[] buffer = new byte[1024];
    int len;
    while((len = inputStream.read(buffer)) != -1){
    outputStream.write(buffer,0,len);
    }
    }catch(IOException e){
    e.printStackTrace();
    }
    }

FileReader

  • 简介:用来读取字符文件的便捷类

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    public static void main(String[] args) throws IOException {
    String filepath = "D:\\idea\\project\\untitled\\src\\1.txt";
    FileReader fileReader = null;
    try {
    fileReader = new FileReader(filepath);
    int len = 0;
    char[] buffer = new char[1024];
    while ((len = fileReader.read(buffer)) != -1) {
    System.out.println(new String(buffer, 0, len));
    }
    }catch (FileNotFoundException e) {
    e.printStackTrace();
    }
    }

参考

参考自https://drun1baby.github.io/2022/05/30/Java-IO%E6%B5%81/