How do I decode a Base64 encoded binary?

package org.kodejava.commons.codec;

import org.apache.commons.codec.binary.Base64;

import java.util.Arrays;

public class Base64Decode {
    public static void main(String[] args) {
        String hello = "SGVsbG8gV29ybGQ=";

        // Decode a previously encoded string using decodeBase64 method and
        // passing the byte[] of the encoded string.
        byte[] decoded = Base64.decodeBase64(hello.getBytes());

        // Print the decoded array
        System.out.println(Arrays.toString(decoded));

        // Convert the decoded byte[] back to the original string and print
        // the result.
        String decodedString = new String(decoded);
        System.out.println(hello + " = " + decodedString);
    }
}

The result of our code is:

[72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100]
SGVsbG8gV29ybGQ= = Hello World

Maven Dependencies

<dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>1.16.0</version>
</dependency>

Maven Central

How do I encode binary data as Base64 string?

package org.kodejava.commons.codec;

import org.apache.commons.codec.binary.Base64;

import java.util.Arrays;

public class Base64Encode {
    public static void main(String[] args) {
        String hello = "Hello World";

        // The encodeBase64 method take a byte[] as the parameter. The byte[]
        // can be from a simple string like in this example, or it can be from
        // an image file data.
        byte[] encoded = Base64.encodeBase64(hello.getBytes());

        // Print the encoded byte array
        System.out.println(Arrays.toString(encoded));

        // Print the encoded string
        String encodedString = new String(encoded);
        System.out.println(hello + " = " + encodedString);
    }
}

The result of our program:

[83, 71, 86, 115, 98, 71, 56, 103, 86, 50, 57, 121, 98, 71, 81, 61]
Hello World = SGVsbG8gV29ybGQ=

Maven Dependencies

<dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>1.16.0</version>
</dependency>

Maven Central

How do I search for files recursively using Apache Commons IO?

This example demonstrates how we can use the FileUtils class listFiles() method to search for a file specified by their extensions. We can also define to find the file recursively deep down into the subdirectories.

package org.kodejava.commons.io;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.util.Collection;

public class SearchFileRecursive {
    public static void main(String[] args) {
        File root = new File("F:/Wayan/Kodejava/kodejava-example");

        try {
            String[] extensions = {"xml", "java", "dat"};

            // Find files within a root directory and optionally its
            // subdirectories, which match an array of extensions. When the
            // extensions are null, all files will be returned.
            //
            // This method will return matched file as java.io.File
            Collection<File> files = FileUtils.listFiles(root, extensions, true);

            for (File file : files) {
                System.out.println("File = " + file.getAbsolutePath());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Maven Dependencies

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.14.0</version>
</dependency>

Maven Central

How do I connect to FTP server?

The File Transfer Protocol (FTP) is a standard network protocol used to transfer computer files between a client and server on a computer network. The example below shows you how to connect to an FTP server.

In this example we are using the FTPClient class of the Apache Commons Net library. To connect to the server, we need to provide the FTP server name. Login to the server can be done by calling the login() method of this class with a valid username and password. To logout we call the logout() method.

Let’s try the code snippet below:

package org.kodejava.commons.net;

import org.apache.commons.net.ftp.FTPClient;

import java.io.IOException;

public class FTPConnectDemo {
    public static void main(String[] args) {
        FTPClient client = new FTPClient();

        try {
            client.connect("ftp.example.com");

            // When login success, the login method returns true.
            boolean login = client.login("demo", "password");
            if (login) {
                System.out.println("Login success...");

                // When logout success, the logout method returns true.
                boolean logout = client.logout();
                if (logout) {
                    System.out.println("Logout from FTP server...");
                }
            } else {
                System.out.println("Login fail...");
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                // Closes the connection to the FTP server
                client.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Maven Dependencies

<dependency>
    <groupId>commons-net</groupId>
    <artifactId>commons-net</artifactId>
    <version>3.10.0</version>
</dependency>

Maven Central

How do I touch a file using Apache Commons IO?

This example demonstrates a touch command just like Unix touch command. If the file to be touched doesn’t exist, a new empty file will be created.

package org.kodejava.commons.io;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;

public class TouchFileExample {
    public static void main(String[] args) {
        try {
            File file = new File("Touch.dat");

            // Touch the file. When the file does not exist, a new file will be
            // created. If the file exists, change the file timestamp.
            FileUtils.touch(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Maven Dependencies

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.14.0</version>
</dependency>

Maven Central