Advertisement
Guest User

Untitled

a guest
Apr 11th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. /*
  2. *
  3. * @author Shreya Dhaga
  4. */
  5. import java.io.*;
  6. import java.util.Scanner;
  7.  
  8. class Details {
  9.  
  10. Scanner scan = new Scanner(System.in);
  11. String username = new String();
  12. String password = new String();
  13.  
  14. void getName() {
  15. System.out.println("Take username");
  16. this.username = scan.next();
  17. }
  18.  
  19. String setName() {
  20. return this.username;
  21. }
  22.  
  23. void getPassword() {
  24. System.out.println("Take password");
  25. this.password = scan.next();
  26. }
  27.  
  28. String setPassword() {
  29. return this.password;
  30. }
  31.  
  32. }
  33.  
  34. public class ObjectOutputStreamDemo {
  35.  
  36. public static void main(String[] args) {
  37. Details g = new Details();
  38. String s, q;
  39. //TODO run this for multiple times
  40. g.getName();
  41. g.getPassword();
  42. s = g.setName();
  43. q = g.setPassword();
  44.  
  45. try {
  46. FileOutputStream out = new FileOutputStream("F:\\newfile.txt");
  47. ObjectOutputStream login = new ObjectOutputStream(out);
  48. login.writeObject(s);
  49. login.writeObject(q);
  50. login.close();
  51. // check that the file is appended in multiple run
  52. // TODO search a string in a file
  53. // character wise check or string cmp
  54. FileInputStream in = new FileInputStream("F:\\newfile.txt");
  55. ObjectInputStream iin = new ObjectInputStream(in);
  56. System.out.println("" + (String) iin.readObject() + " " + (String) iin.readObject());
  57. } catch (Exception e) {
  58. System.out.println("Exception caught");
  59. e.printStackTrace();
  60. }
  61.  
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement