Class AutoDeleteTempFile

java.lang.Object
cloud.opencode.base.io.temp.AutoDeleteTempFile
All Implemented Interfaces:
Closeable, AutoCloseable

public class AutoDeleteTempFile extends Object implements Closeable
Auto Delete Temp File 自动删除临时文件

A temporary file that automatically deletes itself when closed. Implements Closeable for use with try-with-resources.

关闭时自动删除自身的临时文件。 实现Closeable以便与try-with-resources一起使用。

Features | 主要功能:

  • Auto delete on close - 关闭时自动删除
  • Convenient read/write methods - 便捷的读写方法
  • Try-with-resources support - 支持try-with-resources

Usage Examples | 使用示例:

try (AutoDeleteTempFile temp = OpenTempFile.createAutoDeleteTempFile("data", ".json")) {
    temp.write("{\"key\": \"value\"}");
    processFile(temp.getPath());
} // File is automatically deleted

Security | 安全性:

  • Thread-safe: No - 线程安全: 否
Since:
JDK 25, opencode-base-io V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    void
     
    boolean
    Deletes the file immediately 立即删除文件
    boolean
    Checks if the file exists 检查文件是否存在
    Gets the file path 获取文件路径
    byte[]
    Reads file content as bytes 读取文件内容为字节
    Reads file content as string (UTF-8) 读取文件内容为字符串(UTF-8)
    Reads file content as string with charset 使用指定字符集读取文件内容为字符串
    long
    Gets file size 获取文件大小
    Gets the file as File object 获取File对象
     
    void
    write(byte[] content)
    Writes byte content to the file 写入字节内容到文件
    void
    write(String content)
    Writes string content to the file (UTF-8) 写入字符串内容到文件(UTF-8)
    void
    write(String content, Charset charset)
    Writes string content to the file with charset 使用指定字符集写入字符串内容到文件

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Method Details

    • getPath

      public Path getPath()
      Gets the file path 获取文件路径
      Returns:
      path | 路径
    • toFile

      public File toFile()
      Gets the file as File object 获取File对象
      Returns:
      File object | File对象
    • write

      public void write(byte[] content)
      Writes byte content to the file 写入字节内容到文件
      Parameters:
      content - the content | 内容
    • write

      public void write(String content)
      Writes string content to the file (UTF-8) 写入字符串内容到文件(UTF-8)
      Parameters:
      content - the content | 内容
    • write

      public void write(String content, Charset charset)
      Writes string content to the file with charset 使用指定字符集写入字符串内容到文件
      Parameters:
      content - the content | 内容
      charset - the charset | 字符集
    • read

      public byte[] read()
      Reads file content as bytes 读取文件内容为字节
      Returns:
      byte array | 字节数组
    • readString

      public String readString()
      Reads file content as string (UTF-8) 读取文件内容为字符串(UTF-8)
      Returns:
      content | 内容
    • readString

      public String readString(Charset charset)
      Reads file content as string with charset 使用指定字符集读取文件内容为字符串
      Parameters:
      charset - the charset | 字符集
      Returns:
      content | 内容
    • exists

      public boolean exists()
      Checks if the file exists 检查文件是否存在
      Returns:
      true if exists | 如果存在返回true
    • size

      public long size()
      Gets file size 获取文件大小
      Returns:
      size in bytes | 字节大小
    • delete

      public boolean delete()
      Deletes the file immediately 立即删除文件
      Returns:
      true if deleted | 如果删除成功返回true
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
    • toString

      public String toString()
      Overrides:
      toString in class Object