Code

Appending to a file – Java

For more control over the file append process, I am using an OutputStream as follows (Note, if using a single file then the filePath parameter is not required):


public void appendToFile(String filePath, String contentToAppend) {
boolean yesAppendToFile = true;
OutputStream outputStream = null;
File file = new File(filePath);
try {
if (file.exists()) {
outputStream = new FileOutputStream(filePath, yesAppendToFile);
outputStream.write(contentToAppend.getBytes());
} else {
throw new RuntimeException(String.format("File: %s does not exist, can't append.", file.getAbsolutePath()));
}
} catch (IOException e) {
LOG.error(e.getMessage(), e);
}
}

The Process is:

  • Get object instance of type Event
  • Convert object instance to JSON
  • Invoke append method with the file path and json content
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s