史上最强学生管理系统之IO版
既上一博发布的ArrayList版本之后,新一版的IO版又来了,其实只是在上一个版本里面添加了IO流的内容,将存入更改的信息更新到了文件中而已,这个版本网上仍然很多,本人只是在某些方面稍加修改,因为自己刚学的时候也参考了最基础的版本,觉得在某些方面还有些不足(当然本人写的仍然还有很多不足之处啦哈哈^_^),这里也贴出自己的代码,说几年自己的劳动成功也好,说为了让更多新人作为参考也罢,大牛勿喷,不足之处欢迎大家指正。
这个版本的话个人建议还是先自己创建一个文本文件(也是本人一开始写的时候的一个不足之处,很简单的一两行代码可以搞定),否则第一次查询的时候会出现找不到异常,其实可以在findStudent(destFileName)方法体内创建一个BufferedWriter对象,其中调用的是FileWriter带两个参数的构造方法,将续写功能开启( 本人还没试过,理论上应该可以避免这个异常,欢迎有心人告知一下谢谢)
主类:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner; public class BestStuManagerIO {
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
String destFileName = "student.txt"; while (true) {// 为的是每次操作之后都能回到这个页面
System.out.println("********欢迎使用学生管理系统********");
System.out.println("请选择您要进行的操作:1.查询 2.添加 3.修改 4.删除 5.退出");
String choiceNum = scanner.nextLine();
switch (choiceNum) {
case "1":
// 查询学生信息
findStudent(destFileName);
break;
case "2":
// 添加学生信息
addStudent(destFileName);
break;
case "3":
// 修改学生信息
updateStudent(destFileName);
break;
case "4":
// 删除学生信息
deleteStudent(destFileName);
break;
case "5":
// 退出
// 因为选择退出跟选择除了1234是一样的,所以用case穿透
default:
System.out.println("谢谢您的使用,再见!");
System.exit(0);// 退出系统,同时也退出了整个while无限循环
break;
}
}
} /**
* 将集合中的数据写入到文本中
*
* @param list
* @param destFileName
* @throws IOException
*/
private static void arrayList2File(ArrayList<Student> list, String destFileName) throws IOException {
// 创建输出缓冲流对象
BufferedWriter bw = new BufferedWriter(new FileWriter(destFileName));
BufferedReader br = new BufferedReader(new FileReader(destFileName));
if (br.readLine() == null) {
// System.out.println("学号\t\t姓名\t年龄\t居住地");
bw.write("学号\t姓名\t年龄\t居住地");
bw.newLine();
bw.flush();
} for (int x = 0; x < list.size(); x++) {
Student s = list.get(x);
StringBuilder sb = new StringBuilder();
sb.append(s.getSid()).append(" ").append(s.getName()).append(" ").append(s.getAge()).append(" ")
.append(s.getAddress()); bw.write(sb.toString());
bw.newLine();
bw.flush();
} bw.close();
} /**
* 从文件中读取数据到集合中
*
* @param list
* 接受文件数据的集合
* @param srcFileName
* 文件名
* @throws IOException
*/
private static void StudentFile2Array(ArrayList<Student> list, String srcFileName) throws IOException {
// 创建输入缓冲流对象
BufferedReader br = new BufferedReader(new FileReader(srcFileName)); String blank = br.readLine();//这行代码用于吸收文本文件中的第一行,因为第一行写的是标题,不应该出现在下面的循环中
String line;
while ((line = br.readLine()) != null) {
String[] datas = line.split(" ");
Student s = new Student();
s.setSid(datas[0]);
s.setName(datas[1]);
s.setAge(datas[2]);
s.setAddress(datas[3]);
list.add(s);
} br.close();
} /**
* 这是删除学生的方法
*
* @param array
* @throws IOException
*/
public static void deleteStudent(String destFileName) throws IOException {
ArrayList<Student> array = new ArrayList<Student>();
StudentFile2Array(array, destFileName);
Scanner scanner = new Scanner(System.in);
boolean flag = true; while (flag) {
System.out.println("请输入你想删除信息学生的学号");
String id = scanner.nextLine(); // 这里需要讨论到底有没有输入的这个学号的这个学生 int index = -1;// 定义变量index为-1
for (int i = 0; i < array.size(); i++) {
Student stu = array.get(i);
if (id.equals(stu.getSid())) {// 如果输入的id跟集合中学生的id有一样的,那么把这个学生的id赋值给index
index = i;
break;
}
}
if (index != -1) {// 如果不等于-1,说明进去过上面的判断条件,也就是有输入的id的学生,所以我们修改这个学生的信息就行了
System.out.println("有你需要的学生信息,请问是否确认删除? 1.(是) 2.(否,重新选择操作) 其他.(退出)");
String choice = scanner.nextLine();
if (choice.equals("1")) {
array.remove(index);
arrayList2File(array, destFileName);
System.out.println("学生信息删除成功");
} else if (choice.equals("2")) {
return;
} else {
System.out.println("谢谢使用,再见");
System.exit(0);
}
flag = false;
} else {
System.out.println("您想删除信息的学生不存在,您可以选择:1.(重新输入) 2.(重新选择操作) 其他:(退出系统)");
String choice = scanner.nextLine();
if (choice.equals("1")) { } else if (choice.equals("2")) {
return;
} else {
System.out.println("谢谢使用,再见");
System.exit(0);
}
}
} } /**
* 这是修改集合内学生信息的方法
*
* @param array
* @throws IOException
*/
public static void updateStudent(String destFileName) throws IOException {
// 创建集合对象
ArrayList<Student> array = new ArrayList<Student>();
// 从文件中把数据读取到集合中
StudentFile2Array(array, destFileName);
Scanner scanner = new Scanner(System.in);
boolean flag = true; while (flag) {
System.out.println("请输入你想修改信息学生的学号");
String id = scanner.nextLine(); // 这里需要讨论到底有没有输入的这个学号的这个学生 int index = -1;// 定义变量index为-1
for (int i = 0; i < array.size(); i++) {
Student stu = array.get(i);
if (id.equals(stu.getSid())) {// 如果输入的id跟集合中学生的id有一样的,那么把这个学生的id赋值给index
index = i;
break;
}
}
if (index != -1) {// 如果不等于-1,说明进去过上面的判断条件,也就是有输入的id的学生,所以我们修改这个学生的信息就行了
System.out.println("有你查找的学生信息,请问需要修改哪一项:1.(全部) 2.(姓名) 3.(年龄) 4.(居住地) 5.(放弃修改,重新选择操作) 6.(退出)");
String choice = scanner.nextLine();
if (choice.equals("1")) {
Student sNew = new Student();
System.out.println("请输入学生姓名");
String name = scanner.nextLine();
System.out.println("请输入学生年龄");
String age = scanner.nextLine();
System.out.println("请输入学生居住地 ");
String address = scanner.nextLine();
sNew.setSid(id);
sNew.setName(name);
sNew.setAge(age);
sNew.setAddress(address);
array.set(index, sNew);
arrayList2File(array, destFileName);
System.out.println("学生信息全部更新成功");
} else if (choice.equals("2")) {
Student s = array.get(index);
System.out.println("请输入修改后的姓名");
String name = scanner.nextLine();
s.setName(name);
arrayList2File(array, destFileName);
System.out.println("学生姓名信息更新成功");
} else if (choice.equals("3")) {
Student s = array.get(index);
System.out.println("请输入修改后的年龄");
String age = scanner.nextLine();
s.setAge(age);
arrayList2File(array, destFileName);
System.out.println("学生年龄信息更新成功");
} else if (choice.equals("4")) {
Student s = array.get(index);
System.out.println("请输入修改后的居住地");
String address = scanner.nextLine();
s.setAddress(address);
arrayList2File(array, destFileName);
System.out.println("学生居住地信息更新成功");
} else if (choice.equals("5")) {
return;
} else {
System.out.println("谢谢使用,再见");
System.exit(0);
}
flag = false;
} else {
System.out.println("您想修改的学生不存在,您可以选择:1.(重新输入) 2.(重新选择操作) 其他:(退出系统)");
String choice = scanner.nextLine();
if (choice.equals("1")) { } else if (choice.equals("2")) {
return;
} else {
System.out.println("谢谢使用,再见");
System.exit(0);
}
}
}
} /**
* 这是查询所有学生信息的方法
*
* @param array
* 需要被查询的学生信息的集合
* @throws IOException
*/
public static void findStudent(String destFileName) throws IOException {
Scanner scanner = new Scanner(System.in);
ArrayList<Student> array = new ArrayList<Student>();
StudentFile2Array(array, destFileName);
// 如果现在没有录入学生信息
if (array.size() <= 0) {
System.out.println("现在还没有学生信息,您可以选择:1.(重新选择操作) 其他.(退出系统)");
String choice = scanner.nextLine();
if (choice.equals("1")) {
return;
} else {
System.out.println("谢谢使用,再见");
System.exit(0);
}
} System.out.println("学号\t姓名\t年龄\t居住地");
for (int i = 0; i < array.size(); i++) {
Student stu = array.get(i);
System.out.println(stu.getSid() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t" + stu.getAddress()); }
} /**
* 这是向ArrayList集合中添加学生的方法
*
* @param array
* 需要被操作的ArrayList集合
* @throws IOException
*/
public static void addStudent(String destFileName) throws IOException {
ArrayList<Student> array = new ArrayList<Student>();
StudentFile2Array(array, destFileName);
Student stu = new Student();
Scanner scanner = new Scanner(System.in);
String id;
while (true) {
boolean flag = false;// 定义flag标记学号是否冲突
System.out.println("请输入学生学号:");
id = scanner.nextLine();
for (int i = 0; i < array.size(); i++) {
if (id.equals(array.get(i).getSid())) {
System.out.println("您添加的学号已经存在,您可以选择:1.(重新输入学号) 2.(重新选择操作) 其他.(退出系统)");
String choice = scanner.nextLine();
if (choice.equals("1")) {
flag = true;
break;
} else if (choice.equals("2")) {
return;
} else {
System.out.println("谢谢使用,再见");
System.exit(0);
}
}
}
if (flag == false) {
break;
}
}
System.out.println("请输入学生姓名:");
String name = scanner.nextLine();
System.out.println("请输入学生年龄:");
String age = scanner.nextLine();
System.out.println("请输入学生居住地:");
String address = scanner.nextLine();
stu.setSid(id);
stu.setName(name);
stu.setAge(age);
stu.setAddress(address);
array.add(stu);
arrayList2File(array, destFileName);
System.out.println("添加学生信息成功");
}
}
public class Student {
private String sid;
private String name;
private String age;
private String address;
public Student() {
super();
}
public Student(String sid, String name, String age, String address) {
super();
this.sid = sid;
this.name = name;
this.age = age;
this.address = address;
}
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
史上最强学生管理系统之IO版的更多相关文章
- 史上最强学生管理系统之ArrayList版
其实不管是网上或者培训班,都会有学生管理系统的最基础版本,本人也不过是照猫画虎,在某些细节方面进行了一些渲染,使这个最基本的小程序更加人性化和便于利于操作一点,个人愚见,大牛勿喷,欢迎转载(请注明出处 ...
- Java 简化版学生管理系统(IO版)
Student management system 学生管理系统IO版 详细知识点可参考一下几篇文章 常用API Java 之ArrayList集合及应用 Java 之IO流及应用 Compreh ...
- Java学生管理系统(IO版)
图解: cade: student.java /* * 这是我的学生类 */ public class Student { //学号 private String id; //姓名 private S ...
- 史上最强Java NIO入门:担心从入门到放弃的,请读这篇!
本文原题“<NIO 入门>,作者为“Gregory M. Travis”,他是<JDK 1.4 Tutorial>等书籍的作者. 1.引言 Java NIO是Java 1.4版 ...
- JVM面试题(史上最强、持续更新、吐血推荐)
文章很长而且持续更新,建议收藏起来,慢慢读! 高并发 发烧友社群:疯狂创客圈(总入口) 奉上以下珍贵的学习资源: 疯狂创客圈 经典图书 : 极致经典 + 社群大片好评 < Java 高并发 三部 ...
- Webbrowser控件史上最强技巧全集
原文:Webbrowser控件史上最强技巧全集 Webbrowser控件史上最强技巧全集 VB调用webbrowser技巧集 1.获得浏览器信息: Private Sub Command1_Click ...
- 史上最强php生成pdf文件,html转pdf文件方法
body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...
- 史上最强maven配置详情
史上最强maven配置详情 优点 对第三方依赖库进行了统一的版本管理 统一了构建过程 统一了项目的目录结构 构建 清理 : mvn clear 编译 : mvn compile 测试 : mvn te ...
- 一文深入了解史上最强的Java堆内缓存框架Caffeine
它提供了一个近乎最佳的命中率.从性能上秒杀其他一堆进程内缓存框架,Spring5更是为了它放弃了使用多年的GuavaCache 缓存,在我们的日常开发中用的非常多,是我们应对各种性能问题支持高并发的一 ...
随机推荐
- 二:Maven中pom.xml元素详解
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6628201.html 一个pom.xml中包含了许多标签,各个标签是对项目生命周期.依赖管理的配置.常用的主 ...
- java 导出blob图片到excel
实现功能,导出当前页面显示员工的图片,核心代码已给出,仅供参考, 如需转载请注明出处http://www.cnblogs.com/wangjianguang/p/7852060.html 随便再扯2句 ...
- C语言实现快速排序法(分治法)
title: 快速排序法(quick sort) tags: 分治法(divide and conquer method) grammar_cjkRuby: true --- 算法原理 分治法的基本思 ...
- Machine Learning &&Deep Learning&&Sklearn
参考资料:https://github.com/ty4z2008/Qix/blob/master/dl.md https://morvanzhou.github.io/ 如图,先了解一下都有什么模型方 ...
- 【转】python XML 操作总结(创建、保存和删除,支持utf-8和gb2312)
原文地址:http://hi.baidu.com/tbjmnvbagkfgike/item/6743ab10af43bb24f6625cc5 最近写程序需要用到xml操作,看了看python.org上 ...
- 巧学DBhelper
这几天在教我很重要的人学习,她属于那种超级小白,很超级的那种. 教她的过程中 发现有的知识点 不管这么教都不会.DBhelper就是不知道怎么记. 当时我就想到 杰哥(程杰)的出的大话系列,和他写书的 ...
- jquery.base64.js 中文乱码处理
c# 转码:Convert.ToBase64String(Encoding.UTF8.GetBytes(str)) js 解码:$.base64.atob(this.options.valids, t ...
- c# winform treelistview的使用(treegridview)
TreeView控件显示的内容比较单一,如果需要呈现更详细信息TreeListView是一个不错的选择. 先看效果: 首先需要引用文件System.Windows.Forms.TreeListView ...
- accept 文件描述符用尽处理
if (events[i].data.fd == listenfd) { peerlen = sizeof(peeraddr); connfd = ::accept4(listenfd, (struc ...
- 数据结构与算法--Boyer-Moore和Rabin-Karp子字符串查找
数据结构与算法--Boyer-Moore和Rabin-Karp子字符串查找 Boyer-Moore字符串查找算法 注意,<算法4>上将这个版本的实现称为Broyer-Moore算法,我看了 ...