![](/img/trans.png)
[英]How do I tell if an empty line has been read in with a BufferedReader?
[英]How do you detect if a file has a blank line at the end with a BufferedReader?
我目前正在使用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")
之后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]
如果文件的最后一行只不過是\\n
(或\\r\\n
),則lastLine
將為空( ""
)。 readLine
行尾字符。
如果lastLine
不為空,則僅在寫入文件時添加空格。 所以...這不是問題。
然而:
那是唯一會生成您顯示的輸出的東西。 當您添加兩個換行符時,您將在缺少它的多余行中添加一個,然后創建第二個“空白”行; 沒有行尾字符。
這也是為什么將兩個換行符添加到以key=java
結尾的文件中只會產生一個“空白行”的原因。
讀取文件時需要考慮這一點。 正則表達式?
老實說,每次寫入整個文件都更加容易。 這就是內置的Properties
對象在Java中的作用。
您對使用掃描儀類有什么反對嗎?
使用掃描儀,您可以逐行讀取文件,然后在=處拆分結果字符串。 如果在一行上分割失敗(結果String []的長度為1),則您知道該行的格式不正確,如果該行的長度為1且匹配“ \\ n”,則為空行,如果Scanner.hasNextLine()== false,EOF。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.