Writing an InputStream (Java Doc) to a RandomAccessFile (Java Doc) is quite simple as shown in the following example. This example also makes use of FileLock (Java Doc) to make sure that no one modifies the file while the data is being written to file.

package com.javacreed.examples.io;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.ReadableByteChannel;

public class Example {
  public static void main(final String[] args) throws IOException {
    final File outputFile = new File("output.data");
    try (RandomAccessFile raf = new RandomAccessFile(outputFile, "rw"); FileChannel fc = raf.getChannel();) {
      final FileLock fl = fc.tryLock();
      if (fl == null) {
        // Failed to acquire lock
      } else {
        try (final ReadableByteChannel in = Channels.newChannel(new BufferedInputStream(Example.class
            .getResourceAsStream("/remote.data")))) {
          for (final ByteBuffer buffer = ByteBuffer.allocate(1024); in.read(buffer) != -1;) {
        } finally {

This example also makes use of ByteBuffer (Java Doc) to store the data read from the InputStream and then written to the file.

Albert Attard
Author: 1 day ago

This tutorial demonstrates how to develop a simple Java Swing (or user interface) application and test the UI related logic using automated tests, such as JUnit (Homepage), using a series of videos compiled as a playlist (Youtube). The application is kept very simple while making use of various technologies, such as EasyMock (Homepage). This tutorial is targeted to college or university students who studied Java but lack experience and would like to do something productive. The main idea of this tutorial is not to learn Java, but to help the viewers to build testable Swing applications. The playlist presented by this tutorial shows how to build a Java Swing application that can be easily tested using automated tests in a step-by-step fashion.

Albert Attard
Author: 3 months ago

This tutorial provides a step-by-step guide how to setup the development environment on a Windows 7 (Homepage) operating system. Please note that while you can follow this tutorial even if you are using a different operating system, some of the steps shown here may not be the same for all operating systems. For example, a Linux operating system (Homepage) does not install programs under the “Programs Files” folder like a Windows operating system does. On the other hand, the configuration of the SpringSource Tool Suite is the same irrespective of the operating system. Furthermore, irrespective of the operating system, the order of how things are done is the same for all operating systems.

Albert Attard
Author: 4 months ago

Programs work well when dealing with a considerable size of data, but their performance tend to degradate when dealing with large volumes of data. Resources are cheaper than ever, but this does not mean that we can have inefficient programs. Dealing with large amounts of data brings several challenges, even if the task is simple parsing some Java objects into JSON strings, as we will see in this article. Inefficient use of memory can make a program wasting more than 5 times the memory when compared with a more efficient one. This means that one program makes use of 1G of memory, while the inefficient one requires 5G of memory to perform the same task.

Albert Attard
Author: 6 months ago

This tutorial demonstrates how to develop a Java application that connects with a database and provides a user interface and is targeted to students who studied Java, but lack experience. This tutorial assumes basic knowledge of Java and it describes everything in great detail. The main idea of this tutorial is not learn Java, but to help the viewers to gain experience in developing Java applications. During the tutorial we explore various technologies, such as Maven (Homepage). These technologies are quite popular amongst developers communities and are considered as good traits when applying for jobs.

Albert Attard
Author: 6 months ago

Java objects can be serialised to JSON strings and deserialised back using JsonSerializer (Article and Java Doc) and the JsonDeserializer (Article and Java Doc) respectively. These two classes simplify the translation between these two realms but add an extra layer which can be avoided. Instead of the JsonSerialiser or JsonDeserialiser we can use an instance of TypeAdapter (Java Doc) which can serialise and deserialise JSON objects efficiently as we will see in this article.

Albert Attard
Author: 6 months ago

Java web application require a web container, such as Tomcat (homepage), on which these can run. Installing and configure a web container on each developing machine may be time consuming. Furthermore, other developers need to manage the dependencies manually if they want to run the web application.

Albert Attard
Author: 7 months ago