学生兴趣特长信息管理系统(数据结构课设)
考查点:数据结构、文件操作
注意:必须使用文件存储数据,不得使用数据库管理系统。
任务:通过此系统可以实现如下功能(包含但不限于以下功能,可自行设计):
1、兴趣类别包括:球类、棋类、田径、文化、舞蹈……
兴趣类别是可以添加、删除、修改的。
2、每一项兴趣属于一种类别。
例如球类包括:足球、篮球、乒乓球、羽毛球……
兴趣也是可以添加、删除、修改的。
3、学生信息包括:学号、姓名、性别、年级、专业……
学生信息是可以添加、删除、修改的。
4、每个学生可以有多项兴趣特长。
每个学生的兴趣是可以添加、删除的。
5、定义好相应的数据结构,将文件的信息读取至内存。
在内存中进行兴趣类别、兴趣、学生、学生兴趣的更新操作,并将更新后的数据写回文件,可以设置专门的功能或在退出系统时批量更新。
文件格式自行定义,可以定义多个文件将不同的信息分别存放。文件中已有部分信息。
6、插入、删除、修改、查询
l 添加一个新的兴趣类别
l 修改、删除兴趣类别
l 添加一个新的兴趣项,注意每一个兴趣项必须属于一种兴趣类别
l 修改、删除兴趣项
l 添加一个新的学生
l 修改、删除学生信息
l 给学生添加兴趣,可以全选(例如喜欢所有的球类),可以挑选。
l 删除学生的兴趣
l 根据用户输入的关键词查询某学生的基本信息和该学生的兴趣
l 根据用户输入的关键词查询某兴趣的所有爱好者信息,例如查询所有喜欢足球的学生信息。
7、将更新后的数据写回文件。
8、为了便于查询,可以增加索引文件,参考绪论中的图书检索问题。
注;本人使用java编写
源码:
import java.util.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
public class do_file {
//文本写入文件
public static void write(String filePathAndName,String m) {
try {
File myFilePath = new File(filePathAndName);
// if (!myFilePath.exists()) {
// myFilePath.createNewFile();
//}
//FileWriter resultFile = new FileWriter(myFilePath);
FileWriter resultFile = new FileWriter(myFilePath,true);// 表示在原文件的基础上添加内容
PrintWriter myFile = new PrintWriter(resultFile);
myFile.println(m);
//将内容写入文件
myFile.close(); resultFile.close();
// System.out.println("文件写入操作成功!");
} catch (IOException e) {
System.out.println("新建文件操作出错!");
e.printStackTrace();
}
}
//新建文件
public static void newFile(String filePathAndName, String fileContent) {
try {
File myFilePath = new File(filePathAndName);
if (!myFilePath.exists()) {
myFilePath.createNewFile();
}
FileWriter resultFile = new FileWriter(myFilePath);
// FileWriter resultFile = new FileWriter(myFilePath,true); 表示在原文件的基础上添加内容
PrintWriter myFile = new PrintWriter(resultFile);
myFile.println(fileContent);
//将内容写入文件
myFile.close(); resultFile.close();
// System.out.println("新建文件操作成功!");
} catch (IOException e) {
System.out.println("新建文件操作出错!");
e.printStackTrace();
}
}
/*
//读整个文件
public static void visit() {
try{
FileInputStream fis = new FileInputStream("E:\\数据结构课设\\Student.txt"); //定义一个文件字节流
InputStreamReader isr = new InputStreamReader(fis); //将字节流转换成字符流
BufferedReader br = new BufferedReader(isr); //将字符流转换成缓存流
String s=""; String ss ="";
while((s = br.readLine())!= null) { //一次读取一行
ss += s+"\n";
}
br.close(); isr.close(); fis.close();
System.out.print(ss);
}
catch(IOException e){
e.printStackTrace();
}
}
*/
//部分查找
public static void Find(String id) {
try{
FileInputStream fis = new FileInputStream("E:\\数据结构课设\\Student.txt"); //定义一个文件字节流
InputStreamReader isr = new InputStreamReader(fis); //将字节流转换成字符流
BufferedReader br = new BufferedReader(isr); //将字符流转换成缓存流
String s=""; String ss ="";
int i=0;
s = br.readLine();
while((s = br.readLine())!= null) { //一次读取一行
if(s.equals(id)) {
while(i<4) {
s = br.readLine();
ss += s+"\n";
i++;
}
break;
}
}
br.close(); isr.close(); fis.close();
System.out.print(ss);
}
catch(IOException e){
e.printStackTrace();
}
}
//对文件指定内容的修改
public static boolean fileChange(String filePathandName, String oldstr, String newstr) {
RandomAccessFile raf = null;
try {
raf = new RandomAccessFile(filePathandName, "rw");//“rw”对文件进行读和写
String line = null;
long lastPoint = 0; //记住上一次的偏移量
while ((line = raf.readLine()) != null) {
final long ponit = raf.getFilePointer();
if(line.contains(oldstr)){ //contains()包含的意思,line字符串里是否包含oldstr,返回值为Boolean型
String str=line.replace(oldstr, newstr);
raf.seek(lastPoint);
raf.writeBytes(str);
}
lastPoint = ponit;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
raf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
public static boolean Change(String id, String oldstr, String newstr) {
RandomAccessFile raf = null;
try {
raf = new RandomAccessFile("E:\\数据结构课设\\Student.txt", "rw");//“rw”对文件进行读和写
String line = null;
int i=0;
long lastPoint = 0; //记住上一次的偏移量
while ((line = raf.readLine()) != null&&i<5) {
if(line.equals(id)) {
while ((line = raf.readLine()) != null&&i<5) {
final long ponit = raf.getFilePointer();
if(line.contains(oldstr)){
String str=line.replace(oldstr, newstr);
raf.seek(lastPoint);
raf.writeBytes(str);
}
lastPoint = ponit;
i++;
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
raf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
public static boolean Change1(String id, String oldstr, String newstr) { //ok
RandomAccessFile raf = null;
try {
raf = new RandomAccessFile("E:\\数据结构课设\\Interest.txt", "rw");//“rw”对文件进行读和写
String line = null;
int i=0;
long lastPoint = 0; //记住上一次的偏移量
while ((line = raf.readLine()) != null&&i<1) {
if(line.equals(id)) {
while ((line = raf.readLine()) != null&&i<1) {
final long ponit = raf.getFilePointer();
if(line.contains(oldstr)){
String str=line.replace(oldstr, newstr);
raf.seek(lastPoint);
raf.writeBytes(str);
}
lastPoint = ponit;
i++;
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
raf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
public static void delFile(String filepathandname){
File file=new File(filepathandname);
if(file.exists()&&file.isFile())
file.delete();
}
public static void changeStudent(String id,String oldstr,String newstr) {
try {
File file = new File("E:\\数据结构课设\\Student.txt");
InputStream is = new FileInputStream(file);
BufferedReader reader = new BufferedReader(
new InputStreamReader(is));
String filename = file.getName();
// tmpfile为缓存文件,代码运行完毕后此文件将重命名为源文件名字。
File tmpfile = new File(file.getParentFile().getAbsolutePath()
+ "\\" + filename + ".tmp");
BufferedWriter writer = new BufferedWriter(new FileWriter(tmpfile));
boolean flag = false;
String str = null;
while (true) {
str = reader.readLine();
if (str == null)
break;
else if(str.contains(id)) {
writer.write(str + "\n");
while((str=reader.readLine())!=null)
if (str.contains(oldstr)) {
String mn= str.replace(oldstr, newstr);
//str.replace(target, newContent);
writer.write(mn + "\n");
flag = true;
break;
}
else
writer.write(str + "\n");
}
else
writer.write(str + "\n");
}
is.close();
writer.flush(); //将缓存区的数据强制输出
writer.close();
if (flag) {
file.delete();
tmpfile.renameTo(new File(file.getAbsolutePath()));
} else
tmpfile.delete();
} catch (Exception e) {
e.printStackTrace();
}
}
//下面为对兴趣进行操作的函数
public static void doInterest(String id,String type,String oldstr,String newstr) {
try {
File file = new File("E:\\数据结构课设\\Interest.txt");
InputStream is = new FileInputStream(file);
BufferedReader reader = new BufferedReader(
new InputStreamReader(is));
String filename = file.getName();
// tmpfile为缓存文件,代码运行完毕后此文件将重命名为源文件名字。
File tmpfile = new File(file.getParentFile().getAbsolutePath()
+ "\\" + filename + ".tmp");
BufferedWriter writer = new BufferedWriter(new FileWriter(tmpfile));
boolean flag = false;
String str = null;
while (true) {
str = reader.readLine();
if (str == null)
break;
else if(str.contains(id)) {
writer.write(str + "\n");
while((str=reader.readLine())!=null)
if (str.contains(type)) {
String mn= str.replace(oldstr, newstr);
//str.replace(target, newContent);
writer.write(mn + "\n");
flag = true;
}
else
writer.write(str + "\n");
}
else
writer.write(str + "\n");
}
is.close();
writer.flush(); //将缓存区的数据强制输出
writer.close();
if (flag) {
file.delete();
tmpfile.renameTo(new File(file.getAbsolutePath()));
} else
tmpfile.delete();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void addInterest(String id,String type,String newstr) { //addInterest
try {
File file = new File("E:\\数据结构课设\\Interest.txt");
InputStream is = new FileInputStream(file);
BufferedReader reader = new BufferedReader(
new InputStreamReader(is));
String filename = file.getName();
// tmpfile为缓存文件,代码运行完毕后此文件将重命名为源文件名字。
File tmpfile = new File(file.getParentFile().getAbsolutePath()
+ "\\" + filename + ".tmp");
BufferedWriter writer = new BufferedWriter(new FileWriter(tmpfile));
boolean flag = false;
String str = null;
while (true) {
str = reader.readLine();
if (str == null)
break;
else if(str.contains(id)) {
writer.write(str + "\n");
while((str=reader.readLine())!=null)
if (str.contains(type)) {
String mn= str+" "+newstr;
//str.replace(target, newContent);
writer.write(mn + "\n");
flag = true;
break;
}
else
writer.write(str + "\n");
}
else
writer.write(str + "\n");
}
is.close();
writer.flush();
writer.close();
if (flag) {
file.delete();
tmpfile.renameTo(new File(file.getAbsolutePath()));
} else
tmpfile.delete();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void delInterest(String id,String type,String oldinterest) { //doInterest
try {
File file = new File("E:\\数据结构课设\\Interest.txt");
InputStream is = new FileInputStream(file);
BufferedReader reader = new BufferedReader(
new InputStreamReader(is));
String filename = file.getName();
// tmpfile为缓存文件,代码运行完毕后此文件将重命名为源文件名字。
File tmpfile = new File(file.getParentFile().getAbsolutePath()
+ "\\" + filename + ".tmp");
BufferedWriter writer = new BufferedWriter(new FileWriter(tmpfile));
boolean flag = false;
String str = null;
while (true) {
str = reader.readLine();
if (str == null)
break;
else if(str.contains(id)) {
writer.write(str + "\n");
while((str=reader.readLine())!=null)
if (str.contains(type)) {
String mid="";
String mn= str.replace(oldinterest,mid);
//str.replace(target, newContent);
writer.write(mn + "\n");
flag = true;
break;
}
else
writer.write(str + "\n");
}
else
writer.write(str + "\n");
}
is.close();
writer.flush();
writer.close();
if (flag) {
file.delete();
tmpfile.renameTo(new File(file.getAbsolutePath()));
} else
tmpfile.delete();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void Addnewitem(String id,String typeandinterest) { //doInterest
try {
File file = new File("E:\\数据结构课设\\Interest.txt");
InputStream is = new FileInputStream(file);
BufferedReader reader = new BufferedReader(
new InputStreamReader(is));
String filename = file.getName();
// tmpfile为缓存文件,代码运行完毕后此文件将重命名为源文件名字。
File tmpfile = new File(file.getParentFile().getAbsolutePath()
+ "\\" + filename + ".tmp");
BufferedWriter writer = new BufferedWriter(new FileWriter(tmpfile));
boolean flag = false;
String str = null;
while (true) {
str = reader.readLine();
if (str == null)
break;
else if(str.contains(id)) {
writer.write(str + "\n");
writer.write(typeandinterest + "\n");
flag = true;
}
else
writer.write(str + "\n");
}
is.close();
writer.flush();
writer.close();
if (flag) {
file.delete();
tmpfile.renameTo(new File(file.getAbsolutePath()));
} else
tmpfile.delete();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void delitem(String type) { //doInterest
try {
File file = new File("E:\\数据结构课设\\Interest.txt");
InputStream is = new FileInputStream(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String filename = file.getName();
// tmpfile为缓存文件,代码运行完毕后此文件将重命名为源文件名字。
File tmpfile = new File(file.getParentFile().getAbsolutePath()
+ "\\" + filename + ".tmp");
BufferedWriter writer = new BufferedWriter(new FileWriter(tmpfile));
boolean flag = false;
String str = null;
while (true) {
str = reader.readLine();
if (str == null)
break;
else if(str.contains(type)) {
String mid="";
writer.write(mid + "\n");
flag = true;
}
else
writer.write(str + "\n");
}
is.close();
writer.flush();
writer.close();
if (flag) {
file.delete();
tmpfile.renameTo(new File(file.getAbsolutePath()));
} else
tmpfile.delete();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void Findinterest(String id) {
try{
FileInputStream fis = new FileInputStream("E:\\数据结构课设\\Interest.txt"); //定义一个文件字节流
InputStreamReader isr = new InputStreamReader(fis); //将字节流转换成字符流
BufferedReader br = new BufferedReader(isr); //将字符流转换成缓存流
String s=""; String ss ="";
String mid="";
int i=0;
while((s = br.readLine())!= null) { //一次读取一行
if(s.contains(id)) {
while((s = br.readLine())!= null) {
mid=s;
if(mid.contains("2016")) {
break;
}
else
ss += s+"\n";
}
break;
}
}
br.close(); isr.close(); fis.close();
System.out.print(ss);
}
catch(IOException e){
e.printStackTrace();
}
}
public static void Search(String interest) {
try{
FileInputStream fis = new FileInputStream("E:\\数据结构课设\\Interest.txt"); //定义一个文件字节流
InputStreamReader isr = new InputStreamReader(fis); //将字节流转换成字符流
BufferedReader br = new BufferedReader(isr); //将字符流转换成缓存流
String s="";
String mid1="";
String mid2="";
int i=0;
boolean flag=false;
while((s = br.readLine())!= null) { //一次读取一行
i=0;
if(s.contains("2016")) {
mid1=s;
while((s = br.readLine())!= null&&i<2) {
mid2=s;
if(mid2.contains(interest)) {
System.out.println(mid1);
Find(mid1);
break;
}
i++;
}
}
}
br.close(); isr.close(); fis.close();
}
catch(IOException e){
e.printStackTrace();
}
}
public static void Search(String interest) {
try{
FileInputStream fis = new FileInputStream("E:\\数据结构课设\\Interest.txt"); //定义一个文件字节流
InputStreamReader isr = new InputStreamReader(fis); //将字节流转换成字符流
BufferedReader br = new BufferedReader(isr); //将字符流转换成缓存流
String s="";
String mid1="";
String mid2="";
int i=0;
boolean flag=false;
while((s = br.readLine())!= null) { //一次读取一行
i=0;
if(s.contains("2016")) {
mid1=s;
while((s = br.readLine())!= null&&i<2) {
mid2=s;
if(mid2.contains(interest)) {
System.out.println(mid1);
Find(mid1);
break;
}
i++;
}
}
}
br.close(); isr.close(); fis.close();
}
catch(IOException e){
e.printStackTrace();
}
}
public static void main(String args[]) {
while(true) {
Scanner input=new Scanner(System.in);
String varm;
//newFile("E:\\数据结构课设\\Student.txt","");
//newFile("E:\\数据结构课设\\Interest.txt","");
System.out.println("请选择功能,直接输入功能前面的序号:");
System.out.println("1------录入学生的基本信息");
System.out.println("2------录入学生的兴趣信息");
System.out.println("3------修改学生的基本信息");
System.out.println("4------修改学生的兴趣信息");
System.out.println("5------根据学号查找学生的基本信息和兴趣信息");
System.out.println("6------根据兴趣查找学生信息");
int var1=input.nextInt();
if(var1==1) {
String n="end";
String m="";
while(true) {
m=input.nextLine();
if(m.equals(n)) //此处只能用值比较,不能用==
break;
write("E:\\数据结构课设\\Student.txt",m);
}
System.out.println("学生基本信息录入成功");
}//if(var1==1)的括号
else if(var1==2) {
System.out.println("兴趣类别:球类、棋类、田径等");
System.out.println("球类:篮球、足球、羽毛球等");
System.out.println("棋类:国际象棋、围棋、跳棋等");
System.out.println("田径:跑步、跳远、跨栏等");
System.out.println("请输入学生兴趣,先输入学号,再输入兴趣,输入end结束");
String n="end";
String m="";
while(true) {
m=input.nextLine();
if(m.equals(n)) //此处只能用值比较,不能用==
break;
write("E:\\数据结构课设\\Interest.txt",m);
//System.out.println("兴趣信息录入成功!");
}
}
else if(var1==3) {
System.out.println("请选择修改类型,直接输入类型前的代号:");
System.out.println("1------修改学生信息");
System.out.println("2------删除学生信息");
System.out.println("3------查找学生信息");
int var2=input.nextInt();
if(var2==1) {
Scanner sc1=new Scanner(System.in);
Scanner sc=new Scanner(System.in);
System.out.println("请输入需要修改信息的学生学号、原来信息和正确信息");
String var3=sc1.nextLine();
String var5=sc.nextLine();
String var6=sc.nextLine();
changeStudent(var3,var5,var6);
System.out.println("学生信息修改成功!");
}
else if(var2==2) {
delFile("E:\\数据结构课设\\Student.txt");
System.out.println("学生信息删除成功!");
}
else if(var2==3) {
System.out.println("请输入学生学号:");
int var3=input.nextInt();
String var4=""+var3;
Find(var4);
}
else {
System.out.println("代号输入错误");
}
}
else if(var1==4){
System.out.println("1------修改学生兴趣");
System.out.println("2------增加学生兴趣");
System.out.println("3------删除学生兴趣");
System.out.println("4------增加新的兴趣项和相应的兴趣");
System.out.println("5------删除某一兴趣项");
System.out.println("6------查看学生信息");
Scanner sc1=new Scanner(System.in);
int var2=sc1.nextInt();
if(var2==1) {
Scanner sc3=new Scanner(System.in);
System.out.println("请输入要修改的学生的学号,兴趣类别,原来的兴趣和现在的兴趣");
String id=sc3.nextLine();
String type=sc3.nextLine();
Scanner sc2=new Scanner(System.in);
String inter1=sc2.nextLine();
String inter2=sc2.nextLine();
doInterest(id,type,inter1,inter2);
System.out.println("兴趣修改成功!");
}
else if(var2==2) {
Scanner sc3=new Scanner(System.in);
System.out.println("请输入要添加兴趣的学生的学号,兴趣类别,新的兴趣");
String id=sc3.nextLine();
String type=sc3.nextLine();
Scanner sc2=new Scanner(System.in);
String newinterest=sc2.nextLine();
addInterest(id,type,newinterest);
System.out.println("兴趣添加成功!");
}
else if(var2==3) {
Scanner sc3=new Scanner(System.in);
System.out.println("请输入要删除兴趣的学生的学号,兴趣类别,要删除的兴趣");
String id=sc3.nextLine();
String type=sc3.nextLine();
Scanner sc2=new Scanner(System.in);
String oldinterest=sc2.nextLine();
delInterest(id,type,oldinterest);
System.out.println("兴趣删除成功!");
}
else if(var2==4) {
Scanner sc3=new Scanner(System.in);
System.out.println("请输入要添加新兴趣项的学生的学号,新的兴趣项和兴趣");
String id=sc3.nextLine();
String typeandinterest=sc3.nextLine();
Addnewitem(id,typeandinterest);
System.out.println("新兴趣项及兴趣添加成功!");
}
else if(var2==5) {
Scanner sc3=new Scanner(System.in);
System.out.println("请输入要删除的兴趣项");
String type=sc3.nextLine();
delitem(type);
System.out.println("兴趣项删除成功!");
}
else if(var2==6) {
Scanner sc3=new Scanner(System.in);
System.out.println("请输入要查询的学生学号");
String id=sc3.nextLine();
Findinterest(id);
}
else {
System.out.println("代号输入错误!");
}
}
else if(var1==5) {
System.out.println("请输入要查找的学生的学号");
Scanner sc2=new Scanner(System.in);
String id=sc2.nextLine();
System.out.println("基本信息:");
Find(id);
System.out.println("兴趣:");
Findinterest(id);
}
else if(var1==6) {
System.out.println("请输入兴趣:");
Scanner sc2=new Scanner(System.in);
String interest=sc2.nextLine();
System.out.println("喜欢该兴趣的学生的基本信息:");
Search(interest);
}
else {
System.out.println("代号输入错误!");
}
System.out.println("你是否要继续使用其他功能");
System.out.println("Y/N");
Scanner sc2=new Scanner(System.in);
String mid=sc2.nextLine();
if(mid.equals("N")||mid.equals("n"))
break;
}
}//mian函数括号
}//public类括号
使用方法:
输入时按行输入,详细见截图,输入请看截图,不然容易出错
信息录入完以后对其进行功能操作后,存回文件的是纯文本,所以用记事本打开没有换行,换用word打开就可以看到有换行,既正常格式,记得用word打开后一定要在下次功能操作之前关闭,不然功能不能正确完成,原因是word占用了相关进程。还有就是记得把文件先建好,你可以自己建,也可以用我的main函数里的代码建(//newFile("E:\\数据结构课设\\Student.txt","");
//newFile("E:\\数据结构课设\\Interest.txt","");),建好之后记得注释掉,不然每次运行都会新建,抹掉之前的一切。文件路径自己随便。
以下为具体截图: