从文件中读取数组列表并显示在屏幕上的函数 [英] Function to read an array list from file and display on the screen
问题描述
我正在尝试编写一种方法来从文件中读取数组列表,然后将其显示在屏幕上,但最终不起作用.这是我正在使用的方法:
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;
}
}
- 注意上面的代码使用了try-with-resources一>.
- 如果您声明该方法
getPointFromFile
抛出FileNotFoundException
,那么您不应在该方法中处理它.删除方法声明中的throws FileNotFoundException
或删除try-catch
.在上面的代码中,我删除了try-catch
. - Note that the above code uses try-with-resources.
- If you declare that method
getPointFromFile
throwsFileNotFoundException
, then you shouldn't handle it in the method. Either remove thethrows FileNotFoundException
in the method declaration or remove thetry-catch
. In the code above, I removed thetry-catch
.
下面是我运行上面代码时得到的输出.
Below is the output I got when I ran the above code.
A[3.0, 4.0]
B[5.0, 6.0]
C[7.0, 8.0]
或者,如果您至少使用 Java 8,则可以使用 流 API.
Alternatively, if you are using at least Java 8, you can use the streams API.
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());
}
}
这篇关于从文件中读取数组列表并显示在屏幕上的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!