Class FileComparator
java.lang.Object
cloud.opencode.base.io.file.FileComparator
File Content Comparator - Compares files by content
文件内容比较器 - 按内容比较文件
Provides various methods for comparing files by content, including byte-by-byte comparison, hash-based comparison, and line-by-line comparison.
提供多种按内容比较文件的方法,包括逐字节比较、基于哈希的比较和逐行比较。
Features | 主要功能:
- Byte-by-byte content comparison - 逐字节内容比较
- Hash-based comparison for large files - 大文件的基于哈希比较
- Line-by-line text comparison - 逐行文本比较
- Diff generation - 差异生成
- Directory comparison - 目录比较
Usage Examples | 使用示例:
// Simple content comparison
boolean same = FileComparator.contentEquals(path1, path2);
// Compare using hash (faster for large files)
boolean same = FileComparator.hashEquals(path1, path2, "SHA-256");
// Compare directories
DirectoryDiff diff = FileComparator.compareDirectories(dir1, dir2);
// Get line differences
LineDiff diff = FileComparator.diffLines(file1, file2);
Security | 安全性:
- Thread-safe: Yes (stateless utility) - 线程安全: 是(无状态工具类)
- Null-safe: No, paths must not be null - 空值安全: 否,路径不可为null
- Since:
- JDK 25, opencode-base-io V1.2.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordResult of comparing two directories.static final recordA line change between two files.static final recordResult of comparing lines between two files.static final recordA line entry with line number. -
Method Summary
Modifier and TypeMethodDescriptionstatic FileComparator.DirectoryDiffcompareDirectories(Path dir1, Path dir2) Compares two directories.static FileComparator.DirectoryDiffcompareDirectories(Path dir1, Path dir2, Predicate<Path> filter) Compares two directories with a filter.static byte[]computeHash(Path file, String algorithm) Computes the hash of a file.static StringcomputeHashHex(Path file, String algorithm) Computes the hash of a file as hexadecimal string.static booleancontentEquals(Path file1, Path file2) Compares two files byte-by-byte.static FileComparator.LineDiffGenerates a diff between two text files using UTF-8.static FileComparator.LineDiffGenerates a diff between two text files.static booleanhashEquals(Path file1, Path file2, String algorithm) Compares two files using hash.static booleanlinesEqual(Path file1, Path file2) Compares two text files line by line using UTF-8.static booleanlinesEqual(Path file1, Path file2, Charset charset) Compares two text files line by line.
-
Method Details
-
contentEquals
Compares two files byte-by-byte. 逐字节比较两个文件。- Parameters:
file1- the first file | 第一个文件file2- the second file | 第二个文件- Returns:
- true if files have identical content | 如果文件内容相同返回true
- Throws:
OpenIOOperationException- if comparison fails | 如果比较失败
-
hashEquals
Compares two files using hash. 使用哈希比较两个文件。- Parameters:
file1- the first file | 第一个文件file2- the second file | 第二个文件algorithm- the hash algorithm (e.g., "SHA-256", "MD5") | 哈希算法- Returns:
- true if files have the same hash | 如果文件哈希相同返回true
- Throws:
OpenIOOperationException- if comparison fails | 如果比较失败
-
computeHash
Computes the hash of a file. 计算文件的哈希值。- Parameters:
file- the file | 文件algorithm- the hash algorithm | 哈希算法- Returns:
- the hash bytes | 哈希字节
- Throws:
OpenIOOperationException- if hashing fails | 如果哈希失败
-
computeHashHex
Computes the hash of a file as hexadecimal string. 计算文件的哈希值并返回十六进制字符串。- Parameters:
file- the file | 文件algorithm- the hash algorithm | 哈希算法- Returns:
- the hash as hex string | 十六进制哈希字符串
- Throws:
OpenIOOperationException- if hashing fails | 如果哈希失败
-
linesEqual
Compares two text files line by line. 逐行比较两个文本文件。- Parameters:
file1- the first file | 第一个文件file2- the second file | 第二个文件charset- the charset | 字符集- Returns:
- true if files have identical lines | 如果文件行相同返回true
- Throws:
OpenIOOperationException- if comparison fails | 如果比较失败
-
linesEqual
Compares two text files line by line using UTF-8. 使用UTF-8逐行比较两个文本文件。- Parameters:
file1- the first file | 第一个文件file2- the second file | 第二个文件- Returns:
- true if files have identical lines | 如果文件行相同返回true
- Throws:
OpenIOOperationException- if comparison fails | 如果比较失败
-
diffLines
Generates a diff between two text files. 生成两个文本文件之间的差异。- Parameters:
file1- the first file | 第一个文件file2- the second file | 第二个文件charset- the charset | 字符集- Returns:
- the line diff | 行差异
- Throws:
OpenIOOperationException- if diff fails | 如果差异生成失败
-
diffLines
Generates a diff between two text files using UTF-8. 使用UTF-8生成两个文本文件之间的差异。- Parameters:
file1- the first file | 第一个文件file2- the second file | 第二个文件- Returns:
- the line diff | 行差异
- Throws:
OpenIOOperationException- if diff fails | 如果差异生成失败
-
compareDirectories
Compares two directories. 比较两个目录。- Parameters:
dir1- the first directory | 第一个目录dir2- the second directory | 第二个目录- Returns:
- the directory diff | 目录差异
- Throws:
OpenIOOperationException- if comparison fails | 如果比较失败
-
compareDirectories
public static FileComparator.DirectoryDiff compareDirectories(Path dir1, Path dir2, Predicate<Path> filter) Compares two directories with a filter. 使用过滤器比较两个目录。- Parameters:
dir1- the first directory | 第一个目录dir2- the second directory | 第二个目录filter- the file filter | 文件过滤器- Returns:
- the directory diff | 目录差异
- Throws:
OpenIOOperationException- if comparison fails | 如果比较失败
-