Comprehensive Java utility library providing collections, strings, beans, dates, I/O, and numerous other utility functions.
—
File operations, stream handling, resource management, and NIO utilities through FileUtil and IoUtil classes.
Read file contents in various formats and encodings.
/**
* Read file content as UTF-8 string
* @param file the file to read
* @return file content as string
* @throws IORuntimeException if reading fails
*/
public static String readUtf8String(File file);
/**
* Read file content as string with specified charset
* @param file the file to read
* @param charset charset to use for reading
* @return file content as string
* @throws IORuntimeException if reading fails
*/
public static String readString(File file, Charset charset);
/**
* Read file content as byte array
* @param file the file to read
* @return file content as bytes
* @throws IORuntimeException if reading fails
*/
public static byte[] readBytes(File file);
/**
* Read file lines as list
* @param file the file to read
* @param charset charset to use for reading
* @return list of lines
* @throws IORuntimeException if reading fails
*/
public static List<String> readLines(File file, Charset charset);
/**
* Read file lines as UTF-8 list
* @param file the file to read
* @return list of lines in UTF-8
* @throws IORuntimeException if reading fails
*/
public static List<String> readUtf8Lines(File file);Usage Examples:
import cn.hutool.core.io.FileUtil;
import java.io.File;
import java.nio.charset.StandardCharsets;
File textFile = new File("example.txt");
// Read file content
String content = FileUtil.readUtf8String(textFile);
String gbkContent = FileUtil.readString(textFile, Charset.forName("GBK"));
byte[] bytes = FileUtil.readBytes(textFile);
// Read lines
List<String> lines = FileUtil.readUtf8Lines(textFile);
List<String> gbkLines = FileUtil.readLines(textFile, Charset.forName("GBK"));Write content to files in various formats and encodings.
/**
* Write string content to file using UTF-8 encoding
* @param content content to write
* @param file target file
* @return target file
* @throws IORuntimeException if writing fails
*/
public static File writeUtf8String(String content, File file);
/**
* Write string content to file using specified charset
* @param content content to write
* @param file target file
* @param charset charset to use for writing
* @return target file
* @throws IORuntimeException if writing fails
*/
public static File writeString(String content, File file, Charset charset);
/**
* Write byte array to file
* @param data bytes to write
* @param file target file
* @return target file
* @throws IORuntimeException if writing fails
*/
public static File writeBytes(byte[] data, File file);
/**
* Append string content to file using UTF-8 encoding
* @param content content to append
* @param file target file
* @return target file
* @throws IORuntimeException if writing fails
*/
public static File appendUtf8String(String content, File file);
/**
* Write lines to file
* @param lines lines to write
* @param file target file
* @param charset charset to use
* @return target file
* @throws IORuntimeException if writing fails
*/
public static File writeLines(Collection<String> lines, File file, Charset charset);Perform file system operations like copying, moving, and deleting.
/**
* Copy file or directory
* @param src source file or directory
* @param dest destination file or directory
* @param isOverride whether to override existing files
* @return destination file
* @throws IORuntimeException if copying fails
*/
public static File copy(File src, File dest, boolean isOverride);
/**
* Copy file content only (not directory structure)
* @param src source file
* @param dest destination file
* @param isOverride whether to override existing file
* @return destination file
* @throws IORuntimeException if copying fails
*/
public static File copyContent(File src, File dest, boolean isOverride);
/**
* Move file or directory
* @param src source file or directory
* @param dest destination file or directory
* @param isOverride whether to override existing files
* @return destination file
* @throws IORuntimeException if moving fails
*/
public static File move(File src, File dest, boolean isOverride);
/**
* Delete file or directory recursively
* @param file file or directory to delete
* @return true if deletion successful
*/
public static boolean del(File file);
/**
* Create directory and parent directories if not exist
* @param dir directory to create
* @return created directory
*/
public static File mkdir(File dir);
/**
* Create file and parent directories if not exist
* @param file file to create
* @return created file
* @throws IORuntimeException if creation fails
*/
public static File touch(File file);Check file properties and validate file operations.
/**
* Check if file or directory exists
* @param path file path
* @return true if exists
*/
public static boolean exist(String path);
/**
* Check if file or directory exists
* @param file file object
* @return true if exists
*/
public static boolean exist(File file);
/**
* Check if path represents a file (not directory)
* @param file file object
* @return true if is file
*/
public static boolean isFile(File file);
/**
* Check if path represents a directory
* @param file file object
* @return true if is directory
*/
public static boolean isDirectory(File file);
/**
* Get file size in bytes
* @param file file object
* @return file size in bytes, -1 if file doesn't exist
*/
public static long size(File file);
/**
* Get file extension
* @param file file object
* @return file extension without dot, null if no extension
*/
public static String extName(File file);
/**
* Get file name without extension
* @param file file object
* @return file name without extension
*/
public static String mainName(File file);Handle input/output streams with automatic resource management.
/**
* Copy data from input stream to output stream
* @param in input stream
* @param out output stream
* @return number of bytes copied
* @throws IORuntimeException if copying fails
*/
public static long copy(InputStream in, OutputStream out);
/**
* Copy data with specified buffer size
* @param in input stream
* @param out output stream
* @param bufferSize buffer size for copying
* @return number of bytes copied
* @throws IORuntimeException if copying fails
*/
public static long copy(InputStream in, OutputStream out, int bufferSize);
/**
* Read input stream content as string
* @param in input stream
* @param charset charset to use for reading
* @return content as string
* @throws IORuntimeException if reading fails
*/
public static String read(InputStream in, Charset charset);
/**
* Read input stream content as UTF-8 string
* @param in input stream
* @return content as UTF-8 string
* @throws IORuntimeException if reading fails
*/
public static String readUtf8(InputStream in);
/**
* Read input stream content as byte array
* @param in input stream
* @return content as bytes
* @throws IORuntimeException if reading fails
*/
public static byte[] readBytes(InputStream in);
/**
* Close closeable resource safely (no exception thrown)
* @param closeable resource to close
*/
public static void close(Closeable closeable);
/**
* Close multiple closeable resources safely
* @param closeables resources to close
*/
public static void close(Closeable... closeables);Usage Examples:
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import java.io.*;
// File operations
File sourceFile = new File("source.txt");
File destFile = new File("destination.txt");
// Writing
FileUtil.writeUtf8String("Hello World", destFile);
FileUtil.appendUtf8String("\nNew line", destFile);
// Copying
FileUtil.copy(sourceFile, destFile, true); // Override if exists
// File properties
boolean exists = FileUtil.exist("example.txt");
long size = FileUtil.size(new File("example.txt"));
String extension = FileUtil.extName(new File("example.txt")); // "txt"
// Stream operations
try (InputStream in = new FileInputStream("input.txt");
OutputStream out = new FileOutputStream("output.txt")) {
long copied = IoUtil.copy(in, out);
System.out.println("Copied " + copied + " bytes");
}
// Safe resource closing
FileInputStream fis = null;
try {
fis = new FileInputStream("file.txt");
String content = IoUtil.readUtf8(fis);
} finally {
IoUtil.close(fis); // Safe close, no exception thrown
}Modern NIO-based file operations for better performance.
/**
* Copy file using NIO
* @param src source file path
* @param dest destination file path
* @return destination path
* @throws IORuntimeException if copying fails
*/
public static Path copy(Path src, Path dest);
/**
* Move file using NIO
* @param src source file path
* @param dest destination file path
* @return destination path
* @throws IORuntimeException if moving fails
*/
public static Path move(Path src, Path dest);
/**
* Delete file or directory using NIO
* @param path path to delete
* @return true if deletion successful
*/
public static boolean del(Path path);
/**
* Read file content using NIO
* @param path file path
* @return file content as string
* @throws IORuntimeException if reading fails
*/
public static String readUtf8(Path path);
/**
* Write content to file using NIO
* @param path file path
* @param content content to write
* @return file path
* @throws IORuntimeException if writing fails
*/
public static Path writeUtf8(Path path, String content);Handle classpath resources and resource loading.
/**
* Get resource URL from classpath
* @param resource resource path
* @return resource URL, null if not found
*/
public static URL getResource(String resource);
/**
* Get resource as stream from classpath
* @param resource resource path
* @return resource stream, null if not found
*/
public static InputStream getResourceAsStream(String resource);
/**
* Read resource content as UTF-8 string
* @param resource resource path
* @return resource content
* @throws IORuntimeException if reading fails
*/
public static String readUtf8Str(String resource);
/**
* Read resource content as string with specified charset
* @param resource resource path
* @param charset charset to use
* @return resource content
* @throws IORuntimeException if reading fails
*/
public static String readStr(String resource, Charset charset);Detect file types based on file headers and content.
/**
* Get file type based on file header
* @param file file to check
* @return file type string (e.g., "jpg", "png", "pdf")
*/
public static String getType(File file);
/**
* Get file type based on input stream header
* @param in input stream
* @return file type string
*/
public static String getType(InputStream in);
/**
* Check if file is of specified type
* @param file file to check
* @param type expected file type
* @return true if file matches type
*/
public static boolean isType(File file, String type);Usage Examples:
// Resource operations
String config = ResourceUtil.readUtf8Str("config/application.properties");
InputStream stream = ResourceUtil.getResourceAsStream("data/template.xml");
// File type detection
String fileType = FileTypeUtil.getType(new File("image.jpg")); // "jpg"
boolean isPdf = FileTypeUtil.isType(new File("document.pdf"), "pdf");
// NIO operations
Path source = Paths.get("source.txt");
Path dest = Paths.get("destination.txt");
NioUtil.copy(source, dest);
String content = NioUtil.readUtf8(source);File and I/O operations use IORuntimeException for consistent error handling:
public class IORuntimeException extends RuntimeException {
public IORuntimeException(String message);
public IORuntimeException(String message, Throwable cause);
public IORuntimeException(Throwable cause);
}Install with Tessl CLI
npx tessl i tessl/maven-cn-hutool--hutool-core