-2
public class Human {
private int age;
private float height;
private float weight;
private String name = new String();
public Human() {
}
public Human(String name, int age, float height, float weight) {
this.name = name;
this.age = age;
this.height = height;
this.weight = weight;
}
public Human(Human human) {
this.name = human.getName();
this.age = human.getAge();
this.height = human.getHeight();
this.weight = human.getWeight();
}
public static void writeFile(Human human, OutputStream output) throws IOException {
output.write((human.getName() + "\n").getBytes());
output.write((human.getAge() + "\n").getBytes());
output.write((human.getHeight() + "\n").getBytes());
output.write((human.getWeight() + "\n").getBytes());
}
public static Human readFile(InputStream input) throws IOException {
int temp;
String file = "";
String[] fileArray;
file += (char) input.read();
while ((temp = input.read()) != -1) {
file += (char) temp;
}
fileArray = file.split("\n", 0);
Human madeHuman = new Human(fileArray[0], Integer.parseInt(fileArray[1]), Float.parseFloat(fileArray[2]), Float.parseFloat(fileArray[3]));
return madeHuman;
}
public static void writeArrayListInFile(ArrayList<Human> humans, OutputStream output) throws IOException {
for (int i = 0; i < humans.size(); i++) {
output.write((humans.get(i).getName() + "\n").getBytes());
output.write((humans.get(i).getAge() + "\n").getBytes());
output.write((humans.get(i).getHeight() + "\n").getBytes());
output.write((humans.get(i).getWeight() + "\n").getBytes());
output.write('#');
}
}
public static ArrayList<Human> readArrayListFromFile(InputStream input) throws IOException {
int temp = ' ';
String file = "";
String[] fileArray1;
String[][] fileArray2 = new String[10][];
ArrayList<Human> madeHumanArrayList = new ArrayList<Human>();
file += (char) input.read();
while ((temp = input.read()) != -1) {
file += (char) temp;
}
fileArray1 = file.split("#", 0);
for (int i = 0; i < fileArray1.length; i++)
for (int j = 0; j < 4; j++)
fileArray2[i] = fileArray1[i].split("\n", 0);
for (int i = 0; i < fileArray2.length; i++)
madeHumanArrayList.add(new Human(fileArray2[i][0], Integer.parseInt(fileArray2[i][1]), Float.parseFloat(fileArray2[i][2]), Float.parseFloat(fileArray2[i][3])));//null pointer exception
return madeHumanArrayList;
}
虽然我有我的初始化数组列表,它给我空指针异常。我是java中的一个笨蛋,所以我会感谢任何简单的帮助。
对于重复的句子感到抱歉:$ –
您的例外是哪一行? –
请在提交之前验证您发布的内容,以避免上述重复杂乱。 – jags