내 문제가 StringTokenizer
만의 첫 번째 줄을 읽는 것이다 ip.txt자바 있고 StringTokenizer는 첫 번째 라인을 읽는
및 배열에 저장하여 내 네트워크의 장치에 자동으로 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.txtDesktop1//192//168//1//127//
Desktop2//192//168//1//128//
Desktop3//192//168//1//129//
그냥 말씀 드리지만 여기
는 코드입니다. "... _StringTokenizer는 호환성을 이유로 유지되는 레거시 클래스이며 새 코드에서는 사용하지 않는 것이 좋습니다.이 기능을 사용하려는 사람은 String의 split 메서드 나 java.util.regex 패키지를 대신 사용하는 것이 좋습니다. .. " –내 컴퓨터에서 코드를 실행 해봤는데 정상적으로 작동합니다. ip.txt 파일 전체를 읽는 중입니다. –