Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于读取文本文件和创建对象的Java程序_Java_Arrays_Object - Fatal编程技术网

用于读取文本文件和创建对象的Java程序

用于读取文本文件和创建对象的Java程序,java,arrays,object,Java,Arrays,Object,我目前正在编写一个程序,从文本文件中读取数据,并以某种方式利用这些数据。到目前为止,我可以阅读的文件没有问题,但我有一个问题是什么下一步。当从文本文件读取数据时,必须从我已经构建的类中创建适当的对象,并根据数据存储在2个数组中。正如我之前所说,我已经完成了读取数据的代码,但我不知道如何使用该数据创建对象并将这些对象存储到数组中 以下是我到目前为止在main方法中使用的代码: public static void main(String[] args) { BufferedRead

我目前正在编写一个程序,从文本文件中读取数据,并以某种方式利用这些数据。到目前为止,我可以阅读的文件没有问题,但我有一个问题是什么下一步。当从文本文件读取数据时,必须从我已经构建的类中创建适当的对象,并根据数据存储在2个数组中。正如我之前所说,我已经完成了读取数据的代码,但我不知道如何使用该数据创建对象并将这些对象存储到数组中

以下是我到目前为止在main方法中使用的代码:

public static void main(String[] args) {
        BufferedReader inputStream = null;

        String fileLine;
        try {
            inputStream = new BufferedReader(new FileReader("EmployeeData.txt"));

            System.out.println("Employee Data:");
            // Read one Line using BufferedReader
            while ((fileLine = inputStream.readLine()) != null) {
                System.out.println(fileLine);
            }//end while
        } catch (IOException io) {
            System.out.println("File IO exception" + io.getMessage());
        }finally {
            // Need another catch for closing 
            // the streams          
            try {               
                if (inputStream != null) {
                inputStream.close();
            }                
            } catch (IOException io) {
                System.out.println("Issue closing the Files" + io.getMessage());
            }//end catch
        }//end finally
    }//end main method

您必须考虑如何在文本文件中表示数据,并将它们相应地映射到
Employee

例如,如果
Employee
类如下所示-

class Employee {
   String firstName;
   String lastName;

}
文件中的行如下所示:

first1 last1
first2 last2
您可以创建
Employee
arrayList
来保存数据-

List<Employee> employees = new ArrayList();

基本上,你必须考虑源(文本文件)中数据的结构,并找出如何解析它们并构造所需的对象。

< P>你必须考虑文本文件中的数据是如何表示的,并将它们映射到<代码>雇员类。

例如,如果
Employee
类如下所示-

class Employee {
   String firstName;
   String lastName;

}
文件中的行如下所示:

first1 last1
first2 last2
您可以创建
Employee
arrayList
来保存数据-

List<Employee> employees = new ArrayList();

基本上,必须考虑源(文本文件)中数据的结构。在
while
循环中,您可以创建对象并将其添加到数组中。创建对象并将其添加到数组中的困难到底是什么?在
while
循环中,您可以创建对象并将其添加到数组中数组。在创建对象并将其添加到数组中时,您面临的困难到底是什么?