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'/改行、およびその中間の // または同様のものを無視させることができますか?

ps. String.split を使用して質問に答えてもかまいません。

4

4 に答える 4

2

メソッドcountTokensを使用して、2 つのトークンを持たない行をスキップします。

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

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

于 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レンダリングエンジンを作成しました。これにより、リーダーから直接解析できるようになり、使用していると思われる番号を識別するための便利な機能も提供されます。(日食が読み込まれたら、例を投稿します!)

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 に答える