使用扫描仪和测距仪

时间:2018-11-20 19:36:32

标签: java java.util.scanner

我需要从看起来像this picture的.txt文件中扫描一系列行 进行如下输出:“ 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();

1 个答案:

答案 0 :(得分:0)

您可以尝试以下操作:

添加:

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

输出:

stack 
o2 
flow
相关问题