如何在java中读取txt文件并将其存储在具有数组的对象中?

如何在java中读取txt文件并将其存储在具有数组的对象中?,java,android,arrays,Java,Android,Arrays,我有一个包含15行的文本文件,其中包含3台不同的计算机及其制造商、型号、处理器、Ram和价格。 我有以下代码通过URL读取文本文件: URL file_url = new URL(urlfile); Scanner fsc = new Scanner(file_url.openStream()); //Computer object to store each computer manufacturer, model, processor, Ram and price

我有一个包含15行的文本文件,其中包含3台不同的计算机及其制造商、型号、处理器、Ram和价格。 我有以下代码通过URL读取文本文件:

    URL file_url = new URL(urlfile);
    Scanner fsc = new Scanner(file_url.openStream());

    //Computer object to store each computer manufacturer, model, processor, Ram and price
    Computer pc1 = new Computer();
    Computer pc2 = new Computer();
    Computer pc3 = new Computer();

    //Created the array called arrayCom
    Computer [] arrayCom = new Computer[15];
    int counter = 0;

    //I am stuck here. Need to stored them in an array 
    while (fsc.hasNext())
    {

    }
以下是计算机类,包括制造商、型号、处理器、Ram和价格以及setter和getter:

public class Computer {
private String manufacturer;
private String model;
private String processor;
private int ram;
private double price;

public Computer(){

}
public Computer(String manufacturer, String model, String processor, int ram, double price){
    this.manufacturer = manufacturer;
    this.model = model;
    this.processor = processor;
    this.ram = ram;
    this.price = price;
}
public String getManufacturer(){
    return manufacturer;
}
public void setManufacturer(){
    this.manufacturer = manufacturer;
}
public String getModel(){
    return model;
}
public void setModel(){
    this.model = model;
}
public String getProcessor(){
    return processor;
}
public void setProcessor(){
    this.processor = processor;
}
public int getRam(){
    return ram;
}
public void setRam(){
    this.ram = ram;
}
public double getPrice(){
    return price;
}
public void setPrice(){
    this.price = price;
}
我的问题是如何将它们存储在我在数组中创建的对象PC1、PC2和PC3中,并获得它们的平均价格和PC1、PC2、PC3的RAM平均值?有什么建议吗?
谢谢

最好使用将文件读取为字符串的方法创建额外的类。例子: Java源文件。。。TextReader.java

import java.io.File;
import java.nio.file.Files;


public class TextReader{

  //Get data from file
  public static String getDataFromFile(String file){ 
     String data="";
     try{
         File f=new File(file);
         data=new String(Files.readAllBytes(f.toPath()));
     }catch(Exception e){e.printStackTrace();}
   return data;
  }

}
例如,在方法“getModel()”中,可以执行以下操作:

   public String getModel(){
      return TextReader.getDataFromFile("modeldata.txt");
   }
注意:此方法将整个文件文本读入字符串,假设每个数据段都在特定的文件中,否则,如果所有数据都在单个文件中,则需要知道包含特定数据的每一行,然后使用“readAllLines()”。

可能重复的