2016-10-29 6 views
0

내 문제가 StringTokenizer 만의 첫 번째 줄을 읽는 것이다 ip.txt자바 있고 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// 
+2

그냥 말씀 드리지만 여기

는 코드입니다. "... _StringTokenizer는 호환성을 이유로 유지되는 레거시 클래스이며 새 코드에서는 사용하지 않는 것이 좋습니다.이 기능을 사용하려는 사람은 String의 split 메서드 나 java.util.regex 패키지를 대신 사용하는 것이 좋습니다. .. " –

+0

내 컴퓨터에서 코드를 실행 해봤는데 정상적으로 작동합니다. ip.txt 파일 전체를 읽는 중입니다. –

답변

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); 
       } 
      } 
    } 
} 

그리고 그림 : javadoc의가 있고 StringTokenizer에 대해서 말의

enter image description here

+0

나는 내 코드에 무엇이 잘못되었는지 알지 못하지만, 내 컴퓨터를 재부팅 한 후에 작동하기 때문에 일식에 문제가 있다고 가정합니다. 대신 분할 방법을 사용해야한다는 힌트 일 수 있습니다. 내가 추천 한대로 코드를 변경했습니다. 감사! – user2731181

관련 문제