Get Network Prefix Length in Java
Description
The following code shows how to get Network Prefix Length.
Example
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
// w w w. j a v a 2s . c o m
public class Main {
public static void main(String[] args) throws Exception {
Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
while (en.hasMoreElements()) {
NetworkInterface ni = en.nextElement();
System.out.println(" Display Name = " + ni.getDisplayName());
List<InterfaceAddress> list = ni.getInterfaceAddresses();
Iterator<InterfaceAddress> it = list.iterator();
while (it.hasNext()) {
InterfaceAddress ia = it.next();
System.out.println(" Network prefix length = " + ia.getNetworkPrefixLength());
}
}
}
}
The code above generates the following result.