从文件中读取数组列表并显示在屏幕上的函数 [英] Function to read an array list from file and display on the screen

查看:28
本文介绍了从文件中读取数组列表并显示在屏幕上的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一种方法来从文件中读取数组列表,然后将其显示在屏幕上,但最终不起作用.这是我正在使用的方法:

I'm trying to write a method to read an array list from a file and then display it on the screen but it doesn't work at the end. Here is the method I'm using:

说明:

  • Point 类包含构造函数、getter、setter、toString()
  • PrintPoint 类包含构造函数、打印方法和读取文件方法
  • 要测试的TestPoint类

点类

public class Point {
    
    private double x;
    private double y;
    private String name;
    
    public Point(String name, double x, double y){
        this.name = name;
        this.x = x;
        this.y = y;
    }
    
    //setter and getter
    
    @Override
    public String toString(){
        return this.name + "[" + this.x + ", " + this.y + "]";
    }
}

PrintPoint 类

PrintPoint class

import java.util.*;
import java.io.*;
public class PrintPoint {
    
    private ArrayList<Point> pointList;
    
    //Constructor
    public PrintPoint(String path) throws FileNotFoundException{
      pointList = getPointFromFile(path);
    }
    
    //Print method
    public void printPointList(){
      for(Point p : pointList){
        System.out.println(p);
      }
    }

    //Read from file method
    public ArrayList<Point> getPointFromFile(String path) throws FileNotFoundException{
        try {
            Scanner myReader = new Scanner(new File(path));
            ArrayList<String> list = new ArrayList<String>();
            while (myReader.hasNextLine()) {
                list.add(myReader.nextLine());
            }
            myReader.close();
        } catch (FileNotFoundException e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
        return new ArrayList<Point>();
    }
}

TestPoint 类:

TestPoint class:

import java.util.*;
import java.io.*;
public class TestPoint {
    public static void main(String[] args) throws FileNotFoundException{
        PrintPoint a = new PrintPoint("arraylist.txt");
        a.printPointList();
    }
}

arraylist.txt

arraylist.txt

A,3,4
B,5,6
C,7,8

我想打印数组列表但它不起作用(没有错误只是不打印).

I want to print the array list but it doesn't work (no error just not print).

更新:我已经用另一个要求更新了这个问题.

UPDATED: I have updated the question with another requirement.

推荐答案

您需要将文件 arraylist.txt 中的文本行转换为 Point 对象.

You need to convert lines of text in file arraylist.txt to Point objects.

下面是 PrintPoint 类的代码,因为这是我唯一改变的类.

Below is the code for class PrintPoint since this was the only class that I changed.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class PrintPoint {
    private ArrayList<Point> pointList;

    // Constructor
    public PrintPoint(String path) throws FileNotFoundException {
        pointList = getPointFromFile(path);
    }

    // Print method
    public void printPointList() {
        for (Point p : pointList) {
            System.out.println(p);
        }
    }

    // Read from file method
    public ArrayList<Point> getPointFromFile(String path) throws FileNotFoundException {
        ArrayList<Point> list = new ArrayList<Point>();
        try (Scanner myReader = new Scanner(new File(path))) {
            while (myReader.hasNextLine()) {
                String line = myReader.nextLine();
                String[] fields = line.split(",");
                String name = fields[0];
                double x = Double.parseDouble(fields[1]);
                double y = Double.parseDouble(fields[2]);
                Point pt = new Point(name, x, y);
                list.add(pt);
            }
        }
        return list;
    }
}

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