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

时间:2016-06-03 00:13:24

标签: java constructor io bufferedreader

我有一个代码从文本文件中读取三行的id块,我想在Strings和int中拆分这些行,并将其传递给另一个类的构造函数。 现在,这个类要求输入一个id号并打印#符号下面的所有内容,直到并包括每个id块的第三行。我希望每个id中的这三行被解析为Employee类的'name','age'和'job',以便创建id的对象,如下所示:Employee(“Richard Smith”,22, “电气工程师”)。文件中总会有三行用于id,那么如何“找到”这些行并将它们拆分成字符串和一个int?

#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;
  }

}

1 个答案:

答案 0 :(得分:1)

您只需使用( )即可保存从文件中读取的内容并在以后处理。由于您确定已保存文件的功能在包含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); } } } 之间 这是代码:

{{1}}