既上一博发布的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;
} }

最新文章

  1. myeclipse中的weblogic 服务未正常关闭问题解决。
  2. Mac 使用 SSH 免密连接服务器
  3. jbuilder的set!方法重构接口
  4. Mysql 分区
  5. poj3292-Semi-prime H-numbers(筛法打表)
  6. [Cocos2d-x For WP8]点击移动精灵
  7. NeHe OpenGL教程 第七课:光照和键盘
  8. Python之路【第六篇】:socket
  9. Spring MVC Controller与jquery ajax请求处理json
  10. List subList()的一个demo
  11. Windows环境下多线程编程原理与应用读书笔记(4)————线程间通信概述
  12. Oracle 11gR2 用exp无法导出空表解决方法
  13. [WinForm]最小化到系统托盘,右键退出
  14. Qt中绘制五子棋棋盘
  15. 织梦CMS增加复制文档功能
  16. 前端部署ant+yuicompressor文件压缩+获取版本+SSH公布(部分代码)
  17. ffmypeg 视频处理类库使用方法
  18. tmux复制模式
  19. 使用Let&#39;s Encrypt加密你的小站
  20. djaogo 图片上传与读取

热门文章

  1. c# word文档与二进制数据的相互转换
  2. [转载] ETL和Kettle
  3. windows下忘记mysql的root密码解决方法(图文)
  4. Unity3D 热更新方案(集合各位专家的汇总)
  5. 2718:晶晶赴约会-poj
  6. 基于iframe的移动端嵌套
  7. Docker理解
  8. 深入理解java虚拟机_第三章(上)-----&gt;垃圾收集器与内存分配策略
  9. iOS中常见的锁
  10. Asp.Net下,基于Jquery的Ajax二级联动