如何从文件输入中读取识别 class 和字段?
How to read recognizing class and fields from file input?
我有一个如下所示的文件:
7
CAT Tabby Jane F 2 7.0 true false
DOG Sam Jeff M 7 32.1 true
CAT Elsie Kate F 9 9.8 true true
DOG Spot Dick M 6 63.4 false
BIRD Tweety Dick M 3 0.06 false
BIRD Opus Berkeley M 31 10.3 true
DOG Spot John M 3 42.6 true
我的文件的第一列代表 classes,接下来的几列代表 variable/field 各自的值 classes.I 想要以某种方式读取这些行,以便我的 reader 识别第一列是 class,第二列是名称变量,第三列是所有者变量,第四列是名称(宠物)的性别等等。
以下是变量(以防万一你需要强大的想法):
protected String name;
protected String owner;
protected String sex = "F";
protected int age = 3;
protected double weight = 5.0;
protected boolean spayedNeutered;
private boolean declawed;(which is a subclass filed)
因此,当我读取文件时,按以下方式输入:
public void readData() throws Exception{
BufferedReader filename = new BufferedReader(new FileReader("pets.txt"));
String str;
List<String> list = new ArrayList<String>();
while((str = filename.readLine()) != null){
String[] temp = str.split("\s+",2);
list.add(temp[1]);
}
pets = list.toArray(new String[6]);
for (String x: pets) {
System.out.println(x);
}
}
这只会打印来自 file.I 的纯数据不知道如何告诉 reader 识别每个特定列代表 class 和各自(按顺序)变量values.After我认识的字段和class,我会把它们放在一个数组里,像Pet[] pets(Pet is my parent/super class)。然后我会应用自定义toString 方法以某种方式打印对象数组,如下所示:
> java AnimalHospital pets.data
Tabby owned by Jane: Small Cat, F, Age 2, 7.0 lbs. (not declawed)
Sam owned by Jeff: Medium Dog, M, Age 7, 32.1 lbs.
Elsie owned by Kate: Medium Cat, F, Age 9, 9.8 lbs.
Spot owned by Dick: Large Dog, M, Age 6, 63.4 lbs. (not spayed/neutered)
Tweety owned by Dick: Small Bird, M, Age 3, 0.06 lbs. (not clipped)
Opus owned by Berkeley: Large Bird, M, Age 31, 10.3 lbs.
Spot owned by John: Medium Dog, M, Age 3, 42.6 lbs.
我希望我的问题有足够的信息来澄清我的核心问题。
我从你的问题中了解到,你必须基本上对 class-name 和值进行映射。您可以为此使用地图。
因为你知道第一列是宠物的class,第二列是名字,第三列是性别等,所以在根据“”拆分整行之后,你可以使用 Map m 这样:
m.put("class",temp[0]);
m.put("name",temp[1]);
等等...
并且 return 类型将是地图。
希望对您有所帮助。
如果你想避免反射,假设你事先知道你所有的 类 并且你使用 java8,你可以这样写一个 switch 语句:
Animal animal;
switch (tmp[0]) {
case "CAT":
animal = new Cat();
break;
case "DOG":
animal = new Dog();
break;
// etc etc
default:
throw new RuntimeException("Unknown animal:" + tmp[0]);
}
您不能使用 BufferedReader.BufferedReader 来做到这一点,因为 functionality.You 必须解析它。
我有一个如下所示的文件:
7
CAT Tabby Jane F 2 7.0 true false
DOG Sam Jeff M 7 32.1 true
CAT Elsie Kate F 9 9.8 true true
DOG Spot Dick M 6 63.4 false
BIRD Tweety Dick M 3 0.06 false
BIRD Opus Berkeley M 31 10.3 true
DOG Spot John M 3 42.6 true
我的文件的第一列代表 classes,接下来的几列代表 variable/field 各自的值 classes.I 想要以某种方式读取这些行,以便我的 reader 识别第一列是 class,第二列是名称变量,第三列是所有者变量,第四列是名称(宠物)的性别等等。 以下是变量(以防万一你需要强大的想法):
protected String name;
protected String owner;
protected String sex = "F";
protected int age = 3;
protected double weight = 5.0;
protected boolean spayedNeutered;
private boolean declawed;(which is a subclass filed)
因此,当我读取文件时,按以下方式输入:
public void readData() throws Exception{
BufferedReader filename = new BufferedReader(new FileReader("pets.txt"));
String str;
List<String> list = new ArrayList<String>();
while((str = filename.readLine()) != null){
String[] temp = str.split("\s+",2);
list.add(temp[1]);
}
pets = list.toArray(new String[6]);
for (String x: pets) {
System.out.println(x);
}
}
这只会打印来自 file.I 的纯数据不知道如何告诉 reader 识别每个特定列代表 class 和各自(按顺序)变量values.After我认识的字段和class,我会把它们放在一个数组里,像Pet[] pets(Pet is my parent/super class)。然后我会应用自定义toString 方法以某种方式打印对象数组,如下所示:
> java AnimalHospital pets.data
Tabby owned by Jane: Small Cat, F, Age 2, 7.0 lbs. (not declawed)
Sam owned by Jeff: Medium Dog, M, Age 7, 32.1 lbs.
Elsie owned by Kate: Medium Cat, F, Age 9, 9.8 lbs.
Spot owned by Dick: Large Dog, M, Age 6, 63.4 lbs. (not spayed/neutered)
Tweety owned by Dick: Small Bird, M, Age 3, 0.06 lbs. (not clipped)
Opus owned by Berkeley: Large Bird, M, Age 31, 10.3 lbs.
Spot owned by John: Medium Dog, M, Age 3, 42.6 lbs.
我希望我的问题有足够的信息来澄清我的核心问题。
我从你的问题中了解到,你必须基本上对 class-name 和值进行映射。您可以为此使用地图。
因为你知道第一列是宠物的class,第二列是名字,第三列是性别等,所以在根据“”拆分整行之后,你可以使用 Map m 这样:
m.put("class",temp[0]);
m.put("name",temp[1]);
等等...
并且 return 类型将是地图。
希望对您有所帮助。
如果你想避免反射,假设你事先知道你所有的 类 并且你使用 java8,你可以这样写一个 switch 语句:
Animal animal;
switch (tmp[0]) {
case "CAT":
animal = new Cat();
break;
case "DOG":
animal = new Dog();
break;
// etc etc
default:
throw new RuntimeException("Unknown animal:" + tmp[0]);
}
您不能使用 BufferedReader.BufferedReader 来做到这一点,因为 functionality.You 必须解析它。