0

私は現在、INI ファイル解析ライブラリに取り組んでいますが、作成したメソッドでカテゴリを作成しようとすると、1 つだけ問題が発生します。

私が使用している INI ファイルの内容は次のとおりです。

; Format example
[Category]
key=value

; Just a test to see if setting the same key in a different category
; would mess up the reading, which it didn't.
[Language]
cplusplus=C++
key=java
(No extra line; file ends at "key=java")

メソッドを使用してカテゴリを作成すると、十分な数の新しい行を追加しようとするため、各カテゴリには前のカテゴリの終わりと作成中のカテゴリの間に空白行が 1 行あります。ファイルの最後に余分な空白行がないままファイルを保持すると、正常に動作します。しかし、ファイルの内容を次のように変更すると:

; Format example
[Category]
key=value

; Just a test to see if setting the same key in a different category
; would mess up the reading, which it didn't.
[Language]
cplusplus=C++
key=java

(File has extra line; file ends after the "key=java" line)

2 つのカテゴリの間に 2 つの空の行が作成されます...しかし、最後の行が既に空白であるかどうかを確認すると、その理由がわかりません。私は Java に少し慣れていないので、明らかな間違いを犯している場合は教えてください。

/**
 * Adds a category to the current INI file.
 * 
 * @param category The category to be added to the file
 */
public void addCategory(String category) {
    if (iniFile == null) return;

    // Checking to see if the file already contains the desired category
    boolean containsCategory = false;
    String lastLine = "";
    String string;

    try (BufferedReader in = new BufferedReader(new FileReader(iniFile))) {
        while ( (string = in.readLine()) != null) {
            // Line is a comment or is empty, ignoring
            if (!string.equals(""))
                if (string.charAt(0) == ';' || string.charAt(0) == '#')
                    continue;

            lastLine = string;

            // Line is a category, checking if it is the category that is
            // about to be created
            if (!string.equals("") && string.charAt(0) == '[') {
                String thisCategory = string.substring(1, string.length() - 1);
                if (thisCategory.equals(category))
                    containsCategory = true;
            }
        }
    } catch (IOException e) {}

    // The file does not contain the category, so appeanding it to the end
    // of the file.
    if (!containsCategory) {
        try (BufferedWriter out = new BufferedWriter(new FileWriter(iniFile, true))) {
            if (!lastLine.equals("")) {
                System.out.println("Adding extra whitespace");
                out.newLine();
                out.newLine();
            }

            out.write('[' + category + ']');
            out.close();
        } catch (IOException e) {}
    }
}

完全なファイル ソースを表示する場合は、アップロード先のGitHubへのリンクを次に示します。

編集:

addCategory(String)メソッドを呼び出した後、ファイルがどのように見えるかを提供する必要があると思いました。

最初のファイルを呼び出しaddCategory("Example")た後 (余分な空白行はありません):

; Format example
[Category]
key=value

; Just a test to see if setting the same key in a different category
; would mess up the reading, which it didn't.
[Language]
cplusplus=C++
key=java

[Example]

2 番目のファイルを呼び出しaddCategory("Example")た後 (ファイルの最後に余分な空白行を追加):

; Format example
[Category]
key=value

; Just a test to see if setting the same key in a different category
; would mess up the reading, which it didn't.
[Language]
cplusplus=C++
key=java


[Example]
4

2 に答える 2

0

スキャナー クラスの使用に反対するものはありますか?

Scanner を使用すると、ファイルを 1 行ずつ読み取り、結果の文字列を = で分割できます。行で分割が失敗した場合 (結果の String[] の長さが 1 である場合)、その行の長さが 1 で "\n" と一致する場合、行の形式が正しくないことがわかります。 scanner.hasNextLine() == false、EOF。

于 2012-12-29T23:29:32.343 に答える
0

lastLineファイルの最後の行が(または)""に過ぎない場合、空 ( ) になります。行末文字を取り除きます。 \n\r\nreadLine

lastLineが空でない場合、ファイルを書き込むときに空白のみを追加しています。だから...それは問題ではありません。

でも:

  1. おそらく行末文字がない最後の行を考慮していません。
  2. 「余分な」行を含むソースファイル...には、行末文字はありませんが、スペースがあります。

それが、表示する出力を生成する唯一のものです。改行を 2 つ追加すると、欠落していた余分な行に改行が 1 つ追加され、2 つ目の「空白」行が作成されます。行末文字はありませんでした。

で終わる 2 つの改行をファイルに追加しても、key=java「空白行」が 1 つしか生成されないのもそのためです。

ファイルを読み取るときは、これを考慮する必要があります。おそらく正規表現?

正直なところ、毎回ファイル全体を書き込む方が簡単です。これは、組み込みPropertiesオブジェクトが Java で行うことです。

于 2012-12-30T00:10:32.907 に答える