[英]Assigning part of txt file to java variable
I have a txt file with the following output: 我有一个带有以下输出的txt文件:
"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. 我想要做的是阅读COUD111255部分并将其分配给java变量。 I assigned ldap to sCurrentLine, but I'm getting a null point exception.
我将ldap分配给sCurrentLine,但出现空点异常。 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();
}
}
});
You just need to read data from a file line by line and assign the line to your variable str. 您只需要逐行从文件中读取数据,并将该行分配给变量str。 Refer to following link: How to read a large text file line by line using Java?
请参阅以下链接: 如何使用Java逐行读取大文本文件?
To read from .txt, use BufferedReader. 要从.txt中读取,请使用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. testing.txt是您正在读取的txt的名称,必须在您的java文件中。 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]. 您得到的令牌数组为= [CN = COUD111255,OU =工作站OU =错误,OU =帐户,DC = FLHOSP,DC = NET]。 So, now take the 0th element of array and make use of it.
因此,现在使用数组的第0个元素并加以利用。 You got the CN=COUD111255, now!
您现在就拥有CN = COUD111255! Leaving here not to give whole code.
离开这里不要给出完整的代码。
Hope that helps ! 希望能有所帮助!
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. 给我“ COUD111255”作为答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.