Guest User

Untitled

a guest
Mar 31st, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. package Khan2ndSemAPL;
  2.  
  3. import java.awt.Container;
  4. import java.awt.GridLayout;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.io.FileInputStream;
  8. import java.io.FileNotFoundException;
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11.  
  12. import javax.swing.*;
  13.  
  14. public class Activity_33_Reading_fromText_FileInputStream extends JFrame implements ActionListener
  15. {
  16. FileInputStream fsreader;
  17. public JLabel l1, l2;
  18. public JTextField t1, t2;
  19. public JButton b1, b2;
  20. Activity_33_Reading_fromText_FileInputStream()
  21. {
  22. Container c = getContentPane();
  23. c.setLayout(new GridLayout(3, 2));
  24. l1 = new JLabel("Customer Login Name: ");
  25. l2 = new JLabel("Password: ");
  26. t1 = new JTextField();
  27. t2 = new JPasswordField();
  28. b1 = new JButton("Login");
  29. b2 = new JButton("Clear");
  30.  
  31. c.add(l1);
  32. c.add(l2);
  33. c.add(t1);
  34. c.add(t2);
  35. c.add(b1);
  36. c.add(b2);
  37.  
  38. b1.addActionListener(this);
  39. b2.addActionListener(this);
  40.  
  41. pack();
  42. setVisible(true);
  43. this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  44. this.setLocationRelativeTo(null);
  45. }
  46.  
  47. public void actionPerformed(ActionEvent ae)
  48. {
  49. if(ae.getSource() == b1)
  50. {
  51. if(t1.getText().isEmpty() || t1.getText().trim().equals("")
  52. || t2.getText().isEmpty() || t2.getText().trim().equals(""))
  53. {
  54. System.err.println("details are incomplete");
  55. JOptionPane.showMessageDialog(null, "Details are incomplete");
  56. }
  57. else
  58. {
  59. try
  60. {
  61. int i;
  62. char conv = Character.MIN_VALUE;
  63. //int file = Integer.MIN_VALUE;
  64.  
  65. String user_pass = "";
  66.  
  67. fsreader = new FileInputStream("C:\\Users\\10-0235C\\Documents\\migs\\login.text");
  68.  
  69. String temp2 = "PASSWORD: " + t1.getText() + " USERNAME: " + t2.getText();
  70. //String temp2 = t1.getText() + t2.getText();
  71.  
  72. System.out.println(" " + conv);
  73. System.out.println("password\\user inside text file: \nascii check if the same");
  74.  
  75. while((i=fsreader.read())!= -1)
  76. {
  77. System.out.print(" " + i);
  78. System.out.print("=" + (char)i);
  79. conv = (char)i;
  80. // file = file + i;
  81. user_pass = user_pass + Character.toString(conv);
  82. }
  83. //System.out.println(file);
  84. System.out.println();
  85. System.out.println(user_pass);
  86.  
  87. System.out.println();
  88. System.out.println("password\\user entered: \nascii check if the same");
  89.  
  90. for(int loop = 0; loop < temp2.length(); loop++)
  91. {
  92. System.out.print(" " + (int)temp2.charAt(loop) + "=" + temp2.charAt(loop));
  93. }
  94.  
  95. System.out.println();
  96. System.out.println(temp2);
  97.  
  98. if(user_pass == temp2)
  99. //if(user_pass.equals(temp2))
  100. {
  101. System.out.println("\nmatch");
  102. JOptionPane.showMessageDialog(null, "match");
  103. }
  104. else
  105. {
  106. System.out.println("\nnot match");
  107. JOptionPane.showMessageDialog(null, "not match");
  108. }
  109. fsreader.close();
  110. }
  111. catch(FileNotFoundException e)
  112. {
  113. System.out.println("cannot find login.txt file " + e.toString());
  114. e.printStackTrace();
  115. }
  116. catch(IOException e)
  117. {
  118. System.out.println("Error writing to file " + e.toString());
  119. e.printStackTrace();
  120. }
  121.  
  122. }
  123. }
  124. if(ae.getSource() == b2)
  125. {
  126. t1.setText(null);
  127. t2.setText(null);
  128. }
  129.  
  130. }
  131.  
  132. public static void main(String args[])
  133. {
  134. Activity_33_Reading_fromText_FileInputStream e = new Activity_33_Reading_fromText_FileInputStream();
  135. }
  136.  
  137.  
  138. }
Add Comment
Please, Sign In to add comment