学习了io流,就把它用到了以前写的简单学生管理系统上,因为本人还未研究数据库,无法对数据长久存储,就利用io,以及字符串的切割函数对数据进行文件存储,代码如下
首先我最近看了面向接口编程即(设计一个类遵循依赖倒置原则—>降低耦合性,接口隔离原则—>将复杂的接口细化,避免一些实现类有一些没用的方法等..)
我先建立了一个people类,有一个显示人信息的方法,以及得到属性的方法..即使我们里面有get与set方法.....
package Jave_Demo4_24;
public interface IPeople {
public void show();
public String getname();
public String getnid();
}
接下来是Student实现类
package Jave_Demo4_24;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class Student implements IPeople{
private String name;
private String nid;
Scanner input = new Scanner(System.in);
static boolean k = true;
String str = "f:"+File.separator+"io"+File.separator+"Student.txt";
private static ArrayList<Student> arraylist = new ArrayList<Student>();
private void printTitle(){
System.out.println("====欢迎来到学生管理系统!====");
System.out.println("1.增学生,2删除学生,3改学生,4查学生,5显示学生,6退出");
System.out.println("=======================");
}
public Student(){
printTitle();
System.out.println("系统已有学生为:");
updateStudent();
while(k){
System.out.println("请输入操作符(1-5)>");
Scanner inputa = new Scanner(System.in);
int n = StudentTest.checkNum(inputa);
switch (n) {
case 1:
addStu();
break;
case 2:
delStu();
break;
case 3:
changeStu();
break;
case 4:
findStu();
break;
case 5:
show();
break;
case 6:
k = false;
break;
default:
k = false;
break;
}
}
}
public Student(String name,String nid){
this.name = name;
this.nid = nid;
}
public void addStu(){
System.out.println("请输入学生姓名:");
String name = input.next();
System.out.println("请输入学生学号:");
String nid = input.next();
boolean f = checkStu(nid);
if(f == true){
String student = "学生:"+name+" "+"学号:"+nid+" ";
byte[] b = student.getBytes();
writeStu(b,true);
arraylist.add(new Student(name,nid));
System.out.println("添加成功....");
}else{
System.out.println("已有该学生,验证后再输入");
}
}
public boolean checkStu(String id){
for(Student item:arraylist){
if(item.nid.equals(id)){
return false;
}
}
return true;
}
public int findStu(){
System.out.println("请输入学生学号:");
String id = input.next();
int count = 0;
for(Student item:arraylist){
if(item.nid.equals(id)){
System.out.println("该的学生信息为:"+item);
return count;
}
count++;
}
return -1;
}
public void delStu(){
int pos = findStu();
if(pos != -1){
arraylist.remove(pos);
System.out.println("删除成功...");
}else{
System.out.println("学生不存在,请查证后操作...");
}
updateDelStudent();
}
public void changeStu(){
int pos = findStu();
if(pos != -1){
Student stu = arraylist.get(pos);
System.out.println("请重新输入姓名:");
stu.name = input.next();
System.out.println("请重新输入学号:");
stu.nid = input.next();
System.out.println("设置成功");
}else{
System.out.println("学生不存在,请查证后操作...");
}
updateDelStudent();
}
public void updateStudent(){
byte[] byteStu = readStu();
if(byteStu == null){
System.out.println("初始化文件....无数据...");
}else{
String stu =new String(byteStu);
System.out.println(stu);
int pos = stu.indexOf(":");
int posa = 0;
while(pos != -1){
posa = stu.indexOf(" ");
String stuname = stu.substring(pos+1, posa);
stu = stu.substring(posa+1);
pos = stu.indexOf(":");
posa = stu.indexOf(" ");
String stunid = stu.substring(pos+1, posa);
arraylist.add(new Student(stuname,stunid));
stu = stu.substring(posa+1);
pos = stu.indexOf(":");
}
}
}
public void updateDelStudent(){
boolean f = false;
for(Student item:arraylist){
String student = "学生:"+item.name+" "+"学号:"+item.nid+" ";
byte[] b = student.getBytes();
writeStu(b,f);
f = true;
}
}
public byte[] readStu(){
File file = new File(str);
FileInputStream fileinput = null;
byte[] b = new byte[2048];
try {
fileinput = new FileInputStream(file);
try {
int num = fileinput.read(b);
if(num == -1){
System.out.println("无数据");
return null;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fileinput.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return b;
}
public void writeStu(byte[] b,boolean f){
File file = new File(str);
FileOutputStream fileoutput = null;
try {
if(f == true){
fileoutput = new FileOutputStream(file,f);
fileoutput.write(b);
}else{
fileoutput = new FileOutputStream(file);
fileoutput.write(b);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
try {
fileoutput.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void show() {
// TODO Auto-generated method stub
System.out.println("姓名:"+"\t"+"学号:");
for(Student item:arraylist){
System.out.println(item);
}
}
@Override
public String toString() {
return "学生:" + name + "\t " + nid ;
}
@Override
public String getname() {
// TODO Auto-generated method stub
return name;
}
@Override
public String getnid() {
// TODO Auto-generated method stub
return nid;
}
}
最后建立测试类,简单作业请勿见笑......
package Jave_Demo4_24;
import java.util.Scanner;
public class StudentTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
new Student();
}
public static int checkNum(Scanner input){ //简单处理一下输入操作符的异常.....
int i = 0;
try {
i = input.nextInt();
} catch (Exception e) {
// TODO: handle exception
System.out.println("输入有误,操作符为整数(1-6)");
System.out.println("重新输入操作符,非(1-6)的整数,均视为6.退出");
Scanner scanner = new Scanner(System.in);
String str = scanner.next();
int size = str.length();
int num=0;
for(int j=0;j<size;j++){
num = num + (int)str.charAt(j);
}
return num;
}
return i;
}
}