繁体   English   中英

如何使用令牌读取文本文件并将每个令牌内容保存在对象数组Java中

[英]How to read a text file with tokens and save each token content in array of objects Java

我正在尝试在多行文本文件中读取令牌及其值,该文件具有我指定的格式,到目前为止,我已经使用Scanner和正则表达式读取了文件,我能够区分令牌(因为它们总是带有单词“ process”,后跟数字)。 这是期望结果的一个例子。

objects[
        [process0, [1,2,3,4,5]], 
        [process1, [6,7,8,9,10]], 
        [process2, [1,2]]
 ]

我想在将新对象添加到对象列表之后每次将以下行放入while循环中以清理数组(由于我在else块中使用while循环的方法不起作用,因此我将其留在了外部并我没有包含该循环)

List<String> process_numbers = new ArrayList<String>();

我的想法是读取一个令牌,然后在下一个令牌之前获取数字,并将其存储在一个对象中,然后将该对象添加到对象列表中,因此基本上我需要帮助。

注意:我显然不擅长Java,可以随时提出改进代码的建议。

Main.java

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

public class Main {
    public static void main(String[] args) {
        try {
         File f = new File("processes.txt");
         Scanner sc = new Scanner(f);

         List<Objects> objects = new ArrayList<Objects>();
         String process_name = " ";
         List<String> process_numbers = new ArrayList<String>();

         while(sc.hasNextLine()){

            String line = sc.nextLine();
            if (line.matches(".*process.*")){
                process_name = line;
                //System.out.println("PROCESS NAME: " + line);
            }else{
                process_numbers.add(line);
                //System.out.println("NUMBER: " + line);
            }

            Objects o = new Objects(process_name, process_numbers);
            objects.add(o);
        }

      } catch (FileNotFoundException e) {
         e.printStackTrace();
     }
  }
}

Objects.java

import java.util.ArrayList;
import java.util.List;
public class Objects {
    private String process_name;
    List<String> process_numbers = new ArrayList<String>();

    public Objects(String process_name, List<String> process_numbers){
      this.process_name = process_name;
      this.process_numbers = process_numbers;
    }

    public String getProcess_name() {
      return process_name;
    }

    public void setProcess_name(String process_name) {
     this.process_name = process_name;
    }

    public List<String> getProcess_numbers() {
     return process_numbers;
    }

    public void setProcess_numbers(List<String> process_numbers) {
     this.process_numbers = process_numbers;
    }

 }

processes.txt

process0
1
2
3
4
5
process1
6
7
8
9
10
process2
1
2

编辑:

我以这种方式修改了while循环:

        while(sc.hasNextLine()) {

            String line = sc.nextLine();
            List<String> process_numbers = new ArrayList<String>();
            if (line.matches(".*process.*")) {
                process_name = line;
                line = sc.nextLine();
            }

            while (!line.matches(".*process.*") && sc.hasNextLine()) {
                process_numbers.add(line);
                line = sc.nextLine();
            }

            Objects o = new Objects(process_name, process_numbers);
            objects.add(o);

        }

        for(Objects obj : objects) {
            System.out.println(obj.getProcess_name());
            System.out.println(obj.getProcess_numbers());

        }

我有点想要的结果,但是有一些问题,首先,进程的名称都相同(“ process0”),并且程序没有打印文本文件的最后一个数字。

到目前为止的结果

process0
[1, 2, 3, 4, 5]
process0
[6, 7, 8, 9, 10]
process0
[1]

差不多了,但是您总是跳过带有进程名称的行

String line = sc.nextLine();
while(sc.hasNextLine()) {
    List<String> process_numbers = new ArrayList<String>();
    if (line.matches(".*process.*")) {
        process_name = line;
        line = sc.nextLine();
    }

    while (!line.matches(".*process.*") && sc.hasNextLine()) {
        process_numbers.add(line);
        line = sc.nextLine();
    }
    if(!sc.hasNextLine()){
         process_numbers.add(line);
    }

    Objects o = new Objects(process_name, process_numbers);
    objects.add(o);

}

输出:

process0
[1, 2, 3, 4, 5]
process1
[6, 7, 8, 9, 10]
process2
[1, 2]

暂无
暂无

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

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