Java IO _ instance operations-notes for single-user information management programs

Source: Internet
Author: User
Tags object serialization

Java IO _ instance operations-notes for single-user information management programs

Instance requirements:

Expand the previous menu program. The requirement is that the complete information of a person can be added when the program is added. The information of a person includes the name and age. You can modify or delete this information after saving it. How can this code be completed?
Tip: save with Object serialization.

In this case, the program can use the following classes: inputdata, person, operate, and menu. You need to add a file operation class to save and read the file content, and modify the operate class to add specific operations for it.

File Operation class:

Import Java. io. file; import Java. io. fileinputstream; import Java. io. fileoutputstream; import Java. io. objectinputstream; import Java. io. objectoutputstream; public class fileoperate {private file = NULL; Public fileoperate (string pathname) {This. file = new file (pathname);} public Boolean save (Object OBJ) throws exception {objectoutputstream OOS = NULL; Boolean flag = false; // define the operation flag. Try {OOS = new o Bjectoutputstream (New fileoutputstream (this. file); OOS. writeobject (OBJ); flag = true;} catch (exception e) {Throw E; // an exception is thrown to the called for processing.} finally {If (OOS! = NULL) {OOS. close () ;}return flag;} public object load () throws exception {object OBJ = NULL; objectinputstream OIS = NULL; try {OIS = new objectinputstream (New fileinputstream (this. file); OBJ = Ois. readobject (); // read object} catch (exception e) {Throw E;} finally {If (OIS! = NULL) {Ois. Close (); // close} return OBJ ;}}

Operations are personnel information, so you must define the person class.

Import Java. io. serializable; public class person implements serializable {private string name; private int age; Public Person (string name, int age) {This. name = Name; this. age = age;} Public String tostring () {return "name:" + this. name + "; age:" + this. age;} public void setname (string name) {This. name = Name;} public void setage (INT age) {This. age = age;} Public String getname () {return this. name;} public int ageage () {return this. age ;}}

Modify operation class:

Public class operate {public static void add () {inputdata input = new inputdata (); fileoperate fo = new fileoperate ("d :\\ test. per "); string name = input. getstring ("Enter name:"); int age = input. getint ("Enter age:", "age must be a number! "); Person per = new person (name, age); // instantiate the person object try {fo. save (PER);} catch (exception e) {e. printstacktrace ();} system. out. println ("information added successfully! ");} Public static void Delete () {fileoperate fo = new fileoperate (" D: \ test. per "); try {fo. save (null); // Save the object} catch (exception e) {e. printstacktrace ();} system. out. println ("information deleted successfully! ");} Public static void Update () {inputdata input = new inputdata (); fileoperate fo = new fileoperate (" d :\\ test. per "); person per = NULL; try {per = (person) fo. load (); // read object} catch (exception e) {e. printstacktrace ();} string name = input. getstring ("Enter the name (original name:" + per. getname () + ")"); int age = input. getint ("Enter age (original age:" + per. getage () + ")", "age must be a number"); Per = new person (name, age); // instantiate the person object try {fo. sav E (PER);} catch (exception e) {e. printstacktrace ();} system. Out. println ("Information modified successfully! ");} Public static void find () {fileoperate fo = new fileoperate (" D: \ test. per "); person per = NULL; try {per = (person) fo. load ();} catch (exception e) {e. printstacktrace ();} system. out. println (PER );}}

Compile the inputdata class

Import Java. io. *; import Java. util. *; import Java. text. *; public class inputdata {private bufferedreader Buf = NULL; Public inputdata () {This. buf = new bufferedreader (New inputstreamreader (system. in);} Public String getstring (string info) {string temp = NULL; system. out. print (Info); // print the prompt message try {temp = This. buf. readline ();} catch (ioexception e) {e. printstacktrace ();} return temp;} public int Geti NT (string info, string ERR) {int temp = 0; string STR = NULL; Boolean flag = true; while (FLAG) {STR = This. getstring (Info); If (Str. matches ("^ \ D + $") {// determines whether a number is composed of temp = integer. parseint (STR); flag = false;} else {system. out. println (ERR) ;}} return temp;} public float getfloat (string info, string ERR) {float temp = 0; string STR = NULL; Boolean flag = true; while (FLAG) {STR = This. getstring (Info); If (Str. Matches ("^ \ D + .? \ D + $ ") {temp = float. parsefloat (STR); flag = false;} else {system. out. println (ERR) ;}} return temp;} public date getdate (string info, string ERR) {date temp = NULL; string STR = NULL; Boolean flag = true; while (FLAG) {STR = This. getstring (Info); If (Str. matches ("^ \ D {4}-\ D {2}-\ D {2} $ ")) {simpledateformat SDF = new simpledateformat ("yyyy-mm-dd"); try {temp = SDF. parse (STR);} catch (exception e) {} flag = false;} else {system. out. println (ERR) ;}} return temp ;}}

Test the inputdata class

Import Java. io. *; import Java. util. *; public class testinput {public static void main (string ARGs []) throws exception {inputdata input = new inputdata (); // float F = input. getfloat ("Please enter decimal places:", "the input is not a decimal point. Please enter it again! "); // System. out. println ("decimal:" + F); Date d = input. getdate ("Enter the date in the format of (yyyy-mm-dd):", "the input date format is incorrect. Please enter it again"); system. out. println ("date" + d );}};

Compile menu

Public Class Menu {public menu () {While (true) {This. show (); // unlimited call menu display} public void show () {system. out. println ("==== XXX system ===="); system. out. println ("[1], add data"); system. out. println ("[2], delete data"); system. out. println ("[3], modify data"); system. out. println ("[4], View data"); system. out. println ("[0], system logout \ n"); inputdata input = new inputdata (); int I = input. getint ("select:", "enter the correct option! "); Switch (I) {Case 1: {operate. add (); // call the add operation break;} Case 2: {operate. delete (); // call the delete operation break;} Case 3: {operate. update (); // call the update operation break;} case 4: {operate. find (); // call to view the break;} case 0: {system. exit (1); // exit break;} default: {system. out. println ("select the correct operation! ");}}}};

Client type

import java.io.* ;public class ExecDemo03{    public static void main(String args[]) throws Exception{        new Menu() ;    }};

Operation successful:

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.