File is a Java library with a PhatFile wrapper providing capability to write, parse and validate files in the following formats:
- JSON (org.json.JSONObject and com.fasterxml.jackson.databind.JsonNode supported)
- XML (org.w3c.dom.Document supported)
- Free Format Text Fies
- CSV file (comming soon)
File validations
try{
PhatFile phatFileObject = new PhatFile("/absolute/path/to/some/file");
Validators validators = new Validators();
//Get the content of the file as a string
String fileContent = phatFileObject.getContentAsString();
//Get the file format of the file based on the content
FileFormats fileFormat = validators.determineFileType(phatFileObject);
//Validate if the content within a file is valid for a specific file type
boolean isValidJsonContent = validators.validate(fileContent, FileFormats.JSON);
boolean isValidXmlContent = validators.validate(fileContent, FileFormats.XML);
boolean isValidCsvContent = validators.validate(fileContent, FileFormats.CSV);
} catch (IOException e) {
//Handle IOException
} catch (ParserConfigurationException e) {
//Handle ParserConfigurationException
} catch (SAXException e) {
//Handle ParserConfigurationException
}
Reading content from different file types
try{
PhatFile phatFileObject = new PhatFile("/absolute/path/to/some/file");
//Retrieve the content as a JsonNode
JsonNode jsonNode = phatFileObject.getAsJsonNode();
//Retrieve the content as a JSONObject
JSONObject jsonObject = phatFileObject.getAsJsonObject();
//Retrieve the content as an XML Document
Document document = phatFileObject.getAsDocument();
/*
* Manipulate the objects as you wish...
*/
} catch (IOException e) {
//Handle IOException
} catch (ParserConfigurationException e) {
//Handle ParserConfigurationException
}
Writing content to different file types
try{
Writer writer = new Writer();
FileFormats outputFileFormat = FileFormats.JSON;
String contentToWrite = "Some content in some format you want to write...";
String outputFilePath = "/path/to/the/where/you/would/like/to/write";
PhatFile writtenFile = writer.writeToFile(outputFileFormat, contentToWrite, outputFilePath);
/*
* Manipulate the writtenFile as you wish...
*/
} catch (IOException e) {
//Handle IOException
} catch (ParserConfigurationException e) {
//Handle ParserConfigurationException
}
The library is available on the Central Maven Repository here.
Please see the javadocs for info.
1.0.0
Thank you for considering contributing to this library! Please see the contribution documentation for guidance on contributing.
If you discover any bugs or issues with the library, please log an issue or send an email to either Madi or Chris, and we will check it out.
Apache 2.0