File tree Expand file tree Collapse file tree 5 files changed +45
-34
lines changed
Expand file tree Collapse file tree 5 files changed +45
-34
lines changed Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1- package com .stacktips .designpatterns ;
1+ package com .stacktips .designpatterns . singleton ;
22
33import java .util .HashMap ;
44import java .util .LinkedList ;
Original file line number Diff line number Diff line change 1- package com .stacktips .designpatterns ;
1+ package com .stacktips .designpatterns . singleton ;
22
33import java .util .Scanner ;
44
Original file line number Diff line number Diff line change 1+ package com .stacktips .transient_field ;
2+
3+ import java .io .*;
4+
5+ public class TestTransient {
6+
7+ public static void main (String [] args ) {
8+ User user = new User ("john_doe" , "password123" );
9+
10+ // Serialization
11+ try (ObjectOutputStream oos = new ObjectOutputStream (new FileOutputStream ("user.txt" ))) {
12+ oos .writeObject (user );
13+ } catch (IOException e ) {
14+ e .printStackTrace ();
15+ }
16+
17+ // Deserialization
18+ try (ObjectInputStream ois = new ObjectInputStream (new FileInputStream ("user.txt" ))) {
19+ User deserializedUser = (User ) ois .readObject ();
20+ System .out .println (deserializedUser ); // Password will be null
21+ } catch (IOException | ClassNotFoundException e ) {
22+ e .printStackTrace ();
23+ }
24+ }
25+ }
Original file line number Diff line number Diff line change 1+ package com .stacktips .transient_field ;
2+
3+ import java .io .Serializable ;
4+
5+ public class User implements Serializable {
6+ private String username ;
7+ private transient String password ; // will not be serialized
8+
9+ public User (String username , String password ) {
10+ this .username = username ;
11+ this .password = password ;
12+ }
13+
14+ @ Override
15+ public String toString () {
16+ return "User{username='" + username + "', password='" + password + "'}" ;
17+ }
18+ }
You can’t perform that action at this time.
0 commit comments