我需要扫描 .txt 文件中的一系列行,如 this picture 产生如下输出:“this output
这是我的代码:
Scanner scan = new Scanner(new File("C:\\a1.txt"));
String wordbuffer="";
while(scan.hasNext()) {
scan.useDelimiter("\\Z");
wordbuffer+=scan.next();
System.out.println(wordbuffer);
if(scan.next==" "){
wordbuffer="";
}
}
scan.close();
最佳答案
您可以尝试以下操作:
添加:
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
代码:
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(new FileReader("C:\\a.txt"));
StringBuilder sb = new StringBuilder();
while(in.hasNext()) {
String s=in.nextLine();
if (s.equals(""))
sb.append(System.lineSeparator());
else
sb.append(s);
}
in.close();
System.out.println( sb.toString());
}
Output:
stack
o2
flow
关于java - 使用扫描仪和分隔符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53400327/