我已經創建了要搜索的特定標籤的文件的自定義功能,並返回它的價值,就像這樣:自定義功能,搜索文件的標籤總是返回null
函數調用:
getSingleLineValue("tagname");
在文件
實測值行:
<tagname>=tagvalue
返回的字符串:
tagvalue
這裏是該函數的代碼:
public String getSingleLineValue(String tag) {
// The value
String value;
// If the list of passed tags contains the wanted tag
if(passedTags.contains(tag)) {
// Close the readers
close();
// RESET EVERYTHING
try {
// Re-create the FileReader
fileReader = new FileReader(file);
// Re-create the BufferedReader
bufferedReader = new BufferedReader(fileReader);
// Reset the passed tags array
passedTags.clear();
// Recall the function
value = getSingleLineValue(tag);
// Return the value
return value;
} catch(IOException e) {
// Handle the exception
e.printStackTrace();
}
} else {
try {
// The current line
String line;
// While the file has lines left
while ((line = bufferedReader.readLine()) != null) {
// If the current line contains a pair of tag marks (<and>)
if (line.contains("<") && line.contains(">")) {
// If the line contains the tag
if(line.contains(tag)) {
// Store the parts of the tag in an array (tag and value)
String[] tagParts = line.split("=");
// Get the value
value = tagParts[1];
// Return the value
return value;
} else {
// Get the passed tag
String passedTag = line.substring(1, line.indexOf(">") - 1);
// Add the tag to the passed tags array
passedTags.add(passedTag);
}
}
}
} catch(IOException e) {
// Handle the exception
e.printStackTrace();
}
}
// If the tag wasn't found, return null
return null;
}
對象調用file
是一個簡單的File
對象與路徑我想要閱讀該文件。它在同一個班上宣佈。
稱爲fileReader
和bufferedReader
的對象就是它聽起來的樣子。一個FileReader
和BufferedReader
,聲明如下(也在同一類):
private FileReader fileReader;
private BufferedReader bufferedReader;
而且在類的構造函數:
fileReader = new FileReader(file);
bufferedReader = new BufferedReader(fileReader);
的問題是,這個函數總是返回null,所以當我打電話吧,我是多麼希望它的工作反而會是這個樣子以上示範:
函數調用:
getSingleLineValue("tagname");
文件
找到行:
未知(?也許這就是問題所在)
返回的字符串:
null
問題可能是一些與行,因爲當我打印出來,我在控制檯中沒有收到任何消息,但我真的不知道它有什麼問題,如果是問題。
所有幫助非常感謝!