在java中读取类似文本文件时出错

在java中读取类似文本文件时出错,java,linux,string,Java,Linux,String,我一次读取两个文件,但其中一个没有被正确读取。文本文件来自Linux /sbin/ifconfig | grep "inet addr:" > ips.txt 线条如下所示: inet addr:77.221.133.178 Bcast:77.221.133.183 Mask:255.255.255.248 inet addr:77.221.133.179 Bcast:77.221.133.183 Mask:255.255.255.248 inet addr:77.2

我一次读取两个文件,但其中一个没有被正确读取。文本文件来自Linux

/sbin/ifconfig | grep "inet addr:" > ips.txt
线条如下所示:

  inet addr:77.221.133.178  Bcast:77.221.133.183  Mask:255.255.255.248
  inet addr:77.221.133.179  Bcast:77.221.133.183  Mask:255.255.255.248
  inet addr:77.221.133.180  Bcast:77.221.133.183  Mask:255.255.255.248
无法读取文件中的行:

  inet addr:192.64.177.185  Bcast:192.64.177.255  Mask:255.255.255.128
      inet addr:192.64.182.2  Bcast:192.64.182.127  Mask:255.255.255.128
      inet addr:192.64.182.3  Bcast:192.64.182.127  Mask:255.255.255.128
java程序只需要提取IP。正在正确读取文件1,但仅从文件2获取第一个IP。他们来自两个不同的服务器。一个是Centos 6.3,另一个是Centos 6.6。我想这是一些格式或编码问题,但我无法解决。提取IP的代码

static ArrayList<String> list = new ArrayList<String>();
File file = new File("C:\path\ips.txt"); 

        Scanner scan = null;
        try {
            scan = new Scanner(file);
            while (scan.hasNext()) {
                extractIP(scan.nextLine());
             }

            System.out.println("IPs: "+list.size());
            System.out.println(list.toString());
        } catch (FileNotFoundException e) {
            System.out.println("file with IPs not found");
        } finally {
            if (scan != null) {
                scan.close();
            }
        }
static void extractIP (String ipString){
    String[] strarr = ipString.split("inet addr:");
    if (!(strarr[1].contains("127"))){
    list.add(strarr[1].substring(0, strarr[1].indexOf(" ")));
    }
}
static ArrayList list=new ArrayList();
File File=新文件(“C:\path\ips.txt”);
扫描仪扫描=空;
试一试{
扫描=新扫描仪(文件);
while(scan.hasNext()){
extractIP(scan.nextLine());
}
System.out.println(“IPs:+list.size());
System.out.println(list.toString());
}catch(filenotfounde异常){
System.out.println(“找不到IP的文件”);
}最后{
如果(扫描!=null){
scan.close();
}
}
静态void extractIP(字符串ipString){
String[]strarr=ipString.split(“inet addr:”);
如果(!(strarr[1]。包含(“127”)){
list.add(strarr[1]。子字符串(0,strarr[1]。indexOf(“”));
}
}

我避免对IP使用regex,因为它也提取广播IP

文件2的第二行和第三行在
Bcast
字段中包含
127

编辑如果您可以在Linux机器上运行代码,并且不限于从文本文件中读取代码,那么您可以按以下方式获取所有inet地址:

Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
for (NetworkInterface iface : Collections.list(nets)) {
    List<InetAddress> addrs = Collections.list(iface.getInetAddresses());
    for (InetAddress addr : addrs) {
        System.out.println(addr.toString().split("/")[1]);
    }
}
Enumeration nets=NetworkInterface.getNetworkInterfaces();
用于(网络接口iface:Collections.list(网络)){
List addrs=Collections.List(iface.getInetAddresses());
用于(InetAddress地址:地址){
System.out.println(addr.toString().split(“/”[1]);
}
}

错误是什么?您是否尝试过区分文件?(顺便说一句:在这种情况下你也可以使用regex。)抱歉,这些文件不完全相同(将进行编辑),但它们具有相同的结构-只是不同服务器的IP不同@peterremec错误是,从第二个文件中只获取第一个IP。第一个文件是正确读取的在这种情况下,首先要做的是在十六进制编辑器中打开文件,以确保文件不包含任何控制字符,或者您的空格是空格而不是制表符。您没有向我们显示您使用的读取循环。你同时在读吗?一个接一个运行你的程序两次?谢谢,就这样。我把它编辑成!((ipString.contains(“127.0.0.1”))及其工作。您的建议可能是完成此工作的更好方法。感谢大家的快速响应。