7天掌握NIO和SOCKET,第五天,InterfaceAddress类的使用:

import java.net.*;
import java.util.Enumeration;
import java.util.List;

public class test_03 {
    public static void main(String[] args) throws Exception {
        //InterfaceAddress类的使用:
        //对应的是网络接
        //1:getAddress()得到对应的InetAddress对象,getBroadcast(),ipv4得到广播地址的InetAddress对象
        //method_01();

        //点对点设备
        //method_02();

        //是否支持多播
        //method_03();

        //NetworkInterface除getNerworkInterfaces()方法外的三个静态方法
        //根据索引获取NetworkInterface对象:getByIndex()
        method_04();
    }

    private static void method_01() throws SocketException {
        Enumeration<NetworkInterface> networkInterfaceEnumeration = NetworkInterface.getNetworkInterfaces();
        while (networkInterfaceEnumeration.hasMoreElements()){
            NetworkInterface networkInterface = networkInterfaceEnumeration.nextElement();
            System.out.println("网络设备名称:"+networkInterface.getName());
            System.out.println("网络设备显示名称:"+networkInterface.getDisplayName());
            List<InterfaceAddress> interfaceAddresses = networkInterface.getInterfaceAddresses();
            for (int i = 0; i < interfaceAddresses.size(); i++) {
                InterfaceAddress interfaceAddress = interfaceAddresses.get(i);
                InetAddress inetAddress = interfaceAddress.getAddress();
                if(inetAddress!=null)
                    System.out.println("\tinterfaceAddress.getAddress():"+inetAddress.getHostAddress());
                inetAddress = interfaceAddress.getBroadcast();
                if(inetAddress!=null)
                    System.out.println("\tinterfaceAddress.getBroadcast():"+inetAddress.getHostAddress());
                System.out.println("\tinterfaceAddress.getNetworkPrefixLength():"+interfaceAddress.getNetworkPrefixLength());
            }
        }
    }

    private static void method_02() throws SocketException {
        Enumeration<NetworkInterface> networkInterfaceEnumeration = NetworkInterface.getNetworkInterfaces();
        while (networkInterfaceEnumeration.hasMoreElements()){
            NetworkInterface networkInterface = networkInterfaceEnumeration.nextElement();
            if (networkInterface.isPointToPoint()){
                System.out.println("网络设备名称:"+networkInterface.getName());
                System.out.println("网络设备显示名称:"+networkInterface.getDisplayName());
                System.out.println("是否为点对点设备:"+networkInterface.isPointToPoint());
            }
        }
    }

    private static void method_03() throws SocketException{
        Enumeration<NetworkInterface> networkInterfaceEnumeration = NetworkInterface.getNetworkInterfaces();
        while (networkInterfaceEnumeration.hasMoreElements()){
            NetworkInterface networkInterface = networkInterfaceEnumeration.nextElement();
            System.out.println("网络设备名称:"+networkInterface.getName());
            System.out.println("网络设备显示名称:"+networkInterface.getDisplayName());
            System.out.println("网络设备是否支持多播:"+networkInterface.supportsMulticast());
            System.out.println("\n");
        }
    }

    private static void method_04() throws SocketException, UnknownHostException {
        Enumeration<NetworkInterface> networkInterfaceEnumeration = NetworkInterface.getNetworkInterfaces();
        while (networkInterfaceEnumeration.hasMoreElements()){
            NetworkInterface networkInterface = networkInterfaceEnumeration.nextElement();
            System.out.println(networkInterface.getName());
            System.out.println(networkInterface.getDisplayName());
            System.out.println(networkInterface.getIndex());
            System.out.println("\n");
        }
        //上面代码可知1为lo
        System.out.println("----->"+NetworkInterface.getByIndex(1));
        System.out.println("----->"+NetworkInterface.getByName("lo"));
        System.out.println("----->"+NetworkInterface.getByInetAddress(InetAddress.getByName("localhost")));
        /*最后的输出:
        ----->name:lo (Software Loopback Interface 1)
        ----->name:lo (Software Loopback Interface 1)
        ----->name:lo (Software Loopback Interface 1)
        *  */
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Java中的FileChannel类和ByteBuffer类来实现NIO操作,可以提高文件读取效率。以下是一个使用NIO读取文件的示例代码: ```java import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; public class NIOFileReader { public static void main(String[] args) throws IOException { // 打开文件通道 FileChannel fileChannel = new FileInputStream(new File("file.txt")).getChannel(); // 创建一个缓冲区 ByteBuffer buffer = ByteBuffer.allocate(1024); // 从文件通道读取数据到缓冲区 while (fileChannel.read(buffer) != -1) { // 切换读写模式 buffer.flip(); // 读取缓冲区中的数据 while (buffer.hasRemaining()) { System.out.print((char) buffer.get()); } // 清空缓冲区 buffer.clear(); } // 关闭文件通道 fileChannel.close(); } } ``` 在这个示例中,我们首先打开文件通道,然后创建一个ByteBuffer缓冲区来存储读取到的数据。在循环中,我们使用FileChannel的read()方法将数据读取到缓冲区中,然后使用ByteBuffer的flip()方法切换读写模式,从而可以读取缓冲区中的数据。最后使用ByteBuffer的clear()方法清空缓冲区。 需要注意的是,在使用FileChannel读取数据时,需要先将数据读取到缓冲区中,然后再从缓冲区中读取数据。同时,由于缓冲区的大小是有限的,因此需要在循环中反复读取数据,直到读取到文件的末尾为止。 除了读取数据外,FileChannel还可以用于写入数据、截取文件、移动文件指针等操作。使用Java中的NIO操作可以提高文件读取效率,适用于需要频繁读写文件的场景。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值