你如何让String Tokenizer忽略文本?

时间:2012-01-14 09:26:05

标签: java stringtokenizer

我有这段代码:

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?");

那么我怎么能让Tokenizer忽略'ENTER'/ new line,以及//之间的任何东西//或类似的东西,谢谢。

ps.我不在乎您是否使用String.split来回答我的问题。

4 个答案:

答案 0 :(得分:2)

使用方法countTokens跳过没有两个令牌的行:

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

答案 1 :(得分:1)

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

PS。你可以保持并关闭输入流。

答案 2 :(得分: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:

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"

答案 3 :(得分:0)

如果您正在阅读文件,则更好的方法是使用 StreamTokenizer 。然后,您可以声明自己的tokenizer语法。我使用此方法来创建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