继学生管理系统

最近我们学了IO流,可以将数据导入文件夹,也可以将文件夹数据导出。这样就可以对学生管理系统进行添加功能,比如:保存所有学生信息在文件夹或者从文件夹直接将个人学生信息导入学生管理系统表格。
在这里插入图片描述
JFileChooser ch = new JFileChooser();//获取选中文件夹
int i = ch.showOpenDialog(null);//如果被选中就会得到一个数字0或者-1
File file = ch.getSelectedFile();//得到最终选中文件夹的绝对路径

得到最终路径后就可以通过IO流将保存在原集合中的每个学生信息以自定格式写入该文件夹。
int i = ch.showOpenDialog(null);
if (i == 0) {
File file = ch.getSelectedFile();
//被选中文件夹绝对地址
String str = file.getAbsolutePath();

				Writer w = null;
				BufferedWriter bw = null;
				try {
					w = new FileWriter(str);
					bw = new BufferedWriter(w);
					//将所有学生信息遍历,变为固定格式的学生信息,写入文件夹
					for (Student s1 : Text.stus) {
						//得到一个学生信息
						String src = (s1.getName()) + "  " + (s1.getGender() == 1 ? "男" : "女") + "  "
								+ (s1.getBirth()) + "  " + (s1.getMajor()) + "  "
								+ Arrays.toString(s1.getIntersts());
						//写入文件夹
						bw.write(src);
						//换行
						bw.newLine();
					}

这样就已经保存在了相对文件夹中,我们现在就可以以这种格式直接导入表格中。
在这里插入图片描述
在这里插入图片描述
导入和保存方法也类似,首先的要找到存信息的文件夹,得到绝对路径。与上同。然后在以前面所学的方法:面向对象来添加学生个人信息。
while ((src = br.readLine()) != null) {
String[] strs2 = src.split(" “);
//学生姓名
String name = strs2[0];
//学生年龄
int gender = strs2[1].equals(“男”) ? 1 : 2;
String[] str1 = strs2[2].split(”-");
//学生生日
int year = Integer.parseInt(str1[0]);
int month = Integer.parseInt(str1[1]);
int day = Integer.parseInt(str1[2]);
//学生专业
String major = strs2[3];
String[] str3 = strs2[4].split(", ");

						String[] intersts = new String[str3.length];
						//循环得到学生兴趣爱好
						for (int i2 = 0; i2 < str3.length; i2++) {
							if (i2 == 0) {
								intersts[i2] = str3[i2].substring(1, str3[i2].length());
							} else if (i2 == str3.length - 1) {
								intersts[i2] = str3[i2].substring(0, str3[i2].length() - 1);
							} else {
								intersts[i2] = str3[i2];
							}

						}
						//创建学生对象,存入学生信息
						Student s = new Student(name, gender, LocalDate.of(year, month, day), major, intersts);
						//将一个学生信息存入集合
						stus1.add(s);

最终将这新存学生信息的集合给原集合,然后调用打印表格的方法就可以实现。
注:代码没有简化。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值