我想从脚本中获取信息,所以我使用了这个函数
public static HashMap<String, String> getEnvVariables(String scriptFile,String config) {
HashMap<String, String> vars = new HashMap<String, String>();
try {
FileInputStream fstream = new FileInputStream(scriptFile);
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
String var= "if [ \"$1\" = \""+config +"\" ] ; then";
// Read File Line By Line
while ((strLine = br.readLine()) != null) {
// use a Scanner to parse the content of each line
// exclude concatenated variables (export xx:$xx)
if (strLine.startsWith("export") && !strLine.contains("$")) {
strLine = strLine.substring(7);
Scanner scanner = new Scanner(strLine);
scanner.useDelimiter("=");
if (scanner.hasNext()) {
String name = scanner.next();
String value = scanner.next();
System.out.println(name+"="+value);
vars.put(name, value);
}
}
}
但是我想从一个特定的行开始阅读
if [ \"$1\" = \""+config +"\" ] ; then
问题是当一行以空格开头时,程序认为文件已经结束!那么我该如何修复它并使程序解析到文件末尾?考虑到该行可能以不止一个空格开头 thx