java中的文件读取格式化文本

0tdrvxhp  于 2021-06-27  发布在  Java
关注(0)|答案(1)|浏览(394)

我正在尝试用java读取格式化文件,我以前用c做得很好,但这里没有线索。示例行是:
a“0”b c
我想把a和0作为两个分开的字符串,[b,c]作为字符串数组列表中的两个字符串。
行fomrat无论如何都可以修改,例如添加逗号
a'0'b,c,d。。。
你知道怎么分吗?我在c工作的时候经常和fseek,fread等一起做

vybvopom

vybvopom1#

请尝试下面的代码:这里的想法是使用java“scanner”类。这将逐行读取文件,直到到达文件末尾。

import java.io.File;  
import java.io.FileNotFoundException;  
import java.util.Scanner; 

public class fileReader {
  public static void main(String[] args) {
    try {
      File oFile = new File("myfile.txt");
      Scanner oScanner= new Scanner(oFile );
      while (oScanner.hasNextLine()) {
        String sLine = oScanner.nextLine(); //Next line will point to the next line on the file
        System.out.println(sLine ); //And any other operations on the line you would like to perform.
      }
      oScanner.close();
    } catch (FileNotFoundException e) {
      System.out.println("Error Occurred");
      e.printStackTrace();
    }
  }
}

相关问题