Java的StringTokenizer的只读第一行
问题描述:
我的问题是StringTokenizer
似乎只能读的第一线ip.txtJava的StringTokenizer的只读第一行
我试图做的是加载IP列表从“ip.txt”的地址和将其保存到阵列中以自动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//
答
你的代码应该做工精细的例子。更经典的方法来遍历在时间的BufferedReader一条线是通过使用类似的表达式:
while ((line = input.readLine()) != null) {.
这是对我做你的代码的唯一重构和它工作。您可能需要打印完整的IP以检查您是否正确地检索它。
下面是代码:
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);
}
}
}
}
而一个例证:
+0
我不知道我的代码出了什么问题,但我认为这是eclipse的问题,因为它在重新启动我的机器后工作,这可能意味着我应该使用拆分方法。我还根据您的建议更改了我的代码。谢谢! – user2731181
只是一个什么样的Javadoc说的StringTokenizer的提醒。 “... _StringTokenizer是一个保留了兼容性的遗留类,虽然它在新代码中的使用不受欢迎。建议任何寻求该功能的人都使用String或java.util.regex包的split方法。 ..“ –
我试着在我的机器上运行你的代码,它工作正常。它读取整个ip.txt文件。 –