2012-01-14 18 views
0

私はこのコードを持っている:String Tokenizerでテキストを無視する方法を教えてください。

public void readTroops() { 
     File file = new File("resources/objects/troops.txt"); 
    StringBuffer contents = new StringBuffer(); 
    BufferedReader reader = null; 

    try { 
     reader = new BufferedReader(new FileReader(file)); 
     String text = null; 

     // repeat until all lines is read 
     while ((text = reader.readLine()) != null) { 
      StringTokenizer troops = new StringTokenizer(text,"="); 
      String list = troops.nextToken(); 
      String value = troops.nextToken(); 
} 

と、このファイル:

//this is a comment part of the text file// 

Total=1 

問題は、1)私はそれが//、//内のすべてのものを無視し得るカントと得ることができないということですがそれらの間の 'ENTER'(行)で読み取ることができます。たとえば、このテキストは動作します:

Total=1 

だから私の質問は、私はデリミタ領域、すなわちに入力しないものです。

StringTokenizer troops = new StringTokenizer(text,"=","WHAT GOES HERE?"); 

は、どのように私は、トークナイザが/ 'ENTERの新しいライン、およびこれらの間//または似たようなもの、感謝を無視するように得ることができます。

ps.私の質問に答えるためにString.splitを使用するかどうかは気にしません。

+0

あなたはプロパティファイルを解析している(多分コメント名前=値のペアを?) – milan

+0

そのA txtファイルではなく、プロパティファイル(プロパティファイルはファイルタイプですか?)ではなく、rtfまたはrtfd ... – Russell

+0

はい、txtファイル、名前=値行、およびオプションでコメント? – milan

答えて

2

は、2つのトークンを持っていない行をスキップする方法countTokensを使用しますファイルをより良い方法は、StreamTokenizerを使用することです。これにより、トークナイザの独自の構文を宣言することができます。私はこの方法を使ってHTMLレンダリングエンジンを作成しました。これにより、読者からの直接の構文解析が可能になりますし、数字を識別するための便利な機能も提供されます。 (私は日食負荷後の例を掲載します!)

public static String render(String file, HashMap vars){ 

    // Create a stringbuffer to rebuild the string 
    StringBuffer renderedFile = new StringBuffer(); 
    try{ 
    FileReader in = new FileReader(file); 
    BufferedReader reader = new BufferedReader(in); // create your reader 
    StreamTokenizer tok; 
     tok = new StreamTokenizer(reader); //the tokenizer then takes in the reader as a builder 
     tok.resetSyntax(); 
     tok.wordChars(0, 255); //sets all chars (inc spaces to be counted as words) 
     /* 
     * quoteChar allows you to set your comment char, for example $ hello $ means it will ignore hello 
     */ 
     tok.quoteChar('$'); 

    while(tok.nextToken()!=StreamTokenizer.TT_EOF){ //while it is not at the end of file 
    String s = tok.sval; 
    if (vars.containsKey(s)) 
     s =(String)vars.get(s); 
    renderedFile.append(s); 
    } 

    } 
    catch(Exception e){System.out.println("Error Loading Template");} 

    return renderedFile.toString(); 

} 

良いチュートリアルのチェックこれをhttp://tutorials.jenkov.com/java-io/streamtokenizer.html

+0

ありがとう、ありがとう、ありがとう、ありがとう、ありがとう、ありがとう、ありがとう!!!!! – Russell

1
Properties prop = new Properties(); 
prop.load(new FileInputStream("properties_file.txt")); 
assertExuals("1",prop.getProperty("Total")); 

ps。入力ストリームを保持して閉じることができます。

1

トークン化ツールの代わりにPropertiesを使用することもできます(コメントを#で更新する場合)。

Properties troops = new Properties(); 
InputStream inputStream = SomeClass.class.getResourceAsStream("troops.properties"); 
try { 
    props.load(inputStream); 
} catch (IOException e) { 
    // Handle error 
} finally { 
    // Close inputStream in a safe manner 
} 
troops.getProperty("Total"); // Returns "1" 

それともは、Java 7を使用している場合:あなたが読んでいる場合

while ((text = reader.readLine()) != null) { 
     StringTokenizer troops = new StringTokenizer(text,"="); 
     if(troops.countTokens() == 2){ 
      String list = troops.nextToken(); 
      String value = troops.nextToken(); 
      .... 
     }else { 
      //ignore this line 
     } 
} 
0

Properties troops = new Properties(); 
try (InputStream inputStream = SomeClass.class.getResourceAsStream("troops.properties")) { 
    props.load(inputStream); 
} catch (IOException e) { 
    // Handle error 
} 
troops.getProperty("Total"); // Returns "1" 
関連する問題