Java implementation reads the student information from the file and implements the deletion and modification

Source: Internet
Author: User
Tags readline

Implement one-way linked list and realize IO first

Package SAVE;
	public class Linklist {//Linked list public Node-I;

	private int pos;
	Public linklist () {This.first = new Node (null);
		}/** * * @param stu/public void AddNode (Student stu) {node node = new node (stu);
		Node current = i;
		while (current.next!= null) {current = Current.next;
		} Node.next=current.next;
		
	Current.next = node;
		//delete node public void Deletenode (String num) {node current = i; if (First.next!= null) {while (!current.next.stu.snum.equals (num) && (current.next!= null)) {current =
			Current.next;
			} if (Current.next.stu.Snum.equals (num)) {current.next = Current.next.next;
			else {System.out.println ("there is no such student information in the system");
		} else {System.out.println ("There is no storage content in the system currently");
		}///Search node public node selectnode (String num) {node current = i; if (First.next!= null) {while (!current.next.stu.snum.equals (num) && (current.next!= null)) {current =
			Current.next;
	}		if (current.next.stu.Snum.equals (num)) {//current.next = Current.next.next;
			return current.next;
			else {System.out.println ("there is no such student information in the system");
		} else {System.out.println ("There is no storage content in the system currently");
	return null;
		Public boolean select (String num) {Node current = i; if (First.next!= null) {while (current.next.next!= null) &&!current.next.stu.snum.equals (num)) {Curren
			t = Current.next;
				} if (Current.next.stu.Snum.equals (num)) {//current.next = Current.next.next;
			return true;
			else {System.out.println ("there is no such student information in the system");
		} else {System.out.println ("There is no storage content in the system currently");
	return false;
 }

}

Package SAVE;
Import java.io.*;

Import java.util.*;
	Class Student {public String Name;
	Public String Snum;

	Public String School;
		Public Student () {} public Student (string name, String snum, string school) {name = name;
		Snum = Snum;
	School = School;
	} class Node {//Node public node next;
	Student Stu;
	Public Node (Student stu) {this.stu = Stu;  The public class Save {/** * Write method * @param str * @throws ioexception//*public static void Write (String
		STR, Boolean applent) throws IOException {String fileName = "C:" + file.separator + "Student.txt";
		File File = new file (fileName);
		OutputStream out = new FileOutputStream (file, applent);
		STR + + "r";
		Byte[] B = str.getbytes ();
		try {file.createnewfile ();
		catch (Exception e) {e.printstacktrace ();
	} out.close (); }*/public static void Write (String Str,boolean p) throws IOException {string fileName = "D:" + file.separator + "stud"
		Ent.txt "; File File = new file (FilenaME);	
		OutputStream out = new FileOutputStream (file, true);
		String str= "DSF";
		String deststr = new string ();  
        Deststr =str;  
        FileWriter FW = new FileWriter ("D://student.txt", p);  
        PrintWriter pw = new PrintWriter (FW);  
        
		Pw.println (DESTSTR);
		Byte[] B = str.getbytes ();
		try {file.createnewfile ();
		catch (Exception e) {e.printstacktrace ();
		}//out.close ();  
        Pw.close (); 
	Fw.close (); /** * Read Method * * @return * @throws ioexception/public static void Read (Linklist list) throws Ioexcepti
		
		On {InputStream is = new FileInputStream ("D://student.txt"); String str=new () string line;
		Used to hold content read per row Student stu= new Student ();
		BufferedReader reader = new BufferedReader (new InputStreamReader (IS)); line = Reader.readline ();
		Reads the first row if (line = = null) System.out.println ("File is empty"); while (Line!=null&&line.length () >=2) {//If line is empty description read//line+= "\ n";//Add newline character Stu = TranSformtostudent (line);
			List.addnode (Stu);
			System.out.println (line); line = Reader.readline ();
		Read Next line} reader.close ();
	Is.close (); /** * Converts string to Student class plus * * @param str * @param list/public static Student transformtostudent (String str
		) {Student stu;
		string[] Str1 = Str.split (",");
			if (str1.length!= 3) {System.out.println ("wrong Information");
		return null;
			else {stu = new Student (str1[1], str1[0], str1[2]);
		return Stu;
		The public static void Printstudent (Linklist link) throws IOException {Node n = link.first.next;
		String S = new string ();
			while (n!= null) {//system.out.println (N.stu.snum + "," + N.stu.name + "," + N.stu.school);
			S + + N.stu.snum + "," + N.stu.name + "," + N.stu.school + "\ r \ n";
		n = n.next;
		Write (S, false);
	System.out.println (S); }/** * * @param args * @throws ioexception/public static void main (string[] args) throws IOException {L
		Inklist list = new linklist (); Student stu = New Student ();
		File File = new file ("D:" + file.separator + "Student.txt");
		Read (list);
		String str = new string ();
		/*if (str.length () = 0) System.out.println ("File is empty");
			
			else {string[] line = str.split ("\ n");
				Converts the string to the student class and joins the list for (int i = 0; i < line.length; i++) {stu = Transformtostudent (Line[i]);
			List.addnode (Stu);
		}}*///Scanner in = new Scanner (system.in);
		String help = "Please enter a key value number for the corresponding function: 1. Increase the student information in the System 2. Delete the student information in the system" + "3. Revise student Information 4. Find student information 0. Exit";
		System.out.println (Help);
		int key = In.nextint ();
			while (key!= 0) {//system.out.println (Help);
					Switch (key) {//Add Case 1: {//do {System.out.println ("Please enter student information, order for school number, name, schools");
					str = In.next ();
					Student STU1 = transformtostudent (str); if (List.select (STU1).
					Snum)) {System.out.println ("The school number is already in the system, do not repeat input");
						} else{List.addnode (STU1);
						String S = new string (); S = stu1. Snum + "," + STU1. Name + "," + STU1. School;
					}//write (S, true);
					Printstudent (list);
					System.out.println (Help);
				Key = In.nextint ();

				while (key = = 5);
			Break
					//Delete Case 2: {//do {System.out.println ("Please enter the school number");
					str = In.next ();
					List.deletenode (str);
					Printstudent (list);
					System.out.println (Help);
				Key = In.nextint ();

				while (key = = 5);
			Break
					//Change Case 3: {//do {System.out.println ("Please enter the school number");
					str = In.next ();
					Student s2 = list.selectnode (str). Stu;
					System.out.println ("Please enter the revised information in order: Name, school");
					str = In.next ();
					string[] Arr = Str.split (","); S2.
					Name = arr[0]; S2.
					School = arr[1];
					Printstudent (list);
					System.out.println (Help);
				Key = In.nextint ();

				while (key = = 5);
			Break
					//Check Case 4: {//do {System.out.println ("Please enter the school number");
					String STR4 = In.next ();
					Student s2 = List.selectnode (STR4). Stu; String ste = S2. snum+ "," +s2. Name+ "," +s2.
School;					System.out.println ("School Number:" + s2.)
					Snum); System.out.println ("Name:" + s2.)
					Name); System.out.println ("School:" + s2.)
					School);
					System.out.println (Help);
				Key = In.nextint ();
				while (key = = 5);
			Break
				}/*default:system.out.println (Help); break;*/}}}


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.