通过读取.txt文件并逐行保存到对象数组中来创建对象数组

时间:2019-02-20 05:03:19

标签: java arrays text-files

项目提示符如下:在Netbeans中创建一个名为Project1的项目,请确保创建一个程序包,请勿使用默认程序包。将有两个Java文件,第一个最可能命名为Project1.java(包含main),第二个命名为Record.java,其中包含一个变量,该变量包含从文件读取的一行数据(或记录)。 在您的主类中(不在Record.java中)创建一个Record对象数组。程序启动时,有main()调用一个方法,该方法从文件中读取每一行文本,创建一个包含该行的Record对象,并将该对象放置在数组的下一个可用插槽中。

我保存的文本文件是这样:

John, Doe, jd@yahoo.com, 123456, green, 19.24
Mary, Jane, maryj@gmail.com, 7654321, blue, 27.54
Curly, Howard, nyuknyuk@msn.com, 888765, purple, 0.0
Bart, Simpson, donthaveacow@hotmail.com, 457673, magenta, 432.23
Clark, Kent, superdude@dailyplanet.com, 976834, red, 11.10

“我的问题”不了解如何逐行读取文本文件并将其存储在Objects Record数组中。我可以很容易地创建一个字符串数组,但这并不能真正解决问题。我不确定是否要使其变得更复杂,是否必须如此或者我是否完全错误。  到目前为止,这是我的代码

project1:
import java.io.*;
import java.util.Scanner;
public class Project1 {
protected static int arrayLength=5 ;
protected static String filename ="data.txt";

   public static void main(String[] args)throws Exception{
     Record [] objects = new Record[arrayLength];
     ObjectInputStream in = new ObjectInputStream(new FileInputStream(filename));
     Record obj = (Record) in.readObject();
     System.out.print(obj.getRecord());
   }

   public static void arrayObjects()throws IOException, ClassNotFoundException{
     Record [] objects = new Record[arrayLength];
     ObjectInputStream in = new ObjectInputStream(new FileInputStream(filename));
     Record obj = (Record) in.readObject();



      }
       }
package Project1;

import java.io.*;
import java.util.Scanner;
import java.nio.file.*;; 



    /**
     *
     * @author lexif
     */
    public class Record{
     private String path;
     private String data;
     private int number =1;
    public Record(String file_path) throws IOException{
        path = file_path;

      }
    public String getRecord() throws FileNotFoundException, IOException{
        FileReader open = new FileReader(path); 
        BufferedReader reader = new BufferedReader(open);
        String data1 = reader.readLine();
        return data1;
    }

    public String OpenFile(String filename,int num) throws IOException {
        FileReader fr = new FileReader (filename);
        BufferedReader textreader = new BufferedReader(fr);
        for (int i=0; i < num; i++) {
        data = textreader.readLine();
        }
        textreader.close();
        return data;
        }

     public int readLines () throws IOException{
           FileReader file_to_read = new FileReader(path);
           BufferedReader br = new BufferedReader(file_to_read);

           String aline;
           int numLines=0;

           while((aline = br.readLine()) != null){
               numLines++;
           }
           br.close();
           return numLines;
       }
    }

2 个答案:

答案 0 :(得分:0)

首先,您应该在Project类中具有readLines()函数。然后 您所需要做的就是修改readLines()函数以创建RecordObject并返回RecordObjects列表。像这样

public List<Record> readLines () throws IOException{
    FileReader file_to_read = new FileReader(path);
    BufferedReader br = new BufferedReader(file_to_read);

    String aline;
    int numLines=0;
    List<Record> recordList = new ArrayList<Record>();
    while((aline = br.readLine()) != null){

        Record rec = new Record(); // You can have a constructor which takes string as parameter also
        rec.setData(aline);
        recordList.add(rec);
        numLines++;
    }
    br.close();
    return recordList;
}

您在记录类中不需要OpenFile函数,并且getRecord应该仅返回'data'成员

答案 1 :(得分:0)

请使用此代码

   public Record[] readRecord(String pathFile) throws IOException {
        List<String> list = Files.readAllLines(Paths.get(pathFile));
        Record[] records = new Record[list.size()];
        for(int i = 0; i< list.size(); i++) {
            records[i] = new Record(list.get(i));
        }
        return records;
    }