java函数从文件中读取数组列表并显示在屏幕上

ep6jt1vc  于 2021-07-13  发布在  Java
关注(0)|答案(2)|浏览(341)

我正在尝试编写一个方法,从文件中读取数组列表,然后在屏幕上显示它,但最后无法工作。以下是我使用的方法:
说明:
point类包含构造函数、getter、setter和tostring()
printpoint类包含构造函数、print方法和readfromfile方法
要测试的测试点类
点类

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类

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

测试点类:

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

数组列表.txt

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

我想打印数组列表,但它不工作(没有错误只是不打印)。
更新:我用另一个要求更新了问题。

yhuiod9q

yhuiod9q1#

从getpointfromfile()方法返回一个空的arraylist,因此它不会在控制台输出上打印任何内容。
您已从文件中读取数据,但未构造 Point 对象。我不知道数据是什么 arraylist.txt 是的,有一个样本给你:

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

arraylist.txt文件:

10,20
34,56
199,23

控制台输出:

[10.0, 20.0]
[34.0, 56.0]
[199.0, 23.0]
dxxyhpgq

dxxyhpgq2#

您需要转换文件中的文本行 arraylist.txtPoint 物体。
下面是类的代码 PrintPoint 因为这是我唯一换过的课。

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

注意,上面的代码使用try with资源。
如果你声明这个方法 getPointFromFile 投掷 FileNotFoundException ,则不应在方法中处理它。要么移除 throws FileNotFoundException 或删除 try-catch . 在上面的代码中,我删除了 try-catch .
下面是我运行上述代码时得到的输出。

A[3.0, 4.0]
B[5.0, 6.0]
C[7.0, 8.0]

或者,如果您至少使用Java8,则可以使用StreamsAPI。

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;

public class PrintPoint {
    private List<Point> pointList;

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

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

    public List<Point> getPointFromFile(String path) throws IOException {
        Path p = Paths.get(path);
        return Files.lines(p)
                    .map(line -> {
                        String[] fields = line.split(",");
                        return new Point(fields[0],
                                         Double.parseDouble(fields[1]),
                                         Double.parseDouble(fields[2]));
                    })
                    .collect(Collectors.toList());
    }
}

相关问题