通过读取 .txt 文件并逐行保存到对象数组中来创建对象数组 [英] Creating an array of Objects from reading a .txt file and saving line by line into object array

查看:37
本文介绍了通过读取 .txt 文件并逐行保存到对象数组中来创建对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The project prompt looks like this: In Netbeans create a project named Project1, be sure to create a package, DO NOT use the default package. There will be two java files, the first most likely named Project1.java (which contains main) and the second named Record.java which contains a variable containing ONE LINE OF DATA (or record) read from the file. In your main class (not in Record.java) create an array of Record objects. When the program starts have main() call a method that reads each line of text from the file, creates a Record object containing that line and places the object in the next available slot of the array.

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

The text file I have saved is this:

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

我的问题是不了解如何逐行读取文本文件并将其存储在对象记录数组中.我可以很容易地创建一个字符串数组,但它并没有真正帮助这种情况.我不确定我是否让它变得更复杂,然后它必须如此,或者我是否完全错了.到目前为止,这是我的代码

My Issue is not understanding how to read from the text file line by line and storing it in an array of Objects Record. I can create an array of Strings easily but it does not really help the situation. I am not sure if I am making it more complicated then it has to be or if I am completely wrong. SO far this is my code

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

推荐答案

请使用此代码

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

这篇关于通过读取 .txt 文件并逐行保存到对象数组中来创建对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