Class AutoDeleteTempFile
java.lang.Object
cloud.opencode.base.io.temp.AutoDeleteTempFile
- All Implemented Interfaces:
Closeable, AutoCloseable
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 TypeMethodDescriptionvoidclose()booleandelete()Deletes the file immediately 立即删除文件booleanexists()Checks if the file exists 检查文件是否存在getPath()Gets the file path 获取文件路径byte[]read()Reads file content as bytes 读取文件内容为字节Reads file content as string (UTF-8) 读取文件内容为字符串(UTF-8)readString(Charset charset) Reads file content as string with charset 使用指定字符集读取文件内容为字符串longsize()Gets file size 获取文件大小toFile()Gets the file as File object 获取File对象toString()voidwrite(byte[] content) Writes byte content to the file 写入字节内容到文件voidWrites string content to the file (UTF-8) 写入字符串内容到文件(UTF-8)voidWrites string content to the file with charset 使用指定字符集写入字符串内容到文件
-
Method Details
-
getPath
-
toFile
-
write
public void write(byte[] content) Writes byte content to the file 写入字节内容到文件- Parameters:
content- the content | 内容
-
write
Writes string content to the file (UTF-8) 写入字符串内容到文件(UTF-8)- Parameters:
content- the content | 内容
-
write
-
read
public byte[] read()Reads file content as bytes 读取文件内容为字节- Returns:
- byte array | 字节数组
-
readString
Reads file content as string (UTF-8) 读取文件内容为字符串(UTF-8)- Returns:
- content | 内容
-
readString
-
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:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
toString
-