0

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

        }
    });
user6680
  • 79
  • 6
  • 34
  • 78
  • http://docs.oracle.com/javase/tutorial/essential/io/charstreams.html or, if your file contains just one line, use http://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html#readAllLines-java.nio.file.Path-java.nio.charset.Charset- and take the first line from the returned list. – JB Nizet Jun 21 '15 at 17:06
  • 1
    That looks like an LDAP Distinguished Name (DN). Use an LDAP / LDIF library like Spring LDAP to parse the DN and extract the CN component. – dnault Jun 21 '15 at 18:00
  • 1
    possible duplicate of [Parsing the CN out of a certificate DN](http://stackoverflow.com/questions/7933468/parsing-the-cn-out-of-a-certificate-dn) – dnault Jun 21 '15 at 18:00
  • I updated my code above. I tried to use LDAP, but I'm not really sure what I should assign to the variable. I tried assigning ldapName = new LdapName(sCurrentLine); but I'm getting a null pointer exception. Any suggestions. – user6680 Jun 21 '15 at 18:18
  • 1
    Use `javax.naming.ldap.LdapName` to parse LDAP correctly. – Stavr00 Jun 21 '15 at 18:21
  • I imported javax.naming.ldap.LdapName, but it isn't even utilizing this line. See screenshot for reference. – user6680 Jun 21 '15 at 18:26

3 Answers3

0

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?

Community
  • 1
  • 1
V_Singh
  • 729
  • 11
  • 22
0

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 !

Kutay Demireren
  • 640
  • 1
  • 10
  • 25
  • I tried using your code, but I'm getting an output of "null". I feel like I'm really close, but I'm not sure what I'm missing. I updated code above. – user6680 Jun 21 '15 at 19:13
  • Firstly, check if you are getting the array as we wanted. If you are getting then take the first element and just delete CN= from the string. Well if you are not getting the array we wanted, can u debug and inform me what is the tokens array currently? – Kutay Demireren Jun 21 '15 at 21:37
  • I just realized that you better use split method with CurrentLine which is the line itself. So use as CurrentLine.split(","). I also wrote wrong in my code and editing it right now. Please, check it again. – Kutay Demireren Jun 22 '15 at 06:50
0

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.

pkozlov
  • 746
  • 5
  • 17
  • I'm trying to pull ("CN=COUD111255,OU=Workstations,OU=Mis,OU=Accounts,DC=FLHOSP,DC=NET") from a txt file and not a hardcoded string. Grabbing it from the txt file is my problem – user6680 Jun 21 '15 at 18:29
  • Well, as Kutay Demireren already answered you - what you need is BufferedReader and readLine method - it returns one string at a time. When file is finished (EOF) it returns null. As I understand - you have huge file with a lot of lines - probably you can collect a list of CNs from all lines. – pkozlov Jun 21 '15 at 18:36