Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package textlogin;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.RandomAccessFile;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.swing.JOptionPane;
- /**
- *
- * @author Admin
- */
- public class notepad extends javax.swing.JFrame {
- File f = new File("C:\\Users\\Asus\\OneDrive\\Desktop\\Testing");
- int ln;
- String Username,Password,Email;
- /**
- * Creates new form notepad
- */
- public notepad() {
- initComponents();
- }
- void createFolder(){
- if(!f.exists()){
- f.mkdirs();
- }
- }
- void readFile(){
- try {
- FileReader fr = new FileReader(f+"\\logins.txt");
- System.out.println("file exists!");
- } catch (FileNotFoundException ex) {
- try {
- FileWriter fw = new FileWriter(f+"\\logins.txt");
- System.out.println("File created");
- } catch (IOException ex1) {
- Logger.getLogger(notepad.class.getName()).log(Level.SEVERE, null, ex1);
- }
- }
- }
- void addData(String usr,String pswd,String mail){
- try {
- RandomAccessFile raf = new RandomAccessFile(f+"\\logins.txt", "rw");
- for(int i=0;i<ln;i++){
- raf.readLine();
- }
- //if condition added after video to have no lines on first entry
- if(ln>0){
- raf.writeBytes("\r\n");
- raf.writeBytes("\r\n");
- }
- raf.writeBytes("Username:"+usr+ "\r\n");
- raf.writeBytes("Password:"+pswd+ "\r\n");
- raf.writeBytes("Email:"+mail);
- } catch (FileNotFoundException ex) {
- Logger.getLogger(notepad.class.getName()).log(Level.SEVERE, null, ex);
- } catch (IOException ex) {
- Logger.getLogger(notepad.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- void CheckData(String usr,String pswd){
- try {
- RandomAccessFile raf = new RandomAccessFile(f+"\\logins.txt", "rw");
- String line = raf.readLine();
- Username=line.substring(9);
- Password=raf.readLine().substring(9);
- Email = raf.readLine().substring(6);
- if(usr.equals(Username)& pswd.equals(Password)){
- JOptionPane.showMessageDialog(null, "Password matched");
- }else{
- JOptionPane.showMessageDialog(null, "Wrong user/Password");
- }
- } catch (FileNotFoundException ex) {
- Logger.getLogger(notepad.class.getName()).log(Level.SEVERE, null, ex);
- } catch (IOException ex) {
- Logger.getLogger(notepad.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- void logic(String usr,String pswd){
- try {
- RandomAccessFile raf = new RandomAccessFile(f+"\\logins.txt", "rw");
- for(int i=0;i<ln;i+=4){System.out.println("count "+i);
- String forUser = raf.readLine().substring(9);
- String forPswd = raf.readLine().substring(9);
- if(usr.equals(forUser) & pswd.equals(forPswd)){
- JOptionPane.showMessageDialog(null, "password matched");
- break;
- }else if(i==(ln-3)){
- JOptionPane.showMessageDialog(null, "incorrect username/password");
- break;
- }
- // if you are using user & passwword without email
- // then dont forget to replace k<=2 with k=2 below
- for(int k=1;k<=2;k++){
- raf.readLine();
- }
- }
- } catch (FileNotFoundException ex) {
- Logger.getLogger(notepad.class.getName()).log(Level.SEVERE, null, ex);
- } catch (IOException ex) {
- Logger.getLogger(notepad.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- void countLines(){
- try {
- ln=0;
- RandomAccessFile raf = new RandomAccessFile(f+"\\logins.txt", "rw");
- for(int i=0;raf.readLine()!=null;i++){
- ln++;
- }
- System.out.println("number of lines:"+ln);
- } catch (FileNotFoundException ex) {
- Logger.getLogger(notepad.class.getName()).log(Level.SEVERE, null, ex);
- } catch (IOException ex) {
- Logger.getLogger(notepad.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- /**
- * This method is called from within the constructor to initialize the form.
- * WARNING: Do NOT modify this code. The content of this method is always
- * regenerated by the Form Editor.
- */
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- private void initComponents() {
- jButton2 = new javax.swing.JButton();
- jButton3 = new javax.swing.JButton();
- jButton4 = new javax.swing.JButton();
- jLabel1 = new javax.swing.JLabel();
- jLabel2 = new javax.swing.JLabel();
- jLabel3 = new javax.swing.JLabel();
- jLabel4 = new javax.swing.JLabel();
- tfusr = new javax.swing.JTextField();
- tfpswd = new javax.swing.JTextField();
- tfmail = new javax.swing.JTextField();
- jButton1 = new javax.swing.JButton();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- jButton2.setText("Login");
- jButton2.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton2ActionPerformed(evt);
- }
- });
- jButton3.setText("clear all");
- jButton3.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton3ActionPerformed(evt);
- }
- });
- jButton4.setText("test");
- jButton4.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton4ActionPerformed(evt);
- }
- });
- jLabel1.setFont(new java.awt.Font("Arial", 0, 36)); // NOI18N
- jLabel1.setText(".txt Login");
- jLabel2.setFont(new java.awt.Font("Arial", 0, 18)); // NOI18N
- jLabel2.setText("Username");
- jLabel3.setFont(new java.awt.Font("Arial", 0, 18)); // NOI18N
- jLabel3.setText("Password");
- jLabel4.setFont(new java.awt.Font("Arial", 0, 18)); // NOI18N
- jLabel4.setText("Email");
- tfusr.setFont(new java.awt.Font("Arial", 0, 18)); // NOI18N
- tfpswd.setFont(new java.awt.Font("Arial", 0, 18)); // NOI18N
- tfmail.setFont(new java.awt.Font("Arial", 0, 18)); // NOI18N
- jButton1.setText("Register");
- jButton1.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton1ActionPerformed(evt);
- }
- });
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(140, 140, 140)
- .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 202, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGroup(layout.createSequentialGroup()
- .addGap(69, 69, 69)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(69, 69, 69)
- .addComponent(jButton2)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 95, Short.MAX_VALUE)
- .addComponent(jButton3))
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
- .addComponent(jButton4)
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 112, Short.MAX_VALUE)
- .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- .addGap(64, 64, 64)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addComponent(tfusr)
- .addComponent(tfpswd)
- .addComponent(tfmail, javax.swing.GroupLayout.DEFAULT_SIZE, 129, Short.MAX_VALUE)))))))
- .addGap(54, 54, 54))
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(22, 22, 22)
- .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(31, 31, 31)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addComponent(tfusr, javax.swing.GroupLayout.DEFAULT_SIZE, 44, Short.MAX_VALUE)
- .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addComponent(tfpswd, javax.swing.GroupLayout.DEFAULT_SIZE, 43, Short.MAX_VALUE)
- .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(tfmail, javax.swing.GroupLayout.DEFAULT_SIZE, 42, Short.MAX_VALUE))
- .addGap(36, 36, 36)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, 38, Short.MAX_VALUE)
- .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(jButton4))
- );
- pack();
- }// </editor-fold>
- private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
- createFolder();
- readFile();countLines();
- logic(tfusr.getText(), tfpswd.getText());
- }
- private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
- tfusr.setText("");
- tfpswd.setText("");
- tfmail.setText("");// TODO add your handling code here:
- }
- private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
- createFolder();
- readFile();
- countLines();
- addData("nilesh","1234", "nil93@gmail.com");
- //CheckData("nilesh", "1234");
- // TODO add your handling code here:
- }
- private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
- createFolder();
- readFile();
- countLines();
- addData(tfusr.getText(),tfpswd.getText(),tfmail.getText());
- /*changed aftere video -->.*/JOptionPane.showMessageDialog(null,"Data Registered"); // TODO add your handling code here:
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String args[]) {
- /* Set the Nimbus look and feel */
- //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
- /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
- * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
- */
- try {
- for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
- if ("Nimbus".equals(info.getName())) {
- javax.swing.UIManager.setLookAndFeel(info.getClassName());
- break;
- }
- }
- } catch (ClassNotFoundException ex) {
- java.util.logging.Logger.getLogger(notepad.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(notepad.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(notepad.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(notepad.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- }
- //</editor-fold>
- //</editor-fold>
- /* Create and display the form */
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new notepad().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JButton jButton1;
- private javax.swing.JButton jButton2;
- private javax.swing.JButton jButton3;
- private javax.swing.JButton jButton4;
- private javax.swing.JLabel jLabel1;
- private javax.swing.JLabel jLabel2;
- private javax.swing.JLabel jLabel3;
- private javax.swing.JLabel jLabel4;
- private javax.swing.JTextField tfmail;
- private javax.swing.JTextField tfpswd;
- private javax.swing.JTextField tfusr;
- // End of variables declaration
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement