Java NetworkAddressUtil-class And Method Code Example


Here is an example of how the NetworkAddressUtil class from the logback library can be used:

import ch.qos.logback.core.util.NetworkAddressUtil;

public class MyClass {
    public static void main(String[] args) {
        String localHostName = NetworkAddressUtil.getLocalHostName();
        System.out.println("Local host name: " + localHostName);
        InetAddress localHostAddress = NetworkAddressUtil.getLocalHostAddress();
        System.out.println("Local host address: " + localHostAddress);
    }
}

In this example, MyClass uses an instance of NetworkAddressUtil to get the host name and address of the local host. The getLocalHostName method returns the host name of the local host and the getLocalHostAddress method returns the InetAddress of the local host.

You can also use getLocalHostIPAddresses to get all the IP addresses of the local host.

List<InetAddress> localHostAddresses = NetworkAddressUtil.getLocalHostIPAddresses();

You can also use isIPv4 method to check if the IP address is IPv4 or not.

boolean isIPv4 = NetworkAddressUtil.isIPv4("192.168.1.1");

This will return true as the IP address is IPv4.

Please note that NetworkAddressUtil is a utility class and it does not have any constructor.