0

我有一个与此类似的文件:...

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

请帮帮我

4

3 回答 3

0

您的示例代码相距甚远,我不打算重写您的所有代码,不过我会给您一些指示。你已经在做:

if (strLine.startsWith("export") && !strLine.contains("$"))

这是您的条件,应该测试“HDK1001”字符串,而不是它现在正在做的任何事情。我不确定您为什么要检查“导出”一词,因为它似乎对您的程序无关紧要。

没有办法在文件中的特定单词处神奇地开始和结束,您必须从开头开始逐行检查所有单词,直到找到所需的第一行和最后一行。一旦你找到第一行,你就可以继续阅读,直到你到达你想要的最后一行,然后退出。

于 2013-07-17T18:40:41.933 回答
0

这是遵循您希望能够完成此任务的逻辑的伪代码。

flag = false
inside a loop
{
 read in a line
 if( line != #############HDK1001############# && flag == false){  //check to see if you found your starting place
    nothing useful here. lets go around the loop and try again

  else // if i found it, set a flag to true
     flag = true;

  if( flag == true) // am i after my starting place but before my end place?
  {
    if( line == #############HDK1001#############) 
       do nothing and go back through the loop, this line is not useful to us

    else if( line ==  #############HDK7564#############) //did i find my end place?
      flag = false // yes i did, so lets not be able to assign stuff any more

    else // im not at the start, im not at the end. I must be inbetwee. lets read the data and assign it. 
      read in the lines and assign it to variables that you want
 }
于 2013-07-17T19:00:09.150 回答
0

试试这个代码。

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;
    }
于 2013-07-19T02:37:57.463 回答