用JAVA实现对txt文件文本增删改查

如题,需要一个对学生信息管理系统的增删改查源代码,自定义类不少于三个,主功能点不少于三个,注释有无均可。谢谢各位大神了... 如题,需要一个对 学生信息管理系统 的增删改查源代码,自定义类不少于三个,主功能点不少于三个,注释有无均可。谢谢各位大神了 展开
 我来答
匿名用户
2013-08-20
展开全部
代码就是一种编译器认可的语言,通过在编译器里写代码,编译器就可以把代码翻译成机器码,也就是二进制码,二进制码是由0和1组成的,如:1的二进制码可以表示为:0001,3表示为:0011,二进制码机器(电脑)可以直接识别,代码则不行,需要翻译成机器码。
匿名用户
2013-08-20
展开全部
用XML或者properties很容易实现增删改查
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
推荐于2018-04-10
展开全部
正好今天写了个~原来是两个类,好吧,加一个...= =~囧
以文件的形式存储的

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class StudentManager {
private static File DB = new File("students.db");
private static Set<Student> studentList = null;

@SuppressWarnings("unchecked")
private static void initStudentList() {
if (DB.exists()) {
FileInputStream fi = null;
ObjectInputStream oi = null;
try {
fi = new FileInputStream(DB);
oi = new ObjectInputStream(fi);
Object obj = oi.readObject();
fi.close();
oi.close();
if (obj instanceof Set)
studentList = (Set<Student>) obj;
} catch (Exception e) {
e.printStackTrace();
}
}
if (studentList == null)
studentList = new HashSet<Student>();
}

private static void save() {
if (studentList == null)
return;
if (!DB.exists())
try {
DB.createNewFile();
} catch (Exception e) {
e.printStackTrace();
}
FileOutputStream fo;
try {
fo = new FileOutputStream(DB);
ObjectOutputStream oo = new ObjectOutputStream(fo);
oo.writeObject(studentList);
fo.close();
oo.close();
} catch (Exception e) {
e.printStackTrace();
}
}

private static Student[] getStudents(int id, String name) {
HashSet<Student> rlt = new HashSet<Student>();
if (id != 0) {
for (Student s : studentList) {
if (s.id == id)
rlt.add(s);
}
} else if (name != null && !name.isEmpty()) {
for (Student s : studentList) {
if (s._name.equalsIgnoreCase(name))
rlt.add(s);
}
}
return rlt.toArray(new Student[0]);
}

static {
initStudentList();
}
static void out(Object obj){
System.out.println(obj);
}

public static void menu() {
out("Welcome to use Student Manager!\n1-search student;\t2-add student;\t3-list all students;\nothers-exit");
Scanner in = new Scanner(System.in);
Student student = null;
String input = in.nextLine();
if (input.equals("1")) {
Student[] students = new Student[0];
out("please input name/id");
String ins = in.nextLine();
if (ins.matches("\\d+"))
students = getStudents(Integer.parseInt(ins), null);
else
students = getStudents(0, ins);
if (students.length != 0) {
out(Arrays.toString(students).replaceAll(
"\\[|\\]|, ", "\n"));
if (students.length == 1)
student = students[0];
else {
out("please choose a student by id:\n");
int i = in.nextInt();
for (Student s : students) {
if (s.id == i) {
student = s;
break;
} else {
out("error");
menu();
}
}
}
out("1-set score;\t2-delete score;\t3-delete student;\tother-exit");
int input1=in.nextInt();
in.nextLine();
String course=null;
float score=0;
switch(input1){
case 1:
out("please input course");
course=in.nextLine();
out("please input score");
score = in.nextFloat();
in.nextLine();
student.setScore(course, score);
break;
case 2:
out("please input course");
course=in.nextLine();
student.removeScore(course);
break;
case 3:
student.removeFrom(studentList);
break;
}
}
else {
out("NO current record!");
}
}
if (input.equals("2")) {
out("please input id");
int id=in.nextInt();
in.nextLine();
out("please input name");
String name = in.nextLine();
out("please input class");
String cname=in.nextLine();
student=new Student(id, name, cname);
if(studentList.add(student)){
out("create success!\n");
String input2="-";
while(!input2.isEmpty()){
out("set score? \n1-yes;\t2-no");
input2=in.nextLine();
if(input2.equals("1")){
out("please input course");
String course=in.nextLine();
out("please input score");
float score = in.nextFloat();
student.setScore(course, score);
in.nextLine();
}
else break;
}
}else out("Error: id or student exists");
}
if (input.equals("3")) {
out(studentList.toString().replaceAll("\\[|\\]|, ", "\n"));
}
if(input.isEmpty())return;
save();
menu();
}

public static void main(String[] args){
menu();
}
}

class Student implements Serializable {
public String toString() {
return "id: " + id + "\tclass: " + _class + "\tname: " + _name
+ courses.toString().replaceAll("\\{|\\}|, ", "\n").replaceAll("=", ": ");
}

Student(int id, String _name, String _class) {
this.id = id;
this._class = _class;
this._name = _name;
}

private static final long serialVersionUID = 1L;
public int id = 0;

public String _name = "";
public String _class = "";
HashMap<String, Float> courses = new HashMap<String, Float>();

void setScore(String course, float score) {
courses.put(course, score);
System.out.println("set success!\n\n"+this);
}

void removeScore(String course) {
courses.remove(course);
System.out.println("delete success!\n\n"+this);
};

void removeFrom(Set<?> list) {
list.remove(this);
System.out.println("delete success!");
}

public int hashCode() {
return 31;
}

public boolean equals(Object obj) {
Student s = null;
if(obj instanceof Student){
System.out.println(1);
s=(Student)obj;
return s.id==id||(s._class.equals(_class)&&s._name.equals(_name));
}
return false;
}
}
class DaJiangYou{
DaJiangYou(){
System.out.println("I am da jiang you");
}
}
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式