Java StringTokenizer仅读取第一行

时间:2016-10-29 05:54:09

标签: java arrays loops stringtokenizer inputstreamreader

我的问题是StringTokenizer似乎只读取了ip.txt的第一行

我要做的是从“ip.txt”加载一个IP地址列表并将其保存到一个阵列中,以自动ping我网络上的设备,看看他们是否在线。无论我尝试什么,我都无法获得超过“ip.txt”中第一行的数据。我的分隔符是“//”,IP地址的名称也存储在txt文件中。我包含了代码和文本文件的示例。

提前致谢!!

public class IP {
    public static IP[] ips = new IP[100];
    public static int total_ips=0;

    String name;
    String ip1;
    String ip2;
    String ip3;
    String ip4;
    String fullIP;

    public static void read_ips() throws FileNotFoundException{

        FileInputStream fstream1 = new FileInputStream("ip.txt");

        String line;
        String delimiter = "//";

        StringTokenizer tokenizer;
        BufferedReader input = null;     
        try {
            int i = 0;
            int totalIps = 0;

            input = new BufferedReader(new InputStreamReader(fstream1));
            line = input.readLine();

            //outer while
            while(line != null) {
            tokenizer = new StringTokenizer(line, delimiter);

            while(tokenizer.hasMoreElements()) {//process tokens in line
                ips[i] = new IP();
                ips[i].name = tokenizer.nextToken();
                ips[i].ip1 = tokenizer.nextToken();
                ips[i].ip2 = tokenizer.nextToken();
                ips[i].ip3 = tokenizer.nextToken();
                ips[i].ip4 = tokenizer.nextToken();
                ips[i].fullIP = ips[i].ip1+"."+ips[i].ip2+"."+ips[i].ip3+"."+ips[i].ip4;
                i++;
                totalIps = i;

                System.out.println(line);
            }   
            line = input.readLine(); //next line
            }//close outer while
            total_ips = totalIps; // count of total cars
            System.out.println("total_ips after i++ "+total_ips);
            }catch (FileNotFoundException e) {
                System.out.println("Unable to open file " + fstream1);
            } catch (IOException e) {
                System.out.println("Unable to read from file " + fstream1);
            }finally {
                // Close the file
                try {
             if (input != null)
                 input.close();
                } catch (IOException e) {
                    System.out.println("Unable to close file " + fstream1);
                }
            }   
    }
}

以下是ip.txt

的示例
Desktop1//192//168//1//127//
Desktop2//192//168//1//128//
Desktop3//192//168//1//129//

1 个答案:

答案 0 :(得分:0)

您的代码应该正常运行。迭代BufferedReader一次一行的更经典方法是使用如下表达式:

while ( (line = input.readLine()) != null) {. 

关于我对你的代码所做的唯一重构,它正在发挥作用。您可能需要打印fullIP以检查是否正确检索它。

以下是代码:

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class IP {
    public static IP[] ips = new IP[100];
    public static int total_ips=0;

    String name;
    String ip1;
    String ip2;
    String ip3;
    String ip4;
    String fullIP;

    public static void main(String[] args) throws IOException {
        read_ips();
    }
    public static void read_ips() throws FileNotFoundException{

        FileInputStream fstream1 = new FileInputStream("c:\\ips\\ip.txt");

        String line;
        String delimiter = "//";

        StringTokenizer tokenizer;
        BufferedReader input = null;     
        try {
            int i = 0;
            int totalIps = 0;

            input = new BufferedReader(new InputStreamReader(fstream1));
            while ( (line = input.readLine()) != null) {
                tokenizer = new StringTokenizer(line, delimiter);

                while(tokenizer.hasMoreElements()) {//process tokens in line
                    ips[i] = new IP();
                    ips[i].name = tokenizer.nextToken();
                    ips[i].ip1 = tokenizer.nextToken();
                    ips[i].ip2 = tokenizer.nextToken();
                    ips[i].ip3 = tokenizer.nextToken();
                    ips[i].ip4 = tokenizer.nextToken();
                    ips[i].fullIP = ips[i].ip1+"."+ips[i].ip2+"."+ips[i].ip3+"."+ips[i].ip4;
                    System.out.println(ips[i].fullIP);
                    i++;
                    totalIps = i;

                    System.out.println(line);
                }   
            }


            total_ips = totalIps; // count of total cars
            System.out.println("total_ips after i++ "+total_ips);
            }catch (FileNotFoundException e) {
                System.out.println("Unable to open file " + fstream1);
            } catch (IOException e) {
                System.out.println("Unable to read from file " + fstream1);
            }finally {
                // Close the file
                try {
             if (input != null)
                 input.close();
                } catch (IOException e) {
                    System.out.println("Unable to close file " + fstream1);
                }
            }   
    }
}

插图:

enter image description here