【发布时间】:2015-06-21 17:02:49
【问题描述】:
我有一个带有以下输出的 txt 文件:
"CN=COUD111255,OU=Workstations,OU=Mis,OU=Accounts,DC=FLHOSP,DC=NET"
我要做的是读取 COUD111255 部分并将其分配给 java 变量。我将 ldap 分配给 sCurrentLine,但我得到一个空点异常。任何建议。
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();
}
}
});
【问题讨论】:
-
docs.oracle.com/javase/tutorial/essential/io/charstreams.html 或者,如果您的文件只包含一行,请使用 docs.oracle.com/javase/8/docs/api/java/nio/file/… 并从返回的列表中取出第一行。
-
这看起来像一个 LDAP 可分辨名称 (DN)。使用像 Spring LDAP 这样的 LDAP / LDIF 库来解析 DN 并提取 CN 组件。
-
我更新了上面的代码。我尝试使用 LDAP,但我不确定我应该为变量分配什么。我尝试分配 ldapName = new LdapName(sCurrentLine);但我得到一个空指针异常。任何建议。
-
使用
javax.naming.ldap.LdapName正确解析LDAP。
标签: java bufferedreader