Package com.manage.tset;
Import Java.io.File;
Import java.util.ArrayList;
Public interface Imanagenewsinfo {
Ways to display information about all the news
Arraylist<newsinfo> Showallnews () throws Exception;
Implement ways to add news
void Addnews (Newsinfo news) throws Exception;
Ways to delete News
Boolean deletenews (int newsId) throws Exception;
}
Interface//////////////////////////////////////////////
Package com.manage.tset;
Import java.io.Serializable;
Import Java.util.Date;
public class Newsinfo implements serializable{
Private Integer newsid;//News number
Private String newstitle;//News Headlines
Private String newscontent;//News content
Private String newsdate;//Release time
Get GetXXX and Setxxx methods
Public Integer Getnewsid () {
return newsId;
}
public void Setnewsid (Integer newsId) {
This.newsid = newsId;
}
Public String Getnewstitle () {
return newstitle;
}
public void Setnewstitle (String newstitle) {
This.newstitle = Newstitle;
}
Public String getnewscontent () {
return newscontent;
}
public void Setnewscontent (String newscontent) {
This.newscontent = newscontent;
}
Public String getnewsdate () {
return newsdate;
}
public void Setnewsdate (String newsdate) {
This.newsdate = newsdate;
}
Public Newsinfo (Integer newsId, String newstitle, String newscontent,
String newsdate) {
This.newsid = newsId;
This.newstitle = Newstitle;
This.newscontent = newscontent;
This.newsdate = newsdate;
}
@Override
Public String toString () {
Return "Newsinfo [newsid=" + NewsId + ", newstitle=" + newstitle
+ ", newscontent=" + Newscontent + ", newsdate=" + newsdate
+ "]";
}
}
Implement the interface class///////////////////////////////////////////////////////////////
Package com.manage.tset;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.ObjectInputStream;
Import Java.io.ObjectOutputStream;
Import Java.io.OutputStream;
Import java.io.Serializable;
Import java.util.ArrayList;
Import java.util.List;
/*
* Business Management class
* 1. The main way to implement the interface
* 2. Manage Business Implementation
* 3. To serialize the object of the collection to a file, and then remove it from the file by deserializing it
* */
public class Mamagenewsinfo implements imanagenewsinfo{
static file Newsfile = new file ("E:\\manage\\news.txt");
arraylist<newsinfo> list = new arraylist<newsinfo> ();//Define a collection
static{
/*
* Static code blocks are primarily used for initialization of classes. It executes only once, and before the main function.
The main features of static code blocks are:
Static blocks of code are executed automatically when the class is loaded.
Static code blocks can only be defined inside a class and cannot be defined inside a method.
The variables in the static code block are local variables and are only valid within blocks.
Multiple static blocks of code can be defined in a class and executed sequentially.
Static code blocks can only access static members of a class, not access instance members.
* */
Create the file, where you want to surround it with static, or it will go wrong, because you have not allocated memory in memory and start using it will be an error.
File File = new file ("E:\\manage");
if (!file.exists ()) {//determines if the file exists, does not exist then creates
File.mkdirs ();
}
Newsfile = new File ("E:\\manage\\news.txt");
if (!newsfile.exists ()) {//determines if the file exists, does not exist then creates
try {
Newsfile.createnewfile ();
When there is no file, the empty collection serialized into the file, in the anti-serialized out, only loaded once, when there is a file exists, no longer the empty serialization to the file
New Mamagenewsinfo (). Reserializable (Newsfile,new mamagenewsinfo (). list);//serialization of an empty collection into a file
ArrayList list = new Mamagenewsinfo (). deserializable (Newsfile);//anti-serialization set
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
try {
New Mamagenewsinfo (). Reserializable (Newsfile,new mamagenewsinfo (). list);
ArrayList list = new Mamagenewsinfo (). deserializable (Newsfile);
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}//method of calling serialization
}
Define a collection
Newsinfo news = new Newsinfo ();
/* (Non-javadoc)
* @see Com.manage.tset.imanagenewsinfo#showallnews ()
*/
Ways to display information about all the news
Public arraylist<newsinfo> Showallnews () throws exception{
/**
Create an output low-level stream
OutputStream OS = new FileOutputStream ("E:\\manage\\news.txt");
Create a serialized output stream to serialize an empty collection
ObjectOutputStream oos = new ObjectOutputStream (OS);
Oos.writeobject (list);
*/
Reserializable (newsfile,list);//method of calling serialization
/**
Define an input low-level stream
InputStream is = new FileInputStream ("E:\\manage\\news.txt");
Creates an anti-series input stream, a collection object in an inverse series file
ObjectInputStream ois = new ObjectInputStream (IS);
Deserializes the contents of a file and casts it into a Newsinfo object
arraylist<newsinfo> list = (arraylist<newsinfo>) ois.readobject ();
*/
arraylist<newsinfo> list = deserializable (newsfile);//Call the deserialized method and return a collection of ArrayList types
if (!list.isempty ()) {
for (Newsinfo news:list) {
System.out.println (News.getnewsid () + "\ T" +news.getnewstitle () + "\ T" +news.getnewscontent () + "\ T" + News.getnewsdate ());
//}
Reserializable (newsfile,list);//method of calling serialization
return list;
}else{
SYSTEM.OUT.PRINTLN ("There are no news messages");
return null;
}
}
Implement ways to add news
/* (Non-javadoc)
* @see com.manage.tset.imanagenewsinfo#addnews (com.manage.tset.NewsInfo, Java.io.File)
*/
/** Add News,
* 1. First of all, the method of anti-serialization is called, and the set is serialized out,
* 2. Then add the object to the collection,
* 3. Serialize the collection into a file by serialization
* Note: deserialization is done in order not to overwrite the contents of the previous content
* @throws Exception
*/
public void Addnews (Newsinfo news) throws exception{
Arraylist<newsinfo> li = deserializable (newsfile);//Put the set back into series,
Li.add (news);
System.out.println (li); test whether to add objects to the collection
Reserializable (Newsfile,li);//Call the serialization method, and then serialize the collection to the file
/*
* Anti-serialization to see if the collection is deposited in the file,
arraylist<newsinfo> list = deserializable (Newsfile);
for (Newsinfo j:list) {
System.out.println (News.getnewsid () + "\ T" +news.getnewstitle () + "\ T" +news.getnewscontent () + "\ T" +news.getnewsdate ( ));
}
*/
}
Ways to delete News
/* (Non-javadoc)
* @see com.manage.tset.imanagenewsinfo#deletenews (int)
*
* 1. Take the collection out of the counter-serialization
* 2. See if there is a news that is the same as the news number by a circular comparison
* 3, how to have, then delete, no then output not found
*/
public boolean deletenews (int newsId) throws exception{
Boolean flag=false;
Arraylist<newsinfo> li = deserializable (newsfile);//The set is serialized out and received with a ArrayList
if (!li.isempty ()) {
for (Newsinfo Lt:li) {
if (Lt.getnewsid (). Intvalue () ==newsid) {//If the collection exists and the entered numbers are equal, the delete
Li.remove (LT);
And serialize the collection into a file.
Reserializable (Newsfile,li);//Call the serialization method, and then serialize the collection to the file
Flag = true;
return true;
}
}
}
else{
SYSTEM.OUT.PRINTLN ("There are no news messages you want to delete");
return false;
//}
return false;
}
Define a serialized method
private void reserializable (file file,arraylist<newsinfo> list) throws exception{
Create an output low-level stream
OutputStream OS = new FileOutputStream (file);
Create a serialized output stream to serialize an empty collection
ObjectOutputStream oos = new ObjectOutputStream (OS);
Oos.writeobject (list);
Oos.flush ();
Oos.close ();
}
Define an inverse serialization method
Private arraylist<newsinfo> deserializable (file file) throws exception{
Define an input low-level stream
InputStream is = new FileInputStream (file);
Creates an anti-series input stream, a collection object in an inverse series file
ObjectInputStream ois = new ObjectInputStream (IS);
Deserializes the contents of a file and casts it into a Newsinfo object
arraylist<newsinfo> list = (arraylist<newsinfo>) ois.readobject ();
Ois.close ();
return list;
}
}
Business Implementation Class///////////////////////////////////////////
Package com.manage.tset;
Import Java.text.SimpleDateFormat;
Import java.util.ArrayList;
Import Java.util.Date;
Import Java.util.Scanner;
/*
* Test News Management System class
* */
public class Newsinfotest {
public static void Main (string[] args) {
Mamagenewsinfo mio = new Mamagenewsinfo ();
Newsinfo NIF;
SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd");
Create a scanner
Scanner sc = new Scanner (system.in);
System.out.println ("<<<<<<<<<<<<<<<<<<<<<< << Welcome to the Ultimate edition of news management system >>>>>>>>>>>>>>>>>>>>>> > ");
while (true) {
System.out.println ("1. Inquiry News");
System.out.println ("2. Add News");
System.out.println ("3. Delete News");
System.out.println ("0. Exit system");
SYSTEM.OUT.PRINTLN ("Please select function (1,2,3,0)");
int number = Sc.nextint ();
SYSTEM.OUT.PRINTLN ("News content as follows:");
System.out.println ("==========================================================");
System.out.println ("number" + "\ T title" + "\ t content" + "\ t time");
Switch (number) {
Case 1:
try {
arraylist<newsinfo> list = Mio.showallnews ();
if (list! = null) {
for (Newsinfo news:list) {
System.out.println (News.getnewsid () + "\ T" +news.getnewstitle () + "\ T" +news.getnewscontent () + "\ T" +news.getnewsdate ( ));
}
}else{
SYSTEM.OUT.PRINTLN ("There are no news messages");
}
} catch (Exception e) {
Because here is the main method, belongs to the highest level, where the exception can only come out, can not throw throws
SYSTEM.OUT.PRINTLN ("Query news failure!!! ");
E.printstacktrace ();
}
Break
Case 2:
Newsinfo news = new Newsinfo ();
System.out.println ("Please enter the press number:");
Integer newsId = Sc.nextint ();
Sc.nextline ();
System.out.println ("Please enter a news headline:");
String newstitle = Sc.nextline ();
System.out.println ("Please enter news content:");
String newscontent = Sc.nextline ();
System.out.println ("Please enter the press release Time");
String date = Sc.nextline ();
Date date = new Date ();
nif= New Newsinfo (Newsid,newstitle,newscontent,sdf.format (date));
try {
Mio.addnews (NIF);
System.out.println ("added success!!! ");
} catch (Exception e) {
System.out.println ("Add failed!!!" ");
E.printstacktrace ();
}
Break
Case 3:
System.out.println ("Please enter the number you want to delete:");
Integer Newsnumber = Sc.nextint ();
try {
Boolean flag = Mio.deletenews (Newsnumber);
if (flag) {
System.out.println ("Delete success!!!" ");
}else{
System.out.println ("Delete failed!!! ");
}
} catch (Exception e) {
SYSTEM.OUT.PRINTLN ("Exception occurred when deleting, failed!!!") ");
E.printstacktrace ();
}
Break
Case 0://Exit System
SYSTEM.OUT.PRINTLN ("Thank you for using this news management system, welcome the next visit!!!" ");
Sc.close ();
System.exit (0);
Break
}
System.out.println ("==========================================================");
}
}
}
Test class/////////////////////////////////////////////
Use the collection and file series, the inverse serialization simulation realizes the data storage function