How to Validate a TXT File with Java

Text files are generally considered to be simple, benign and extremely useful. They get used so often that character errors are bound to happen every now and then — that’s why it’s smart to validate Text files before storing them. Our TXT Validation API will take care of that for you in an instant, returning information about whether the input file was password protected or contained any errors. If the latter is found to be true, you’ll get a detailed description of the errors that were found along with an error path & some additional details. Let’s walk through how to take advantage of this API & structure an API call with ready-to-run code snippets in Java.

To begin, let’s install the Java SDK with Maven. Begin by adding a reference to the pom.xml repository:

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

Then let’s add one to the pom.xml dependency:

<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>

With installation finished, we can now use the below code block to structure our API call. First, add the imports at the top of your file, and then ensure you’re ready to satisfy the following parameters:

  1. Your Input File
  2. Your Cloudmersive API key (you can get one for free by registering a free account on our website, which will supply a limit of 800 API calls per month)
ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
ValidateDocumentApi apiInstance = new ValidateDocumentApi();
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
try {
DocumentValidationResult result = apiInstance.validateDocumentTxtValidation(inputFile);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling ValidateDocumentApi#validateDocumentTxtValidation");
e.printStackTrace();
}

After that, you’re all done. For your reference, I’ve included an example response model JSON:

{
"DocumentIsValid": true,
"PasswordProtected": true,
"ErrorCount": 0,
"WarningCount": 0,
"ErrorsAndWarnings": [
{
"Description": "string",
"Path": "string",
"Uri": "string",
"IsError": true
}
]
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store