Java: How To Use RandomAccessFile and FileChannel to Write to a Specific Location in a File

If there is ever a need to write bytes to a specific location to an existing file, here is an example of how to use the RandomAccessFile and FileChannel Java classes to do so:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * Will write bytes to the beginning of an existing file.
 *
 * @author Ryan Chapin
 */
public class RandomAccessFileTest {

    public static void main(String[] args) {

        // Generate input string and the → Continue reading “Java: How To Use RandomAccessFile and FileChannel to Write to a Specific Location in a File”

Using a ProcessBuilder to Execute an OS Level Command and Properly Read the Exit Code from a Java Process

There are many a situation where a developer will want to execute an OS level command in another process and read not only the standard out (stdout), and standard error (sterr), but also the exit code returned from the process.

To do so, utilize the ProcessBuilder class along with a helper class (ProcessWrapper) that will provide the ability to set a timeout for the process and read the exit code in a separate thread.

Following is an example and the → Continue reading “Using a ProcessBuilder to Execute an OS Level Command and Properly Read the Exit Code from a Java Process”