0

我有这个代码:

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

4 回答 4

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
        }
} 
于 2012-01-14T10:00:51.727 回答
1
Properties prop = new Properties();
prop.load(new FileInputStream("properties_file.txt"));
assertExuals("1",prop.getProperty("Total"));

附言。您可能会按住并关闭输入流。

于 2012-01-14T09:36:32.087 回答
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"
于 2012-01-14T09:40:27.983 回答
0

如果您正在阅读文件,更好的方法是使用StreamTokenizer。然后,这允许您声明自己的标记器语法。我使用这种方法创建了一个 HTML 渲染引擎。然后,这允许您直接从阅读器解析,并提供有用的功能来识别数字,您似乎可以使用这些功能。(我将在我的 Eclipse 加载后发布一个示例!)

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

于 2012-01-14T12:30:05.947 回答