简体   繁体   English

读取 Java 中的格式化文本

[英]Read formatted text in Java

I'm trying to read a formatted file in Java, I used to do this pretty well in C but no clue here.我正在尝试读取 Java 中的格式化文件,我曾经在 C 中做得很好,但这里没有线索。 The example line is:示例行是:

A '0' B C A '0' B C

And I want to take A and 0 as two separated Strings and [B, C] as two Strings in a String ArrayList.我想将 A 和 0 作为两个单独的字符串,将 [B, C] 作为字符串 ArrayList 中的两个字符串。

The line fomrat can be modified in anyway, for example adding commas无论如何都可以修改行格式,例如添加逗号

A '0' B, C, D... A '0' B, C, D...

Any idea on how to split this?关于如何拆分这个的任何想法? I used to do it with fseek, fread, etc when working in C我以前在 C 工作时使用 fseek、fread 等

Plese try the below code: Idea here is to use the Java "Scanner" class.请尝试以下代码:这里的想法是使用 Java“扫描仪”class。 This will read the file by line until end of file is reached.这将逐行读取文件,直到到达文件末尾。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM