java - 如何从文件中读取并将其发送到 Java 中的构造函数

标签 java constructor io bufferedreader

我有一段代码从文本文件中读取三行的 id block ,我想将这些行拆分为字符串和 int,并将其传递给另一个类的构造函数。 现在,此类请求 id 号并打印 # 符号下方的所有内容,直至并包括每个 id block 的第三行。我希望每个 id 中的这三行被解析为 Employee 类的“姓名”、“年龄”和“工作”,以便创建 id 的对象,如下所示: Employee("Richard Smith",22, “电气工程师”)。文件中总会有三行用于 id,那么我如何“找到”这些行并将它们拆分为字符串和整数?

#1000
Richard Smith
22
Electric engineer

#1001
Elliot Smith
23
Physicist



public class RdB
{
    String b; //file name

    RdB(String ename) {
        b = ename;
    }

    //info about an employee
    boolean showE (String id) {
        int ch;
        String code, info;

        //open the file with the info
        try (BufferedReader showERdr = 
            new BufferedReader (new FileReader(b)))
        {
            do {
                //read characters until a '#' is found
                ch = showERdr.read();

                //check
                if(ch == '#') {
                    code = showERdr.readLine();
                    if(id.compareTo(code) == 0) { //found employee
                        do {
                            info = showERdr.readLine();
                            if(info != null) {
                                System.out.println(info);
                            }
                        } while(((info != null) &&
                            (info.compareTo("") != 0)));
                        return true;
                    }
                }
            } while(ch != -1);
        }
        catch(IOException exc) {
            System.out.println("File error!");
            return false;
        }
        return false; //employee not found
    }

    //Access a registered employee
    String getE() {
        String id = "";

        BufferedReader br = new BufferedReader(
                new InputStreamReader(System.in));

        System.out.println("Enter id number: ");
        try{
            id = br.readLine();
        }
        catch(IOException exc) {
            System.out.println("Access error");
        }
        return id;
    }
}

编辑: 员工类别

class Employee {
private String name, job;
private int age;

    public Employee (String name, int age, String job) {
       this.name = name;
       this.age = age;
       this.job = job;
    }

    public String getName () {
       return name;
    }

    public int getAge () {
       return age;
   }

    public String getJob () {
       return job;
  }

}

最佳答案

您可以简单地使用ArrayList来保存从文件中读取的内容并稍后进行处理。由于您确定保存的文件的功能,它总是在包含 ID 的行之前包含 3 行,因此这是我为解决您的问题而编写的工作代码。您可以看到包裹在 ( )
之间的输出 这是代码:

import java.io.*;
import java.util.List;
import java.util.ArrayList;

public class Emp{

public static void main(String[] args) throws IOException{
    List data = new ArrayList<String>();
    BufferedReader reader = new BufferedReader(new FileReader("PeopleData.txt"));
    String str;
    while((str = reader.readLine()) != null){
 //         System.out.println(str);
        data.add(str);
    }
    for(int i= 0; i< data.size(); i ++){
                i++;
                System.out.println("(");
                System.out.println(data.get(i));
                String name = (String) data.get(i);
                i++;
                System.out.println(data.get(i));
                int age = Integer.valueOf((String) data.get(i));
                i++;
                System.out.println(data.get(i));
                String job = (String) data.get(i);
                i++;
                System.out.println(")");
                //new Employee(name, age, job);
            }
}
}

关于java - 如何从文件中读取并将其发送到 Java 中的构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37604057/

相关文章:

Java 8 FilterOutputStream 异常

java - MessageSecurityMetadataSourceRegistry 的 Spring-Security jar 在哪里

java - JPA错误: without eclipselink

javascript - Array(5) 是否等同于 var a = []; a.长度=5;在 JS 中?

c++ - 复制构造函数 : deep copying an abstract class

java - 将字符串写入 .txt 文件不起作用

python - 如何在不等待输入的情况下检查可能为空的标准输入?

java - 带有动态参数的自定义注释

java - 如何从用户那里获取保存文件的路径?

c++ - 从另一个构造函数调用构造函数?