您好,我正在尝试读取文件的一些凭据,但我只想读取分号后的数据。所以我正在考虑逐行阅读并使用 REGEX 但不太确定如何做到这一点。
该文件的格式类似于 .
Name: Johnny
Age : 23
City: London
代码:
public void readFileForDataBaseCredentials() {
Scanner newFile = null;
try {
newFile = new Scanner(new FileReader(selectedFile));
} catch (Exception e) {
System.out.println("could not find the file");
}
while (newFile.hasNext()) {
}
}
最佳答案
正则表达式对此来说太多了。只需获取 :
字符后面的子字符串:
while (newFile.hasNext()) {
String s = newFile.next();
int ind = s.indexOf(":");
if(ind != -1){
String value = s.substring(ind); //value is what you want
}
}
关于java - 如何读取文件中某个字符后的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36746551/