我目前正在研究 INI 文件解析库,但是当我尝试使用我制作的方法创建类别时,我遇到了一个问题。
我正在使用的 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)
它在两个类别之间创建了两个空行......但我不明白为什么,因为我检查最后一行是否已经是空白。我对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]
在我调用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]