Class OpenCsv
java.lang.Object
cloud.opencode.base.csv.OpenCsv
OpenCsv - Unified CSV Processing Facade
OpenCsv - 统一CSV处理门面
This is the primary entry point for all CSV operations in the OpenCode CSV library. Provides a comprehensive static API for parsing, writing, binding, streaming, diffing, and validating CSV data. All methods delegate to specialized internal components.
这是OpenCode CSV库中所有CSV操作的主要入口点。提供全面的静态API用于解析、写入、绑定、 流处理、差异比较和验证CSV数据。所有方法委托给专门的内部组件。
Features | 主要功能:
- RFC 4180 compliant parsing and writing - 符合RFC 4180的解析和写入
- Multiple input/output sources (String, Path, Stream, Reader/Writer) - 多种输入/输出源
- Object binding for Records and POJOs - Record和POJO的对象绑定
- Streaming read/write for large files - 大文件的流式读写
- CSV document diff/change detection - CSV文档差异/变更检测
- Validation and utility methods - 验证和工具方法
Usage Examples | 使用示例:
// Parse CSV string
CsvDocument doc = OpenCsv.parse("name,age\nAlice,30\nBob,25");
// Parse CSV file
CsvDocument doc = OpenCsv.parseFile(Path.of("data.csv"));
// Bind to objects
List<Person> people = OpenCsv.bind("name,age\nAlice,30", Person.class);
// Write CSV
String csv = OpenCsv.dump(doc);
OpenCsv.writeFile(doc, Path.of("output.csv"));
// Compare documents
List<CsvChange> changes = OpenCsv.diff(original, modified);
// Streaming
try (CsvReader reader = OpenCsv.reader(path)) {
reader.stream().forEach(row -> process(row));
}
Security | 安全性:
- Thread-safe: Yes (stateless facade) - 线程安全: 是(无状态门面)
- Null-safe: Yes (validates all inputs) - 空值安全: 是(验证所有输入)
- DoS protection: Configurable row/column/field limits - DoS保护: 可配置的行/列/字段限制
- Formula injection protection via CsvConfig - 通过CsvConfig的公式注入保护
- Since:
- JDK 25, opencode-base-csv V1.0.3
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> List<T> bind(CsvDocument doc, Class<T> type) Binds CSV document rows to objects 将CSV文档行绑定到对象static <T> List<T> Parses a CSV string and binds rows to objects using default configuration 使用默认配置解析CSV字符串并将行绑定到对象static <T> List<T> Parses a CSV string and binds rows to objects with the specified configuration 使用指定配置解析CSV字符串并将行绑定到对象static <T> List<T> Parses a CSV file and binds rows to objects using default configuration 使用默认配置解析CSV文件并将行绑定到对象static <T> List<T> Parses a CSV file and binds rows to objects with the specified configuration 使用指定配置解析CSV文件并将行绑定到对象static CsvDocument.Builderbuilder()Creates a new CSV document builder 创建新的CSV文档构建器static CsvDocumentconcat(CsvDocument... docs) Concatenates multiple CSV documents vertically (appends rows) 纵向连接多个CSV文档(追加行)static CsvConfig.Builderconfig()Creates a new CSV configuration builder 创建新的CSV配置构建器diff(CsvDocument original, CsvDocument modified) Computes differences between two CSV documents 计算两个CSV文档之间的差异diffByKey(CsvDocument original, CsvDocument modified, String keyColumn) Computes differences using a key column for row matching 使用键列进行行匹配来计算差异static Stringdump(CsvDocument doc) Formats a CSV document to a string using default configuration 使用默认配置将CSV文档格式化为字符串static Stringdump(CsvDocument doc, CsvConfig config) Formats a CSV document to a string with the specified configuration 使用指定配置将CSV文档格式化为字符串static <T> StringdumpObjects(Collection<T> objects, Class<T> type) Converts objects to a CSV string using default configuration 使用默认配置将对象转换为CSV字符串static <T> StringdumpObjects(Collection<T> objects, Class<T> type, CsvConfig config) Converts objects to a CSV string with the specified configuration 使用指定配置将对象转换为CSV字符串static <T> CsvDocumentfromObjects(Collection<T> objects, Class<T> type) Converts a collection of objects to a CSV document 将对象集合转换为CSV文档Extracts headers from a CSV string using default configuration 使用默认配置从CSV字符串提取标题Extracts headers from a CSV string with the specified configuration 使用指定配置从CSV字符串提取标题static CsvDocumentinnerJoin(CsvDocument left, CsvDocument right, String keyColumn) Joins two CSV documents using inner join on a key column 使用内连接在键列上连接两个CSV文档static booleanChecks if a string is valid CSV 检查字符串是否为有效的CSVstatic CsvDocumentleftJoin(CsvDocument left, CsvDocument right, String keyColumn) Joins two CSV documents using left join on a key column 使用左连接在键列上连接两个CSV文档static CsvDocumentparse(InputStream input) Parses CSV data from an input stream using default configuration 使用默认配置从输入流解析CSV数据static CsvDocumentparse(InputStream input, CsvConfig config) Parses CSV data from an input stream with the specified configuration 使用指定配置从输入流解析CSV数据static CsvDocumentParses CSV data from a reader using default configuration 使用默认配置从Reader解析CSV数据static CsvDocumentParses CSV data from a reader with the specified configuration 使用指定配置从Reader解析CSV数据static CsvDocumentParses a CSV string into a document using default configuration 使用默认配置将CSV字符串解析为文档static CsvDocumentParses a CSV string into a document with the specified configuration 使用指定配置将CSV字符串解析为文档static CsvDocumentParses a CSV file using default configuration 使用默认配置解析CSV文件static CsvDocumentParses a CSV file with the specified configuration 使用指定配置解析CSV文件static CsvDocumentParses a CSV file with the specified charset and default configuration 使用指定字符集和默认配置解析CSV文件static CsvQueryquery(CsvDocument doc) Creates a fluent query from a CSV document 从CSV文档创建流式查询static CsvReaderreader(InputStream input) Creates a streaming CSV reader from an input stream using default configuration 使用默认配置从输入流创建流式CSV读取器static CsvReaderreader(InputStream input, CsvConfig config) Creates a streaming CSV reader from an input stream with configuration 使用配置从输入流创建流式CSV读取器static CsvReaderCreates a streaming CSV reader from a reader using default configuration 使用默认配置从Reader创建流式CSV读取器static CsvReaderCreates a streaming CSV reader from a reader with configuration 使用配置从Reader创建流式CSV读取器static CsvReaderCreates a streaming CSV reader from a file using default configuration 使用默认配置从文件创建流式CSV读取器static CsvReaderCreates a streaming CSV reader from a file with configuration 使用配置从文件创建流式CSV读取器static intCounts the number of data rows in a CSV string (excluding header) 计算CSV字符串中的数据行数(不含标题行)static List<CsvDocument> split(CsvDocument doc, int maxRows) Splits a CSV document into chunks of the specified size 将CSV文档按指定大小分割为块static CsvColumnStatsstats(CsvDocument doc, String column) Computes summary statistics for a column 计算列的摘要统计信息static CsvTransformtransform(CsvDocument doc) Creates a fluent transformation pipeline from a CSV document 从CSV文档创建流式转换管道static CsvValidator.BuilderCreates a new CSV validator builder 创建新的CSV校验器构建器static voidwrite(CsvDocument doc, OutputStream output) Writes a CSV document to an output stream using default configuration 使用默认配置将CSV文档写入输出流static voidwrite(CsvDocument doc, OutputStream output, CsvConfig config) Writes a CSV document to an output stream with the specified configuration 使用指定配置将CSV文档写入输出流static voidwrite(CsvDocument doc, Writer writer) Writes a CSV document to a writer using default configuration 使用默认配置将CSV文档写入Writerstatic voidwrite(CsvDocument doc, Writer writer, CsvConfig config) Writes a CSV document to a writer with the specified configuration 使用指定配置将CSV文档写入Writerstatic voidwriteFile(CsvDocument doc, Path file) Writes a CSV document to a file using default configuration 使用默认配置将CSV文档写入文件static voidwriteFile(CsvDocument doc, Path file, CsvConfig config) Writes a CSV document to a file with the specified configuration 使用指定配置将CSV文档写入文件static CsvWriterwriter(OutputStream output) Creates a streaming CSV writer to an output stream using default configuration 使用默认配置创建到输出流的流式CSV写入器static CsvWriterwriter(OutputStream output, CsvConfig config) Creates a streaming CSV writer to an output stream with configuration 使用配置创建到输出流的流式CSV写入器static CsvWriterCreates a streaming CSV writer to a writer using default configuration 使用默认配置创建到Writer的流式CSV写入器static CsvWriterCreates a streaming CSV writer to a writer with configuration 使用配置创建到Writer的流式CSV写入器static CsvWriterCreates a streaming CSV writer to a file using default configuration 使用默认配置创建到文件的流式CSV写入器static CsvWriterCreates a streaming CSV writer to a file with configuration 使用配置创建到文件的流式CSV写入器
-
Method Details
-
parse
Parses a CSV string into a document using default configuration 使用默认配置将CSV字符串解析为文档- Parameters:
csv- the CSV string | CSV字符串- Returns:
- the parsed document | 解析后的文档
- Throws:
OpenCsvException- if parsing fails | 如果解析失败
-
parse
Parses a CSV string into a document with the specified configuration 使用指定配置将CSV字符串解析为文档- Parameters:
csv- the CSV string | CSV字符串config- the CSV configuration | CSV配置- Returns:
- the parsed document | 解析后的文档
- Throws:
OpenCsvException- if parsing fails | 如果解析失败
-
parseFile
Parses a CSV file using default configuration 使用默认配置解析CSV文件- Parameters:
file- the file path | 文件路径- Returns:
- the parsed document | 解析后的文档
- Throws:
OpenCsvException- if parsing or I/O fails | 如果解析或I/O失败
-
parseFile
Parses a CSV file with the specified configuration 使用指定配置解析CSV文件- Parameters:
file- the file path | 文件路径config- the CSV configuration | CSV配置- Returns:
- the parsed document | 解析后的文档
- Throws:
OpenCsvException- if parsing or I/O fails | 如果解析或I/O失败
-
parseFile
Parses a CSV file with the specified charset and default configuration 使用指定字符集和默认配置解析CSV文件- Parameters:
file- the file path | 文件路径charset- the character set | 字符集- Returns:
- the parsed document | 解析后的文档
- Throws:
OpenCsvException- if parsing or I/O fails | 如果解析或I/O失败
-
parse
Parses CSV data from an input stream using default configuration 使用默认配置从输入流解析CSV数据- Parameters:
input- the input stream | 输入流- Returns:
- the parsed document | 解析后的文档
- Throws:
OpenCsvException- if parsing fails | 如果解析失败
-
parse
Parses CSV data from an input stream with the specified configuration 使用指定配置从输入流解析CSV数据- Parameters:
input- the input stream | 输入流config- the CSV configuration | CSV配置- Returns:
- the parsed document | 解析后的文档
- Throws:
OpenCsvException- if parsing fails | 如果解析失败
-
parse
Parses CSV data from a reader using default configuration 使用默认配置从Reader解析CSV数据- Parameters:
reader- the reader | 读取器- Returns:
- the parsed document | 解析后的文档
- Throws:
OpenCsvException- if parsing fails | 如果解析失败
-
parse
Parses CSV data from a reader with the specified configuration 使用指定配置从Reader解析CSV数据- Parameters:
reader- the reader | 读取器config- the CSV configuration | CSV配置- Returns:
- the parsed document | 解析后的文档
- Throws:
OpenCsvException- if parsing fails | 如果解析失败
-
dump
Formats a CSV document to a string using default configuration 使用默认配置将CSV文档格式化为字符串- Parameters:
doc- the CSV document | CSV文档- Returns:
- the CSV string | CSV字符串
- Throws:
OpenCsvException- if formatting fails | 如果格式化失败
-
dump
Formats a CSV document to a string with the specified configuration 使用指定配置将CSV文档格式化为字符串- Parameters:
doc- the CSV document | CSV文档config- the CSV configuration | CSV配置- Returns:
- the CSV string | CSV字符串
- Throws:
OpenCsvException- if formatting fails | 如果格式化失败
-
writeFile
Writes a CSV document to a file using default configuration 使用默认配置将CSV文档写入文件- Parameters:
doc- the CSV document | CSV文档file- the file path | 文件路径- Throws:
OpenCsvException- if writing or I/O fails | 如果写入或I/O失败
-
writeFile
Writes a CSV document to a file with the specified configuration 使用指定配置将CSV文档写入文件- Parameters:
doc- the CSV document | CSV文档file- the file path | 文件路径config- the CSV configuration | CSV配置- Throws:
OpenCsvException- if writing or I/O fails | 如果写入或I/O失败
-
write
Writes a CSV document to an output stream using default configuration 使用默认配置将CSV文档写入输出流- Parameters:
doc- the CSV document | CSV文档output- the output stream | 输出流- Throws:
OpenCsvException- if writing fails | 如果写入失败
-
write
Writes a CSV document to an output stream with the specified configuration 使用指定配置将CSV文档写入输出流- Parameters:
doc- the CSV document | CSV文档output- the output stream | 输出流config- the CSV configuration | CSV配置- Throws:
OpenCsvException- if writing fails | 如果写入失败
-
write
Writes a CSV document to a writer using default configuration 使用默认配置将CSV文档写入Writer- Parameters:
doc- the CSV document | CSV文档writer- the writer | 写入器- Throws:
OpenCsvException- if writing fails | 如果写入失败
-
write
Writes a CSV document to a writer with the specified configuration 使用指定配置将CSV文档写入Writer- Parameters:
doc- the CSV document | CSV文档writer- the writer | 写入器config- the CSV configuration | CSV配置- Throws:
OpenCsvException- if writing fails | 如果写入失败
-
bind
Parses a CSV string and binds rows to objects using default configuration 使用默认配置解析CSV字符串并将行绑定到对象- Type Parameters:
T- the target type | 目标类型- Parameters:
csv- the CSV string | CSV字符串type- the target type | 目标类型- Returns:
- list of bound objects | 绑定的对象列表
- Throws:
OpenCsvException- if parsing or binding fails | 如果解析或绑定失败
-
bind
Parses a CSV string and binds rows to objects with the specified configuration 使用指定配置解析CSV字符串并将行绑定到对象- Type Parameters:
T- the target type | 目标类型- Parameters:
csv- the CSV string | CSV字符串type- the target type | 目标类型config- the CSV configuration | CSV配置- Returns:
- list of bound objects | 绑定的对象列表
- Throws:
OpenCsvException- if parsing or binding fails | 如果解析或绑定失败
-
bind
Binds CSV document rows to objects 将CSV文档行绑定到对象- Type Parameters:
T- the target type | 目标类型- Parameters:
doc- the CSV document | CSV文档type- the target type | 目标类型- Returns:
- list of bound objects | 绑定的对象列表
- Throws:
OpenCsvException- if binding fails | 如果绑定失败
-
bindFile
Parses a CSV file and binds rows to objects using default configuration 使用默认配置解析CSV文件并将行绑定到对象- Type Parameters:
T- the target type | 目标类型- Parameters:
file- the file path | 文件路径type- the target type | 目标类型- Returns:
- list of bound objects | 绑定的对象列表
- Throws:
OpenCsvException- if parsing, I/O, or binding fails | 如果解析、I/O或绑定失败
-
bindFile
Parses a CSV file and binds rows to objects with the specified configuration 使用指定配置解析CSV文件并将行绑定到对象- Type Parameters:
T- the target type | 目标类型- Parameters:
file- the file path | 文件路径type- the target type | 目标类型config- the CSV configuration | CSV配置- Returns:
- list of bound objects | 绑定的对象列表
- Throws:
OpenCsvException- if parsing, I/O, or binding fails | 如果解析、I/O或绑定失败
-
fromObjects
Converts a collection of objects to a CSV document 将对象集合转换为CSV文档- Type Parameters:
T- the object type | 对象类型- Parameters:
objects- the objects | 对象集合type- the object type | 对象类型- Returns:
- the CSV document | CSV文档
- Throws:
OpenCsvException- if conversion fails | 如果转换失败
-
dumpObjects
Converts objects to a CSV string using default configuration 使用默认配置将对象转换为CSV字符串- Type Parameters:
T- the object type | 对象类型- Parameters:
objects- the objects | 对象集合type- the object type | 对象类型- Returns:
- the CSV string | CSV字符串
- Throws:
OpenCsvException- if conversion fails | 如果转换失败
-
dumpObjects
Converts objects to a CSV string with the specified configuration 使用指定配置将对象转换为CSV字符串- Type Parameters:
T- the object type | 对象类型- Parameters:
objects- the objects | 对象集合type- the object type | 对象类型config- the CSV configuration | CSV配置- Returns:
- the CSV string | CSV字符串
- Throws:
OpenCsvException- if conversion fails | 如果转换失败
-
reader
Creates a streaming CSV reader from an input stream using default configuration 使用默认配置从输入流创建流式CSV读取器- Parameters:
input- the input stream | 输入流- Returns:
- the CSV reader | CSV读取器
-
reader
Creates a streaming CSV reader from an input stream with configuration 使用配置从输入流创建流式CSV读取器- Parameters:
input- the input stream | 输入流config- the CSV configuration | CSV配置- Returns:
- the CSV reader | CSV读取器
-
reader
-
reader
-
reader
Creates a streaming CSV reader from a file using default configuration 使用默认配置从文件创建流式CSV读取器- Parameters:
file- the file path | 文件路径- Returns:
- the CSV reader | CSV读取器
- Throws:
OpenCsvException- if the file cannot be opened | 如果无法打开文件
-
reader
Creates a streaming CSV reader from a file with configuration 使用配置从文件创建流式CSV读取器- Parameters:
file- the file path | 文件路径config- the CSV configuration | CSV配置- Returns:
- the CSV reader | CSV读取器
- Throws:
OpenCsvException- if the file cannot be opened | 如果无法打开文件
-
writer
Creates a streaming CSV writer to an output stream using default configuration 使用默认配置创建到输出流的流式CSV写入器- Parameters:
output- the output stream | 输出流- Returns:
- the CSV writer | CSV写入器
-
writer
Creates a streaming CSV writer to an output stream with configuration 使用配置创建到输出流的流式CSV写入器- Parameters:
output- the output stream | 输出流config- the CSV configuration | CSV配置- Returns:
- the CSV writer | CSV写入器
-
writer
-
writer
-
writer
Creates a streaming CSV writer to a file using default configuration 使用默认配置创建到文件的流式CSV写入器- Parameters:
file- the file path | 文件路径- Returns:
- the CSV writer | CSV写入器
- Throws:
OpenCsvException- if the file cannot be opened | 如果无法打开文件
-
writer
Creates a streaming CSV writer to a file with configuration 使用配置创建到文件的流式CSV写入器- Parameters:
file- the file path | 文件路径config- the CSV configuration | CSV配置- Returns:
- the CSV writer | CSV写入器
- Throws:
OpenCsvException- if the file cannot be opened | 如果无法打开文件
-
isValid
Checks if a string is valid CSV 检查字符串是否为有效的CSV- Parameters:
csv- the CSV string | CSV字符串- Returns:
- true if valid | 如果有效返回true
-
rowCount
Counts the number of data rows in a CSV string (excluding header) 计算CSV字符串中的数据行数(不含标题行)- Parameters:
csv- the CSV string | CSV字符串- Returns:
- the row count | 行数
- Throws:
OpenCsvException- if parsing fails | 如果解析失败
-
headers
Extracts headers from a CSV string using default configuration 使用默认配置从CSV字符串提取标题- Parameters:
csv- the CSV string | CSV字符串- Returns:
- the header list | 标题列表
- Throws:
OpenCsvException- if parsing fails | 如果解析失败
-
headers
Extracts headers from a CSV string with the specified configuration 使用指定配置从CSV字符串提取标题- Parameters:
csv- the CSV string | CSV字符串config- the CSV configuration | CSV配置- Returns:
- the header list | 标题列表
- Throws:
OpenCsvException- if parsing fails | 如果解析失败
-
diff
Computes differences between two CSV documents 计算两个CSV文档之间的差异- Parameters:
original- the original document | 原始文档modified- the modified document | 修改后的文档- Returns:
- list of changes | 变更列表
-
diffByKey
public static List<CsvChange> diffByKey(CsvDocument original, CsvDocument modified, String keyColumn) Computes differences using a key column for row matching 使用键列进行行匹配来计算差异- Parameters:
original- the original document | 原始文档modified- the modified document | 修改后的文档keyColumn- the key column header name | 键列标题名称- Returns:
- list of changes | 变更列表
-
query
Creates a fluent query from a CSV document 从CSV文档创建流式查询- Parameters:
doc- the CSV document | CSV文档- Returns:
- a new CsvQuery builder | 新的CsvQuery构建器
-
transform
Creates a fluent transformation pipeline from a CSV document 从CSV文档创建流式转换管道- Parameters:
doc- the CSV document | CSV文档- Returns:
- a new CsvTransform builder | 新的CsvTransform构建器
-
concat
Concatenates multiple CSV documents vertically (appends rows) 纵向连接多个CSV文档(追加行)- Parameters:
docs- the documents to concatenate | 要连接的文档- Returns:
- the merged document | 合并后的文档
-
innerJoin
Joins two CSV documents using inner join on a key column 使用内连接在键列上连接两个CSV文档- Parameters:
left- the left document | 左文档right- the right document | 右文档keyColumn- the key column name | 键列名- Returns:
- the joined document | 连接后的文档
-
leftJoin
Joins two CSV documents using left join on a key column 使用左连接在键列上连接两个CSV文档- Parameters:
left- the left document | 左文档right- the right document | 右文档keyColumn- the key column name | 键列名- Returns:
- the joined document | 连接后的文档
-
stats
Computes summary statistics for a column 计算列的摘要统计信息- Parameters:
doc- the CSV document | CSV文档column- the column name | 列名- Returns:
- the column statistics | 列统计信息
-
split
Splits a CSV document into chunks of the specified size 将CSV文档按指定大小分割为块- Parameters:
doc- the CSV document | CSV文档maxRows- the maximum rows per chunk | 每块最大行数- Returns:
- list of document chunks | 文档块列表
-
validator
Creates a new CSV validator builder 创建新的CSV校验器构建器- Returns:
- a new validator builder | 新的校验器构建器
-
builder
Creates a new CSV document builder 创建新的CSV文档构建器- Returns:
- a new document builder | 新的文档构建器
-
config
Creates a new CSV configuration builder 创建新的CSV配置构建器- Returns:
- a new config builder | 新的配置构建器
-