Search code examples
javabufferedreaderfileinputstream

Get specific text from a .txt file


I want to get information from a script so i used this function

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

However i want to begin reading from a particular line which is

if [ \"$1\" = \""+config +"\" ] ; then

the problem is that when a line begins with a space the program considers that the file have ended ! So how can i fix it and make the program pars to the end of file ? considering that the line could begin with more thant one space thx


Solution

  • I found a solution which i share with you .

    public static HashMap<String, String> getEnvVariables(String scriptFile ,String config1,String config2) {
        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 = config2;
            String startvar =config1;
            String keyword = "set";
            do {
                if (strLine != null && strLine.contains(startvar)) {
                    if (strLine.contains(stopvar)) {
                        return vars;
                    }
                    while (strLine != null && !strLine.contains(stopvar)) {
                        strLine = br.readLine();
                        if (strLine.trim().startsWith(keyword)&& !strLine.contains("$")) {
                            strLine = strLine.trim().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) {
            Status status = new Status(Status.ERROR, Activator.PLUGIN_ID,
                    IStatus.ERROR, e.getMessage(), e);
            Activator.getDefault().getLog().log(status);
        }
        return vars;
    }
    

    thanks for helping !