Java IO流

本文详细介绍了Java中File类的构造方法,如通过路径名创建文件和目录,以及FileInputStream、FileOutputStream、FileReader和FileWriter等字节和字符流的使用示例。涵盖了mkdirs(), createNewFile(), delete(), exists(), getName(), isDirectory(), isFile()等方法,以及如何进行文件操作和数据流传输。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文件操作(File)

构造方法

  • File(File parent, String child) 从父抽象路径名和子路径名字符串创建新的 File实例。
  • File(String pathname) 通过将给定的路径名字符串转换为抽象路径名来创建新的 File实例。
  • File(String parent, String child) 从父路径名字符串和子路径名字符串创建新的 File实例。

常用方法

  • boolean mkdirs() 创建由此抽象路径名命名的目录,包括任何必需但不存在的父目录。
  • boolean createNewFile() 当且仅当具有该名称的文件尚不存在时,原子地创建一个由该抽象路径名命名的新的空文件。
  • boolean delete() 删除由此抽象路径名表示的文件或目录。
  • boolean exists() 测试此抽象路径名表示的文件或目录是否存在。
  • String getName() 返回由此抽象路径名表示的文件或目录的名称。
  • boolean isDirectory() 测试此抽象路径名表示的文件是否为目录。
  • boolean isFile() 测试此抽象路径名表示的文件是否为普通文件。

字节输入输出流(FileInputStream和FileOutputStream)

从文件读取到内存中称作输入;从内存写入到文件称作输出

字节输入流(FileInputStream)

构造方法

  • FileInputStream(File file) 通过打开与实际文件的连接创建一个 FileInputStream ,该文件由文件系统中的 File对象 file命名。
  • FileInputStream(String name) 通过打开与实际文件的连接来创建一个 FileInputStream ,该文件由文件系统中的路径名 name命名。

常用方法

  • void close() 关闭此文件输入流并释放与流相关联的任何系统资源。
  • int read() 从该输入流读取一个字节的数据。
  • int read(byte[] b) 从该输入流读取最多 b.length个字节的数据为字节数组。
  • int available() 返回从此输入流中可以读取(或跳过)的剩余字节数的估计值,而不会被下一次调用此输入流的方法阻塞。

代码示例

FileInputStream fis = null;
try {
     fis = new FileInputStream("a.txt");
     // 创建文件剩余可读取的最大字节数的容量的字节数组
     byte[] resByte = new byte[fis.available()];
     System.out.println(Arrays.toString(resByte));
     // 读取内容,返回读取了多少字节
     int res = fis.read(resByte);
     System.out.println(res);
     // 将读取的内容以UTF8编码方式转成字符串
     String resStr = new String(resByte, StandardCharsets.UTF_8);
     System.out.println(resStr);
 } catch (Exception e) {
     e.printStackTrace();
 } finally {
     if (fis != null) {
         try {
             fis.close();
         } catch (Exception e) {
             e.printStackTrace();
         }
     }
 }

字节输出流(FileOutputStream)

构造方法

  • FileOutputStream(File file) 创建文件输出流以写入由指定的 File对象表示的文件。
  • FileOutputStream(File file, boolean append) 创建文件输出流以写入由指定的 File对象表示的文件,并根据append判断是否追加写入。
  • FileOutputStream(String name) 创建文件输出流以指定的名称写入文件。
  • FileOutputStream(String name, boolean append) 创建文件输出流以指定的名称写入文件,并根据append判断是否追加写入。

常用方法

  • void close() 关闭此文件输出流并释放与此流相关联的任何系统资源。
  • void write(byte[] b) 将 b.length个字节从指定的字节数组写入此文件输出流。
  • void write(int b) 将指定的字节写入此文件输出流。

代码示例

FileOutputStream fos = null;
try {
    fos = new FileOutputStream("a.txt");
    // 写入数据
    String writeStr = "的时间咖啡";
    fos.write(writeStr.getBytes(StandardCharsets.UTF_8));
} catch (Exception e) {
    e.printStackTrace();
} finally {
    if (fos != null) {
        try {
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

字符输入输出流(FileReader和FileWriter)

字符输入输出流用于文本文档,以避免乱码问题。

字符输入流(FileReader)

字符输入流,用默认的适当的编码方式读取文本文件中的字符,如果要指定编码方式使用:InputStreamReader

代码示例

try {
    FileInputStream fis = new FileInputStream("a.txt");
    // 创建字符输入流对象
    InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8);
    // 读取内容
    int i = 0;
    while ((i=isr.read()) != -1) {
        System.out.print((char) i);
    }
    isr.close();
} catch (Exception e) {
    e.printStackTrace();
}

字符输出流(FileWriter)

字符输出流,用默认的适当的编码方式将字符写入到文件中,如果要指定编码方式使用:OutputStreamWriter

代码示例

try {
    FileOutputStream fos = new FileOutputStream("a.txt");
    // 创建字符输出流对象
    OutputStreamWriter osw = new OutputStreamWriter(fos, StandardCharsets.UTF_8);
    // 向文件中写入内容
    osw.write("的河南省开封呢");
    osw.close();
} catch (Exception e) {
    e.printStackTrace();
}

对象输入输出流(ObjectInputStream和ObjectOutputStream)

将对象存入文本中,读取后直接是一个对象

对象输出流(ObjectOutputStream)

代码示例:

try {
    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("a.txt"));
    // 向文件中写入内容
    oos.writeObject(new Date());
    oos.close();
} catch (Exception e) {
    e.printStackTrace();
}

对象输入流

代码示例:

try {
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("a.txt"));
    // 读取文件中的内容
    Date date = (Date) ois.readObject();
    System.out.println(date);
    ois.close();
} catch (Exception e) {
    e.printStackTrace();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值