Assigning part of txt file to java variable

334 Views Asked by At

I have a txt file with the following output:

"CN=COUD111255,OU=Workstations,OU=Mis,OU=Accounts,DC=FLHOSP,DC=NET"

What I'm trying to do is read the COUD111255 part and assign it to a java variable. I assigned ldap to sCurrentLine, but I'm getting a null point exception. Any suggestions.

try (BufferedReader br = new BufferedReader(new FileReader("resultofbatch.txt")))
            {


                final Pattern PATTERN = Pattern.compile("CN=([^,]+).*");
                try {
                    while ((sCurrentLine = br.readLine()) != null) {
                        //Write the function you want to do, here.
                        String[] tokens = PATTERN.split(","); //This will return you a array, containing the string array splitted by what you write inside it.
                        //should be in your case the split, since they are seperated by ","

                    }
                     System.out.println(sCurrentLine);
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                } catch (IOException e2) {
                    // TODO Auto-generated catch block
                    e2.printStackTrace();
                }

        }
    });
3

There are 3 best solutions below

2
On

Your code is almost correct. You are writing this string to standard output - what for? If I understand you right, what you need is simply this:

private static final Pattern PATTERN = Pattern.compile("CN=([^,]+).*");

public static String solve(String str) {
    Matcher matcher = PATTERN.matcher(str);
    if (matcher.matches()) {
        return matcher.group(1);
    } else {
        throw new IllegalArgumentException("Wrong string " + str);
    }
}

This call

solve("CN=COUD111255,OU=Workstations,OU=Mis,OU=Accounts,DC=FLHOSP,DC=NET")

gave me "COUD111255" as answer.

0
On

You just need to read data from a file line by line and assign the line to your variable str. Refer to following link: How to read a large text file line by line using Java?

3
On

To read from .txt, use BufferedReader. To create a one, write:

BufferedReader br = new BufferedReader(new FileReader("testing.txt"));

testing.txt is the name of the txt that you're reading and must be in your java file. After initializing, you must continue as:

while ((CurrentLine = br.readLine()) != null) {
                //Write the function you want to do, here.
                String[] tokens = CurrentLine.split(","); //This will return you a array, containing the string array splitted by what you write inside it.
                //should be in your case the split, since they are seperated by ","
            }

You got tokens array which is = [CN=COUD111255,OU=Workstations OU=Mis,OU=Accounts,DC=FLHOSP,DC=NET]. So, now take the 0th element of array and make use of it. You got the CN=COUD111255, now! Leaving here not to give whole code.

Hope that helps !