Java:文件读取器、按定界符分隔的字符串分隔符帮助
我正在尝试在 java 中创建一个预处理器,它将在源代码中读取。我尝试将代码全部读取为一个字符串。
问题:如何在 <<< 中间添加字符串>>>>>到它自己的某种数组列表。
public class processLines {
public void pLine (String FileName)throws IOException{
Scanner scanner = null;
try{
scanner = new Scanner(new BufferedReader(new FileReader(FileName)));
while (scanner.hasNext()) {
String Line = "";
String LineB = "";
String LineC = "";
ArrayList<String> inside = new ArrayList<String>();
Line = Line + scanner.next()+ " ";
System.out.println("outside token: "+ Line);
StringTokenizer token = new StringTokenizer(Line);
while(token.hasMoreTokens()&& token.nextToken() != null){
LineB = Line;
if(LineB.contains("<<<")){
if(!LineB.contains(">>>") ){
LineC = LineC + scanner.next()+ " ";
inside.add(LineC);
System.out.println("LineC: " + LineC);
System.out.print(inside);
}
if(scanner.next(">>>") != null){
Line = scanner.next();
System.out.println("Line INside:" + Line);
}
}
}
}
}
finally {
if (scanner != null) {
scanner.close();
}
}
}
文本
文件源代码在一行中包含“Mo <<< Mo Larry Curly>>Larry”。如果 <<< 中间只有一个名称,则此代码有效。 >>>>>但是当我添加更多时,我收到错误。
出现的错误消息:外部令牌:Mo
外部标记:<<< C线:钼 [Mo ]线程“main”中出现异常 java.util.InputMismatchException 在 java.util.Scanner.throwFor(来源未知) 在 java.util.Scanner.next(来源未知) 在 java.util.Scanner.next(来源未知) 在 processLines.pLine(processLines.java:26) 在 proProcess.main(proProcess.java:14)
I am trying to create a preProcessor in java where it will read in the source code. I tryed to read the code in all to one string.
Question: How do I add the strings in the middle of <<< >>> to its own array list of some sort.
public class processLines {
public void pLine (String FileName)throws IOException{
Scanner scanner = null;
try{
scanner = new Scanner(new BufferedReader(new FileReader(FileName)));
while (scanner.hasNext()) {
String Line = "";
String LineB = "";
String LineC = "";
ArrayList<String> inside = new ArrayList<String>();
Line = Line + scanner.next()+ " ";
System.out.println("outside token: "+ Line);
StringTokenizer token = new StringTokenizer(Line);
while(token.hasMoreTokens()&& token.nextToken() != null){
LineB = Line;
if(LineB.contains("<<<")){
if(!LineB.contains(">>>") ){
LineC = LineC + scanner.next()+ " ";
inside.add(LineC);
System.out.println("LineC: " + LineC);
System.out.print(inside);
}
if(scanner.next(">>>") != null){
Line = scanner.next();
System.out.println("Line INside:" + Line);
}
}
}
}
}
finally {
if (scanner != null) {
scanner.close();
}
}
}
}
The text file sourceCode includes " Mo <<< Mo Larry Curly >>> Larry" all on one line. this code works if there is only one name in the middle of the <<< >>> but when I added more I get an error.
Error message that occurs: outside token: Mo
outside token: <<<
LineC: Mo
[Mo ]Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at processLines.pLine(processLines.java:26)
at proProcess.main(proProcess.java:14)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是,您正在寻找下一个标记作为关闭三柱门,而这可能不是下一个标记。我会分解逻辑,以便您可以从输入文件中逐行读取。如果某行具有 wicket 分隔符,请使用不同的扫描仪在 wickets 之间分割内容。这是代码。这适用于三柱门内的零到多个令牌以及无三柱门的线。
The problem is that you're looking for the next token to be the closing wickets when that might not be the next token. I'd break the logic up so that you can read line by line from the input file. If a line has your wicket delimiters use a different scanner to split the content between the wickets. Here's the code. This works for zero to many tokens inside the wickets and lines w/o wickets.