如何创建一个返回从txt.file读取的对象的方法



这是我读取文件的尝试。如果有人对如何更好地完成这项工作有任何意见,我会洗耳恭听。

public class Program {
public void readFromFile() throws FileNotFoundException {
File equipment = new File("equipment.txt");
Scanner reader = new Scanner(equipment);
while (reader.hasNextLine()){
}
}
}

这是我的第一节设备课。txt文件中的设备具有一些常见属性。我只把它用作一个抽象类,因为这是我认为我必须根据我的任务(测试考试(来做的。如果我错了,请告诉我:(

public abstract class Equipment {
private Integer id;
private String locker;
private Integer year;
public Equipment(Integer id, String locker, Integer year) {
this.id = id;
this.locker = locker;
this.year = year;
}

儿童装备类演练。钻孔在txt文件中只发生了三次,当钻孔和锯具有一些不同的属性时,我很难弄清楚如何从txt文件中读取内容。

public class Drill extends Equipment{
private String equipmentName;
private Boolean batteryPowered;
private Boolean needsNewBit;
public Drill(String equipmentName, Integer id, String locker, Integer year,  Boolean batteryPowered, Boolean needsNewBit) {
super(id, locker, year);
this.equipmentName = equipmentName;
this.batteryPowered = batteryPowered;
this.needsNewBit = needsNewBit;
}

儿童设备类锯

public class Saw extends Equipment{
private String equipmentName;
private String sawType;
private Boolean needsGrinding;
public Saw(String equipmentName, Integer id, String locker, Integer year, String sawType, Boolean needsGrinding) {
super(id, locker, year);
this.equipmentName = equipmentName;
this.sawType = sawType;
this.needsGrinding = needsGrinding;
}

Equipment.txt文件格式。我把物体分开了,这样更容易阅读。txt文件最初在对象之间没有空格。

Saw
12
Locker 1
2012
Hand
true
Saw
15
Locker 2
2012
Hack
true
Saw
14
Locker 2
2014
Japanese
true
Saw
1
Locker 3
2014
Coping
false
Saw
2
Locker 3
2014
Coping
true
Saw
12
Locker 1
2016
Japanese
false
Saw
3
Locker 1
2016
Hack
true
Saw
5
Locker 4
2016
Hand
true
Saw
6
Locker 4
2019
Hand
true
Drill
16
Locker 5
2019
false
true
Drill
17
Locker 5
2019
false
true
Drill
18
Locker 5
2001
true
false
Saw
20
Locker 6
2001
Hack
false
Saw
21
Locker 6
2001
Hack
true
Saw
31
Locker 7
2021
Japanese
false
Saw
32
Locker 7
2021
Japanese
false
Saw
33
Locker 7
2021
Japanese
true
Saw
34
Locker 7
2018
Coping
false

首先是代码
(后面的解释(

public abstract class Equipment {
private Integer id;
private String locker;
private Integer year;
private String equipmentName;
public Equipment(Integer id, String locker, Integer year, String name) {
this.id = id;
this.locker = locker;
this.year = year;
equipmentName = name;
}
public String toString() {
return String.format("%s %s %s %s", equipmentName, id, locker, year);
}
}

public class Drill extends Equipment {
private Boolean batteryPowered;
private Boolean needsNewBit;
public Drill(String equipmentName, Integer id, String locker, Integer year,  Boolean batteryPowered, Boolean needsNewBit) {
super(id, locker, year, equipmentName);
this.batteryPowered = batteryPowered;
this.needsNewBit = needsNewBit;
}
public String toString() {
return String.format("%s %s %s", super.toString(), batteryPowered, needsNewBit);
}
}

public class Saw extends Equipment {
private String sawType;
private Boolean needsGrinding;
public Saw(String equipmentName, Integer id, String locker, Integer year, String sawType, Boolean needsGrinding) {
super(id, locker, year, equipmentName);
this.sawType = sawType;
this.needsGrinding = needsGrinding;
}
public String toString() {
return String.format("%s %s %s", super.toString(), sawType, needsGrinding);
}
}

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Program {
private static final String  DRILL = "Drill";
private static final String  SAW = "Saw";
public List<Equipment> readFromFile() throws IOException {
File equipment = new File("equipment.txt");
List<Equipment> equipmentList = new ArrayList<>();
List<String> fields = new ArrayList<>();
String line;
String type = null;
Equipment equip = null;
try (Scanner reader = new Scanner(equipment)) {
while (reader.hasNextLine()) {
line = reader.nextLine();
if (isTypeLine(line)) {
if (type != null) {
switch (type) {
case DRILL:
equip = createDrill(fields);
break;
case SAW:
equip = createSaw(fields);
break;
default:
equip = null;
}
if (equip != null) {
equipmentList.add(equip);
}
}
fields.clear();
type = line;
}
else {
fields.add(line);
}
}
if (type != null) {
switch (type) {
case DRILL:
equip = createDrill(fields);
break;
case SAW:
equip = createSaw(fields);
break;
default:
equip = null;
}
if (equip != null) {
equipmentList.add(equip);
}
}
return equipmentList;
}
}
private Drill createDrill(List<String> fields) {
Integer id = Integer.valueOf(fields.get(0));
String locker = fields.get(1);
Integer year = Integer.valueOf(fields.get(2));
Boolean batteryPowered = Boolean.valueOf(fields.get(3));
Boolean needsNewBit = Boolean.valueOf(fields.get(4));
Drill drill = new Drill(DRILL, id, locker, year, batteryPowered, needsNewBit);
return drill;
}
private Saw createSaw(List<String> fields) {
Integer id = Integer.valueOf(fields.get(0));
String locker = fields.get(1);
Integer year = Integer.valueOf(fields.get(2));
String sawType = fields.get(3);
Boolean needsGrinding = Boolean.valueOf(fields.get(4));
Saw saw = new Saw(SAW, id, locker, year, sawType, needsGrinding);
return saw;
}
private boolean isTypeLine(String line) {
return DRILL.equals(line)  ||  SAW.equals(line);
}
public static void main(String[] args) {
Program prog = new Program();
try {
List<Equipment> list = prog.readFromFile();
list.forEach(System.out::println);
}
catch (IOException xIo) {
xIo.printStackTrace();
}
}
}
  • 我将成员equipmentName从子类(即DrillSaw(移到了超类Equipment,因为基类的目的之一是包含所有子类共有的成员。因此,最好在超类中有一个equipmentName成员,而不是在每个子类中都有一个单独的成员
  • 我将方法toString添加到类EquipmentDrillSaw中的每一个中,仅用于调试目的
  • 在文本文件中,一组行定义了一件设备,其中第一行是DrillSaw。因此,在代码(方法readFromFile的代码,在类Program中(中,每当一行包含DrillSaw时,我都会将其保存在局部变量type中。我也知道我们正在开始一个新的定义。这意味着要么这是第一个定义,要么我已经阅读了定义设备的所有行。在后者的情况下,即我刚刚读取了定义设备的所有行,我根据type的值创建了一个实例。我将type初始化为空。这让我知道我是否正在读取文件中的第一个定义
  • 在创建了适当的实例,即DrillSaw之后,我将其添加到列表中
  • 我使用方法readFromFile中的try with resources来确保文件equipment.txt已关闭
  • 同样在方法readFromFile中,当while循环终止时,fields中可能存在我尚未转换为DrillSaw的实例的值。因此,在while循环之后出现了if语句
  • 下面的行(来自类Program中的方法main(被称为方法引用。它只为我节省了一些输入,并且只显示方法readFromFile返回的值,以检查我是否得到了预期的结果
list.forEach(System.out::println);

文件equipment.txt的内容

Saw
12
Locker 1
2012
Hand
true
Saw
15
Locker 2
2012
Hack
true
Saw
14
Locker 2
2014
Japanese
true
Saw
1
Locker 3
2014
Coping
false
Saw
2
Locker 3
2014
Coping
true
Saw
12
Locker 1
2016
Japanese
false
Saw
3
Locker 1
2016
Hack
true
Saw
5
Locker 4
2016
Hand
true
Saw
6
Locker 4
2019
Hand
true
Drill
16
Locker 5
2019
false
true
Drill
17
Locker 5
2019
false
true
Drill
18
Locker 5
2001
true
false
Saw
20
Locker 6
2001
Hack
false
Saw
21
Locker 6
2001
Hack
true
Saw
31
Locker 7
2021
Japanese
false
Saw
32
Locker 7
2021
Japanese
false
Saw
33
Locker 7
2021
Japanese
true
Saw
34
Locker 7
2018
Coping
false

运行Program的输出

Saw 12 Locker 1 2012 Hand true
Saw 15 Locker 2 2012 Hack true
Saw 14 Locker 2 2014 Japanese true
Saw 1 Locker 3 2014 Coping false
Saw 2 Locker 3 2014 Coping true
Saw 12 Locker 1 2016 Japanese false
Saw 3 Locker 1 2016 Hack true
Saw 5 Locker 4 2016 Hand true
Saw 6 Locker 4 2019 Hand true
Drill 16 Locker 5 2019 false true
Drill 17 Locker 5 2019 false true
Drill 18 Locker 5 2001 true false
Saw 20 Locker 6 2001 Hack false
Saw 21 Locker 6 2001 Hack true
Saw 31 Locker 7 2021 Japanese false
Saw 32 Locker 7 2021 Japanese false
Saw 33 Locker 7 2021 Japanese true
Saw 34 Locker 7 2018 Coping false

最新更新