小编典典

从特定行开始读取文件

java

我对此有一个文件模拟:…

The hotspot server JVM has specific code-path optimizations
# which yield an approximate 10% gain over the client version.
export CATALINA_OPTS="$CATALINA_OPTS -server"
#############HDK1001#############

# Disable remote (distributed) garbage collection by Java clients
# and remove ability for applications to call explicit GC collection
export CATALINA_OPTS="$CATALINA_OPTS -XX:+DisableExplicitGC"

# Check for application specific parameters at startup
if [ -r "$CATALINA_BASE/bin/appenv.sh" ]; then
. "$CATALINA_BASE/bin/appenv.sh"
fi
 #############HDK7564#############
# Disable remote (distributed) garbage collection by Java clients
# and remove ability for applications to call explicit GC collection
export CATALINA_OPTS="$CATALINA_OPTS -XX:+DisableExplicitGC"

我想从存在“ HDK1001”字样的行开始阅读,并在世界“ HDK7564”字样处结束阅读

我尝试使用此代码,但无法执行限制

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= "HDK1001";

        while ((strLine = br.readLine()) != null  ) {

            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);
                }
            }

请帮帮我


阅读 262

收藏
2020-11-30

共1个答案

小编典典

试试这个代码。

public static HashMap<String, String> getEnvVariables(String scriptFile,
            String config) {
        HashMap<String, String> vars = new HashMap<String, String>();
        BufferedReader br = null;
        try {
            FileInputStream fstream = new FileInputStream(scriptFile);
            br = new BufferedReader(new InputStreamReader(fstream));
            String strLine = null;
            String stopvar = "HDK7564";
            String startvar = "HDK1001";
            String keyword = "export";
            do {
                if (strLine != null && strLine.contains(startvar)) {
                    if (strLine.contains(stopvar)) {
                        return vars;
                    }
                    while (strLine != null && !strLine.contains(stopvar)) {
                        strLine = br.readLine();
                        if (strLine.startsWith(keyword)) {
                            strLine = strLine.substring(keyword.length())
                                    .trim();
                            String[] split = strLine.split("=");
                            String name = split[0];
                            String value = split[1];
                            System.out.println(name + "=" + value);
                            vars.put(name, value);
                        }
                    }
                }
            } while ((strLine = br.readLine()) != null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return vars;
    }
2020-11-30