Created
March 21, 2018 08:40
-
-
Save Brutt/ab6b026d28d20fb9c316ed6151c997b4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.Serializable; | |
import java.util.Date; | |
public class Message implements Serializable { | |
private int id; | |
private Date date; | |
private String content; | |
public Message(int id, Date date, String content) { | |
this.id = id; | |
this.date = date; | |
this.content = content; | |
} | |
@Override | |
public String toString() { | |
return "Message{" + | |
"id=" + id + | |
", date=" + date + | |
", content='" + content + '\'' + | |
'}'; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.FileNotFoundException; | |
import java.io.IOException; | |
import java.io.Serializable; | |
import java.util.Collection; | |
import java.util.List; | |
public interface MessageStore { | |
void persist(Message message) throws IOException; | |
void persist(Collection<Message> list) throws IOException; | |
Message load() throws IOException, ClassNotFoundException; | |
List<Message> load(int count) throws IOException, ClassNotFoundException; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.sql.Date; | |
import java.util.ArrayList; | |
import java.util.Collection; | |
import java.util.List; | |
public class TestForMessage implements MessageStore{ | |
@Override | |
public void persist(Message message) throws IOException { | |
FileOutputStream fos = new FileOutputStream("out_file.txt"); | |
ObjectOutputStream oos = new ObjectOutputStream(fos); | |
oos.writeObject(message); | |
oos.flush(); | |
oos.close(); | |
} | |
@Override | |
public void persist(Collection<Message> list) throws IOException { | |
FileOutputStream fos = new FileOutputStream("out_file.txt"); | |
ObjectOutputStream oos = new ObjectOutputStream(fos); | |
for (Message message : list) { | |
oos.writeObject(message); | |
} | |
oos.flush(); | |
oos.close(); | |
} | |
@Override | |
public Message load() throws IOException, ClassNotFoundException { | |
FileInputStream fis = new FileInputStream("out_file.txt"); | |
ObjectInputStream oin = new ObjectInputStream(fis); | |
return (Message) oin.readObject(); | |
} | |
@Override | |
public List<Message> load(int count) throws IOException, ClassNotFoundException { | |
FileInputStream fis = new FileInputStream("out_file.txt"); | |
ObjectInputStream oin = new ObjectInputStream(fis); | |
List<Message> msgs = new ArrayList<>(); | |
for (int i = 0; i < count; i++) { | |
msgs.add((Message) oin.readObject()); | |
} | |
return msgs; | |
} | |
public static void main(String[] args) throws IOException, ClassNotFoundException { | |
//System.out.println(Date.valueOf("2018-03-21")); | |
Message newMessage = new Message(1, Date.valueOf("2018-03-21"),"qwertry"); | |
TestForMessage testForMessage = new TestForMessage(); | |
testForMessage.persist(newMessage); | |
Message msg = testForMessage.load(); | |
System.out.println(msg.toString()); | |
ArrayList<Message> newMessages = new ArrayList<>(); | |
newMessages.add(new Message(1, Date.valueOf("2018-03-21"),"qwertry")); | |
newMessages.add(new Message(2, Date.valueOf("2018-03-22"),"zxcvbn")); | |
testForMessage.persist(newMessages); | |
List<Message> msgs = testForMessage.load(2); | |
for (Message message : msgs) { | |
System.out.println(message); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment