https://github.com/dattack/data-formats
A very simple Java library for reading and writing data in different formats
https://github.com/dattack/data-formats
java-library
Last synced: 6 months ago
JSON representation
A very simple Java library for reading and writing data in different formats
- Host: GitHub
- URL: https://github.com/dattack/data-formats
- Owner: dattack
- License: apache-2.0
- Created: 2016-10-16T17:22:01.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2023-03-12T17:05:30.000Z (over 3 years ago)
- Last Synced: 2025-07-14T15:54:46.250Z (12 months ago)
- Topics: java-library
- Language: Java
- Size: 95.7 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# data-formats
[](https://travis-ci.com/dattack/data-formats/builds)
[](LICENSE.md)
[](https://search.maven.org/artifact/com.dattack/data-formats)
[](https://javadoc.io/doc/com.dattack/data-formats)
A very simple Java library for reading and writing data in different formats.
Currently, the available formats are:
* CSV (Comma Separated Values).
## Where can I get the latest release?
You can pull it from the central Maven repositories:
```xml
com.dattack
data-formats
0.2.1
```
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.
## How-To
### Writing CSV data
1. Create a custom configuration by setting the properties of the CSV to be generated.
2. Instantiate the `CSVStringBuilder` class.
3. Append content to the instance of class `CSVStringBuilder` created in the previous step.
```java
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();
}
}
```
### Reading a CSV file
1. Create a custom configuration by setting the properties of the CSV to be generated.
2. Instantiate the `CSVReader` class.
3. Iterate over the content.
```java
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);
// ...
}
}
}
}
```
## Contributing
Pull requests and stars are always welcome. For bugs and feature
requests, [please create an issue](https://github.com/dattack/data-formats/issues).
1. Fork it!
2. Create your feature branch: `git checkout -b my-new-feature`
3. Commit your changes: `git commit -am 'Add some feature'`
4. Push to the branch: `git push origin my-new-feature`
5. Submit a pull request :D
If you have other questions, please contact by [email](mailto:dev@dattack.com) or
[@dattackteam](https://twitter.com/dattackteam)
## License
Code is under the [Apache Licence v2](https://www.apache.org/licenses/LICENSE-2.0.txt).