Begin the reading of a file from a specific line

前端 未结 3 1232
小蘑菇
小蘑菇 2021-01-27 18:01

i have a file similaire to this : ...

The hotspot server JVM has specific code-path optimizations
# which yield an approximate 10% gain over the client versi         


        
相关标签:
3条回答
  • 2021-01-27 18:24

    Your example code is quite far off, and I don't intend to rewrite all of your code, I will give you some pointers though. You are already doing:

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

    This is your conditional that should be testing for the "HDK1001" string instead of whatever it's doing right now. I'm not sure why you are checking for the word "export" when it seems like it doesn't matter for your program.

    There isn't a way to just magically start and end at specific words in the file, you MUST start at the beginning and go line by line checking all of them until you find your desired first and last line. Once you find that first line, you can continue reading until you reach your desired end line and then bail out.

    0 讨论(0)
  • 2021-01-27 18:31

    this is pseudo code that follows the kind of logic you would want to be able to accomplish this task.

    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
     }
    
    0 讨论(0)
  • 2021-01-27 18:37

    try this code.

    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;
        }
    
    0 讨论(0)
提交回复
热议问题