Easy Student Management system (IO stream, file,)

Source: Internet
Author: User
Tags readline


Previously used ArrayList to manage student management system easily. The principle of implementation is basically similar. From keyboard input to the program, read it to assign two variables (name and number), write object (object stream) to file ....



The specific implementation is as follows:


package cn.SystemStudent;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;




public class Student implements Serializable {
String name;
    int number;
Ranch
/ ** has the same effect as the constructor
    public Student (String name, int number) {
    this.name = name;
    this.number = number;
    } * /
public String getName () {
return name;
}
public void setName (String name) {
this.name = name;
}
public int getNumber () {
return number;
}
public void setNumber (int number) {
this.number = number;
}
// static File dir = new File ("f: / StudentTest /"); // File directory
private static final String STUDENT_STORE_DIR = "E: // student_store_dir /";
/ **
* Add students
* Principle: Call the keyboard input method, write the student attributes into the program, and then output the attributes to the file from the program
* /
public static void addStudent () {
Ranch
Student student = new Student (); // Instantiate a student object,
System.out.println ("Please enter student name:");
student.name = FileStudentUtil.getStringKeyBoard ();
System.out.println ("The student name you entered is:" + student.name);
Ranch
System.out.println ("Please enter student ID:");
student.number = FileStudentUtil.getintKeyBoard ();
System.out.println ("The student ID you entered is:" + student.number);
Ranch
File file = new File (STUDENT_STORE_DIR, student.name); // The file name (student name) in the directory
FileOutputStream fos = null;
ObjectOutputStream osr = null;
try {
fos = new FileOutputStream (file);
osr = new ObjectOutputStream (fos);
osr.writeObject (student); // Write the object to a file
} catch (FileNotFoundException e) {
e.printStackTrace ();
} catch (IOException e) {
e.printStackTrace ();
} finally {
if (osr! = null) {
try {
osr.close ();
} catch (IOException e) {
e.printStackTrace ();
}
}
if (fos! = null) {
try {
fos.close ();
} catch (IOException e) {
e.printStackTrace ();
}
}
}
}
/ **
* Get a single student object
* Implementation principle: input the object from the file into the program (read the object from the stream, assign it to another object, and return)
*
* /
public static Student getStudent (String name) {
File file = new File (STUDENT_STORE_DIR, name);
FileInputStream fis = null;
ObjectInputStream ois = null;
try {
fis = new FileInputStream (file);
ois = new ObjectInputStream (fis);
Student student = (Student) ois.readObject ();
return student;
} catch (FileNotFoundException e) {
e.printStackTrace ();
} catch (IOException e) {
e.printStackTrace ();
} catch (ClassNotFoundException e) {
e.printStackTrace ();
} finally {
if (ois! = null) {
try {
ois.close ();
} catch (IOException e) {
e.printStackTrace ();
}
}
if (fis! = null) {
try {
fis.close ();
} catch (IOException e) {
e.printStackTrace ();
}
}
}
return null;
}
Ranch
/ **
* Output all students:
* Implementation principle: Receive files to the array through dir.listFiles (); Iterating through files, calling a single file method
* java.io.InvalidClassException: This error can delete the file content and rewrite it normally. I think it can be resolved by refreshing it
* /
public static void showAllStudent () {
System.err.println ("name \ t student number");
File dir = new File (STUDENT_STORE_DIR); // new a file, pass the path in. Before calling dir.listFiles ()
File [] file = dir.listFiles (); // dir.listFiles (); All files in the directory are received by the array
// File [] file = dir.listFiles (); new file object, when the path parameter is passed, this line is used instead of the above two lines of code
for (File f: file) {// traverse the file,
Ranch
// Invoke a single file method, passing parameters by file name, and the above method reads the student object. Finally assigned to the new student object, print out
Student stu = getStudent (f.getName ());
System.out.println (stu);
}
}
/ **
* Rewrite to String
* /
@Override
public String toString () {
String s = name + "\ t" + number;
return s;
}
/ **
* Find student by student ID
* /
public static Student findStudent (int number) {
File dir = new File (STUDENT_STORE_DIR);
File [] file = dir.listFiles ();
for (File f: file) {
Student student = getStudent (f.getName ());
// if (student.number! = number) {
// return null;
//}
if (student.number == number) {
return student;
}
}
return null;
}
/ **
* Find the corresponding student by the student ID, then find the file name by the student name and delete
* Except for the system standard input and output streams, other streams must be closed, otherwise they cannot be deleted. Because it will be used.
* /
public static void deleteStudent2 () {
System.out.println ("Please delete student ID:");
int number = FileStudentUtil.getintKeyBoard ();
Student stu = findStudent (number);
File file = new File (STUDENT_STORE_DIR, stu.name);
System.out.println (file.getPath ());
Ranch
System.out.println ((file.delete ())? "Delete Student" + stu.name + "Success": "Delete Failure");
Ranch
}
Ranch
public static void findStduentBynumber () {
System.out.println ("Please enter the student ID you are looking for:");
int num = FileStudentUtil.getintKeyBoard ();
File dir = new File (STUDENT_STORE_DIR);
File [] file = dir.listFiles ();
for (File f: file) {
Student student = getStudent (f.getName ());
if (student.number! = num) {
System.out.println ("Student number does not exist.");
}
if (student.number == num) {
System.out.println ("The student you are looking for is:" + student.name);
}
}
Ranch
}
/ **
public static void deleteStudent () {
System.out.println ("Please enter the student ID of the student to be deleted:");
int number = FileStudentUtil.getintKeyBoard ();
Ranch
boolean flag = deleteStudent (number);
Ranch
System.out.println (flag? "Delete successfully": "Delete failed");
}
Ranch
public static boolean deleteStudent (int number) {
File [] file = dir.listFiles ();
for (File f: file) {
Student student = getStudent (f.getName ());
if (student.number == number) {
Ranch
if (! f.delete ()) {
File dF= new File (dir, student.name);
return dF.delete ();
} else {
return true;
}
}
}
return false;
} * /
}

