请高手帮忙实现一个IO的Java程序题

10.从键盘输入多个同学的基本信息如zhangsan|90|89|60lisi|56|56|60wangwu|45|89|60分别为姓名,数学成绩,外语成绩。根据这些信息... 10. 从键盘输入多个同学的基本信息 如
zhangsan|90|89|60
lisi|56|56|60
wangwu|45|89|60 分别为姓名,数学成绩,外语成绩。
根据这些信息写Student类
写一个Banji类,此类可以保存Student对象,可以保存的数量无限。
Banji类里提供
saveStudent()方法,将所有对象保存到data.dat里。
addStudent(String stu)方法 ,向班级添加学生。
DeleteStudent(String name) 根据名字删除学生信息
printStudents()方法,打印所有学生信息。
loadStudent()方法,所有的学生的信息从data.dat中读到。
这个程序在从键盘输入的时候以及建立Student对象的时候是要求通过循环写入和生成的,因此可以输入多个字符串并相应建立多个对象。而在向DATA。DAT文件中写入和读出数据的时候是通过对象流完成的。
有一个格式和思路可以参考(这个程序是有错误的,并且不完全一样)
import java.io.*;
import java.util.*;
public class StudentTest
{public static void main(String args[]) throws IOException {
System.out.println("请输入学生信息");
Scanner get = new Scanner(System.in);
String e = "";
e = get.nextLine();
List<Student> students = new ArrayList<Student>();
while(e.length()!=0)
{Student ee = new Student(e);
students.add(ee);
e=get.nextLine();}
System.out.println("以下信息是手工输入得到的学生信息");
System.out.println("--------------------------");
System.out.println("姓名\t语文\t英语\t数学\t总分");
for (Iterator iter = students.iterator(); iter.hasNext();) {
Student student = (Student) iter.next();
System.out.println(student.getName() + "\t" + student.getChinese()
+ "\t" + student.getEnglish() + "\t" + student.getMath()
+ "\t" + student.getTotalScore());}
System.out.println("--------------------------");
/** 将学生信息写到 a.dat 文件里 要求用对象流写*/
展开
 我来答
百度网友70ae8e86ac
推荐于2016-01-08 · TA获得超过1304个赞
知道小有建树答主
回答量:815
采纳率:0%
帮助的人:1103万
展开全部
你所说的日历类取得天数每个月都是31天,我看了,
原因是我接收的月份参数是0--11,你输入11,实际
上是表示12月,输入0,表示1月。

-----------StuClass.java------------------

import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class StuClass{

protected StuClass() throws IOException, SecurityException {
super();
}

private static final String FILE_PATH = "D:\\data.dat";

private List<Student> stuList = new ArrayList<Student>();

/**
* 保存增加的学生
*
* @param stu
* @throws IOException
*/
public void saveStudent(Student stu) throws IOException {
File f = new File(FILE_PATH);
OutputStream output = new FileOutputStream(f, true);
MyObjectWriter objOut = new MyObjectWriter(output);
objOut.writeObject(stu);
objOut.close();
output.close();
}

/**
* 增加学生
*
* @param student
* @throws IOException
*/
public void addStudent(String student) throws IOException {
String temp[] = student.split(",");
if (temp.length != 4) {
System.out.println("输入格式不合法!");
return;
}
Student stu = new Student();
stu.setName(temp[0]);
try {
stu.setChinese(Double.parseDouble(temp[1]));
stu.setMath(Double.parseDouble(temp[2]));
stu.setEnglish(Double.parseDouble(temp[3]));
} catch (Exception e) {
System.out.println("输入数字不合法!");
stu = null;
}

if (stu != null) {
this.saveStudent(stu);
}

}

/**
* 删除学生
*
* @param name
* @throws IOException
* @throws ClassNotFoundException
*/
public void deleteStudent(String name) throws IOException,
ClassNotFoundException {
this.loadStudent();
Student student = null;
for (Student stu : stuList) {
if (stu.getName().equals(name)) {
student = stu;
break;
}
}
if (student == null)
return;
stuList.remove(student);

File f = new File(FILE_PATH);
OutputStream output = new FileOutputStream(f);
MyObjectWriter objOut = new MyObjectWriter(output);
for (Student stu : stuList) {
objOut.writeObject(stu);
}

objOut.close();
output.close();
}

/**
* 打印所有学生信息
*/
public void printStudents() {
for (Student st : stuList) {
st.detail();
}
}

/**
* 加载所有学生
*
* @throws IOException
* @throws ClassNotFoundException
*/
public void loadStudent() throws IOException, ClassNotFoundException {
stuList = new ArrayList<Student>();
File f = new File(FILE_PATH);
InputStream input = new FileInputStream(f);
ObjectInputStream objInput = new ObjectInputStream(input);
try {
Student obj = (Student) objInput.readObject();
while (obj != null) {
Student student = (Student) obj;
stuList.add(student);

obj = (Student) objInput.readObject();

}
} catch (EOFException e) {

}

objInput.close();
input.close();
}

public static void main(String[] args) throws IOException,
IllegalArgumentException, IllegalAccessException,
ClassNotFoundException {
StuClass stuClass = new StuClass();
while (true) {
System.out.println("-------------选择操作------------------");
System.out.println("1.增加 2.删除 3.查看所有 4.退出");
Scanner scanner = new Scanner(System.in);
int option = 0;
String sOp = scanner.nextLine();
try {
option = Integer.parseInt(sOp);
} catch (Exception e) {
System.out.println("无此选项,重新选择!");
sOp = scanner.nextLine();
option = Integer.parseInt(sOp);
}
switch (option) {
case 1: {
do {
System.out.println("-------------增加学生信息---------------");
System.out.println("格式:姓名,语文成绩,数学成绩,英语成绩");
System.out.println("-----------请输入成绩------------");
String value = scanner.nextLine();
stuClass.addStudent(value.trim());
System.out.println("是否继续添加(y/n)");
String isContinue = scanner.nextLine();
if (!isContinue.equalsIgnoreCase("y"))
break;
} while (true);
}
break;
case 2: {
do {
System.out.println("-------------删除学生信息---------------");
System.out.println("请输入学生姓名:");
String value = scanner.nextLine();
stuClass.deleteStudent(value.trim());
System.out.println("是否继续删除(y/n)");
String isContinue = scanner.nextLine();
if (!isContinue.equalsIgnoreCase("y"))
break;
} while (true);
}
break;
case 3: {
do {
System.out.println("-------------查询学生信息---------------");
stuClass.loadStudent();
stuClass.printStudents();
System.out.println("是否继续(y/n)");
String isContinue = scanner.nextLine();
if (!isContinue.equalsIgnoreCase("y"))
break;
} while (true);
}
break;
case 4:
System.exit(0);
default:
System.out.println("无此选项");
break;
}
}
}
}

/**
* 用来写入对象的类,直接用ObjectOutptStream,
* 每次都会写一个 标识头部,在读入的时候,就会引发异常。
* 此类添加功能判断,看是否是第一次写入,如果是才写文件头,
* 否则不写。
*
*/
class MyObjectWriter extends ObjectOutputStream {

public MyObjectWriter(OutputStream out) throws IOException {
super(out);
}

@Override
public void writeStreamHeader() throws IOException {
if (checkFile())
super.writeStreamHeader();
else
super.reset();
}

public boolean checkFile() {
File f = new File("D:\\data.dat");
if (f.length() > 0) {
return false;
}
return true;
}

}

-----------Student.java-----------------

import java.io.Serializable;

class Student implements Serializable {

/**
*
*/
private static final long serialVersionUID = -553149347006327008L;

// 姓名
private String name;

// 英语成绩
private double english;

// 数序成绩
private double math;

// 语文成绩
private double chinese;

public double getChinese() {
return chinese;
}

public void setChinese(double chinese) {
this.chinese = chinese;
}

public double getEnglish() {
return english;
}

public void setEnglish(double english) {
this.english = english;
}

public double getMath() {
return math;
}

public void setMath(double math) {
this.math = math;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public void detail() {
System.out.print("姓名:" + this.name + "\t");
System.out.print("语文:" + this.chinese + "\t");
System.out.print("英语:" + this.english + "\t");
System.out.println("数序:" + this.math);
}
}
elietian
2007-12-07 · TA获得超过1065个赞
知道小有建树答主
回答量:835
采纳率:0%
帮助的人:666万
展开全部
写了一会居然被别人抢先了,算了
MAIN里面的东西就参考别人的输入命令吧,我就懒得看JDK了
package src;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class StudentUtil {

private List stuList;

private File path;

public void saveStudent() throws Exception {
if (stuList == null) {
throw new Exception("没有载入学生无法保存");
}
OutputStream output = new FileOutputStream(path, true);
for (int i = 0; i < stuList.size(); i++) {
output.write(((String) stuList.get(i) + "/n").getBytes());
}
}

public void addStudent(String stu) throws Exception {
this.loadStudent();
if (stuList == null) {
stuList = new ArrayList();
}
stuList.add(stu);
}

public void DeleteStudent(String name) throws Exception {
if (stuList == null) {
throw new Exception("没有载入学生无法删除");
}
for (int i = 0; i < stuList.size(); i++) {
if (this.isSameName((String) stuList.get(i), name)) {
stuList.remove(i);
}
}
this.saveStudent();

}

public void printStudents() throws Exception {
if (stuList == null) {
throw new Exception("没有载入学生无法打印");
}
for (int i = 0; i < stuList.size(); i++) {
System.out.println(stuList.get(i));
}
}

public void loadStudent() throws Exception {
if (!path.isFile()) {
if (!path.createNewFile()) {
throw new Exception("can not build file 'data.dat' ");
}
}
stuList = this.fileReader(path.getAbsolutePath());
}

public void init() {
this.path = new File(".//data.dat");
if (stuList == null) {
stuList = new ArrayList();
}

}

public boolean isSameName(String stu, String name) {
return name.equals(stu.substring(0, stu.indexOf("|")));
}

private List fileReader(String path) throws Exception {
File fileName = new File(path);
FileReader myFileReader = null;
BufferedReader myBufferedReader = null;
List stuList = new ArrayList();
try {
myFileReader = new FileReader(fileName);
myBufferedReader = new BufferedReader(myFileReader);
String myString = null;
while ((myString = myBufferedReader.readLine()) != null) {
stuList.add(myString);
}

myFileReader.close();

} catch (Exception e) {
throw new Exception(e);
} finally {
try {
if (myFileReader != null) {
myFileReader.close();
}
} catch (Exception e) {
throw new Exception(e);
}

}

// write out the contents of this file
return stuList;

}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
StudentUtil stuutil=new StudentUtil();
stuutil.init();

}

}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
Emily122228
2007-12-07
知道答主
回答量:78
采纳率:0%
帮助的人:2.8万
展开全部
你的积分好多哦
这样的题 你都给100
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式