Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class User implements Serializable{
- public User(String username, String password) {
- this.username=username;
- this.password=password;
- }
- private static final long serialVersionUID = 1L;
- String username;
- String password;
- }
- public class SerializableExample {
- public static void main(String[] args) {
- User user = new User("userB","passwordB");
- String filename = "E:\Proj-docs\userFile.txt";
- FileOutputStream file;
- try {
- file = new FileOutputStream(filename);
- ObjectOutputStream out = new ObjectOutputStream(file);
- out.writeObject(user);
- out.close();
- file.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- User user2=null;
- try {
- FileInputStream file2 = new FileInputStream(filename);
- ObjectInputStream in = new ObjectInputStream(file2);
- user2= (User) in.readObject();
- Optional checkNull = Optional.ofNullable(user2);
- if(checkNull.isPresent()) {
- System.out.println(user2.username + " "+user2.password);
- }else {
- System.out.println("Null Object");
- }
- }catch(IOException | ClassNotFoundException e) {
- e.printStackTrace();
- }
- }
- }
- public class SerializableExample {
- public static void main(String[] args) {
- User user = new User("userD","passwordD");
- String filename = "E:\Proj-docs\userFile.txt";
- FileOutputStream file;
- try {
- file = new FileOutputStream(filename);
- ObjectOutputStream out = new ObjectOutputStream(file);
- out.writeObject(user);
- out.close();
- file.close();
- :
- :
- :
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement