A very simple Java library for reading and writing data in different formats. Currently, the available formats are:
- CSV (Comma Separated Values).
You can pull it from the central Maven repositories:
<dependency>
<groupId>com.dattack</groupId>
<artifactId>data-formats</artifactId>
<version>0.2.1</version>
</dependency>
The source code on the master branch is the current state of development; it is not recommended for general use. If you prefer to build from source, please use an appropriate release tag.
- Create a custom configuration by setting the properties of the CSV to be generated.
- Instantiate the
CSVStringBuilder
class. - Append content to the instance of class
CSVStringBuilder
created in the previous step.
import com.dattack.formats.csv.CSVConfiguration;
import com.dattack.formats.csv.CSVStringBuilder;
public class Example {
// 1: create your customized configuration
private CSVConfiguration getConfiguration() {
return CSVConfiguration
.custom()
// .withCommentChar('#')
// .withSeparator("\t")
// ...
.build();
}
// 2: instantiate the CSVStringBuilder class
private CSVStringBuilder getCsvBuilder() {
return new CSVStringBuilder(getConfiguration());
}
public void writeCsv() {
// 3: append content
String csv = getCsvBuilder()
.comment("comment line").eol() //
.append(1000L)
.append(true)
.append(1000.5)
.append(false)
.append("simple text")
.append("text with #special# chars")
.append(java.sql.Timestamp.from(instant))
.append(10.2)
.append(new BigDecimal(BigInteger.valueOf(105), 1))
.append(1)
.eol()
.toString();
}
}
- Create a custom configuration by setting the properties of the CSV to be generated.
- Instantiate the
CSVReader
class. - Iterate over the content.
import com.dattack.formats.csv.CSVConfiguration;
import com.dattack.formats.csv.CSVObject;
import com.dattack.formats.csv.CSVReader;
import java.util.Objects;
public class Example {
// 1: create your customized configuration
private CSVConfiguration getConfiguration() {
return CSVConfiguration
.custom()
// .withCommentChar('#')
// .withSeparator("\t")
// ...
.build();
}
// 2: instantiate the CSVReader class
private CSVReader getCsvReader(final File file) {
return new CSVReader(getConfiguration(), file);
}
public static void readCsv(final File file) throws IOException {
CSVReader csvReader = getCsvReader(file);
// 3: iterate over the content
CSVObject csvObject;
while (Objects.nonNull(csvObject = csvReader.next())) {
for (int i = 0; i < csvObject.getSize(); i++) {
String value = csvObject.get(i);
// ...
}
}
}
}
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request :D
If you have other questions, please contact by email or @dattackteam
Code is under the Apache Licence v2.