package cn.SystemStudent;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class FileStudentUtil {
/ **
* Use io stream to get input from keyboard to program. Use Input (input from external device to program, output from program to external device);
* System.in (out) The standard system input and output do not need to close the stream. Otherwise, a stream closing error will be reported in a later test class
* /
public static String getStringKeyBoard () {
String name = "";
InputStreamReader isr = null;
BufferedReader br = null;
try {
isr = new InputStreamReader (System.in);
br = new BufferedReader (isr);
name = br.readLine ();
// int number = Integer.parseInt (name); convert String to int
} catch (IOException e) {
e.printStackTrace ();
}
// finally {
// try {
// if (br! = null)
// br.close ();
// if (isr! = null)
// isr.close ();
//} catch (IOException e) {
// e.printStackTrace ();
//}
//}
return name;
}
Ranch
public static int getintKeyBoard () {
while (true) {
int number = 0;
InputStreamReader isr = null;
BufferedReader br = null;
try {
isr = new InputStreamReader (System.in);
br = new BufferedReader (isr);
String s = br.readLine (); // Use readLine () in the buffer stream, each line is read as String. Converting to int
number = Integer.parseInt (s);
Ranch
return number;
Ranch
} catch (IOException e) {
e.printStackTrace ();
} catch (NumberFormatException a) {
System.out.println ("Please enter the correct numeric format");
}
}
// return 0;
}
}

package cn.SystemStudent;

import java.util.Scanner;



/ **
 * Project Name: "Student Information Management System" English: StudentManagerSystemFile
Version number: V1.1
Functional description: The student information management system can display, add, delete, search (find by student number), modify, and exit functions of student information.

Requirements: 1> Persistent storage of student information based on the student information management system version 1.0

              Create a file manipulation tool class FileUtil.
          Encapsulate student persistent storage:
             Add student
             Show Student List (showAllStudent)
             Delete student (deleteStudent)
             Find Student (findStudentByNumber)
             File create method File createFile (String path)
      .......
      
          Definition: storage path constant
        private static final String STUDENT_STORE_DIR = "E: // student_store_dir /";
Ranch
2> Change the keyboard input data obtained by Scanner class in v1.0 version to read data from character buffer stream
 * /
public class Test {
Ranch
public static final int ADDSTUDENT = 1;
Ranch
public static void main (String [] args) {
Ranch
System.out.println ("===== Welcome to the student management system =====")
System.out.println ("1 add student, 2 display student list, 3 delete student, 4 find student, 5 exit");
System.out.println ("===== Add student information operation =====");
System.out.println ("<Please enter the operation command:>");
Ranch
for (;;) {
int number = FileStudentUtil.getintKeyBoard ();
switch (number) {
case ADDSTUDENT:
Student.addStudent ();
Ranch
break;
case 2:
Student.showAllStudent ();
Ranch
break;
case 3:
Student.deleteStudent2 ();
Ranch
break;
case 4:
Student.findStduentBynumber ();
Ranch
break;
case 5:
System.out.println ("Successfully exited the system:");
System.exit (0);
}
System.out.println ("<Please enter the operation command:>");
}
// String s = FileStudentUtil.getStringKeyBoard ();
// System.out.println (s);
//Student.addStudent ();
//Student.getStudent(name);
// Student.showAllStudent ();
//Student.deleteStudent2 ();
// Student.findStduentBynumber ();
}
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.