1

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

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
Rad1
  • 185
  • 1
  • 4
  • 17

4 Answers4

2

You may try to trim the irrelevant spaces from every line ?

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

Edit : don't do that (thanks Joop Eggen!) or you'll have a nice NPE...). Try:

while ((strLine = br.readLine()) != null) {
    strLine = strLine.trim();
    ...
}
johan d
  • 2,798
  • 18
  • 26
  • Good idea, one often forgets the simple `.trim()`. Though do it inside `{ ... }` as you still have not tested for strLine being null. – Joop Eggen Jul 17 '13 at 11:32
  • it is only effective when the line begins with only one space: – Rad1 Jul 17 '13 at 11:35
  • @Rad1 What do you mean be "effective"? It also works with more than one space. That's what `trim()` is for. – Zhedar Jul 17 '13 at 12:12
  • According to the [documentation](http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#trim%28%29) trim works for multiple characters and not only for space-char. @Rad1 may be you meant efficient, which depends on the implementation. – Jost Jul 17 '13 at 12:14
  • @Zhedar i mean when the buffer is reading the file , with trim if the line begins with one space it reads it , when more than one space it consider that the file is over and exit ! – Rad1 Jul 17 '13 at 12:46
  • @Zhedar it is not a problem of space number , i think the text is too long for the buffer or something else it stops after a number of lines – Rad1 Jul 17 '13 at 13:14
1

Sounds for me like you should use regular expressions (e.g. use the String.matches() method). They also can extract strings or substrings (see: another Stackoverflow article).

There is also an excellent introduction by Lars Vogella about regular expressions in Java. Oracle compiled also a Tutorial/Lesson about that topic.

May be this snippet helps a bit (uses org.apache.commons.io.LineIterator):

public void grepLine(File file, String regex)
{
    LineIterator it = FileUtils.lineIterator(file, "UTF-8");
    try
    {
        while (it.hasNext())
        {
            String line = it.nextLine();
            if(line.matches(regex))
            {
                    //...do your stuff
            }
        }
    }
    finally
    {
        LineIterator.closeQuietly(it);
    }
}

The regex might be something like (note: havn't checked it - especially the backslashes):

String regex="^\\s*if\\s+\\[\\s+\\\"\\$1\\\" = \\\""+config +"\\\" \\] ; then";
Community
  • 1
  • 1
Jost
  • 1,549
  • 12
  • 18
0

Before all else: leave out DataInputStream, more Java Object specific.

boolean started = false;
while ...
    if (!started) {
        started = strLine.matches("\\s*...\\s*");
    } else {
        ...

Reg ex \\s* stand for zero or more white-space characters (tab, space).

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
0

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 !

Rad1
  • 185
  • 1
  • 4
  • 17