Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**import codes **/
- 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;
- /**body codes **/
- /**dont forget to change the file loc **/
- /**field names are un,pw,em **/
- /**generate the data first then erase the first lines :> **/
- File f = new File("C:\\Users\\Ivan Louise\\Documents\\Accounts");
- int ln;
- String username,password,email;
- public Login() {
- initComponents();
- }
- void createFolder(){
- if(!f.exists()){
- f.mkdirs();
- }
- }
- void readFile(){
- try {
- FileReader fr = new FileReader(f+"\\database.text");
- System.err.println("connected to database");
- } catch (FileNotFoundException ex) {
- try {
- FileWriter fw = new FileWriter(f+"\\database.text");
- System.out.println("File created");
- } catch (IOException ex1) {
- Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex1);
- }
- }
- }
- void addData(String user,String pass,String email){
- RandomAccessFile raf;
- try {
- raf = new RandomAccessFile(f+"\\database.text","rw");
- for(int i=0;i<ln;i++){
- raf.readLine();
- }
- raf.writeBytes("\r\n");
- raf.writeBytes("\r\n");
- raf.writeBytes("Username:"+user+"\r\n");
- raf.writeBytes("Password:"+pass+"\r\n");
- raf.writeBytes("Email:"+email);
- JOptionPane.showMessageDialog(null, "Registered Successfully");
- } catch (FileNotFoundException ex) {
- Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
- } catch (IOException ex) {
- Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- void CheckData(String user,String pass){
- try {
- RandomAccessFile raf = new RandomAccessFile(f+"\\database.text", "rw");
- String line = raf.readLine();
- username=line.substring(9);
- password=raf.readLine().substring(9);
- email=raf.readLine().substring(6);
- if(user.equals(username)&password.equals(password)){
- JOptionPane.showMessageDialog(null, "Pasword matched");
- }
- else{
- JOptionPane.showMessageDialog(null, "invalid username or password");
- }
- } catch (FileNotFoundException ex) {
- Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
- } catch (IOException ex) {
- Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- void logic(String user,String pass){
- try {
- RandomAccessFile raf= new RandomAccessFile(f+"\\database.text", "rw");
- for(int i=0;i<ln;i+=4){
- String forUser = raf.readLine().substring(9);
- String forPswd = raf.readLine().substring(9);
- if(user.equals(forUser) & pass.equals(forPswd)){
- JOptionPane.showMessageDialog(null, "Log in successfully");
- break;
- }else if(i==(ln-4)){
- JOptionPane.showMessageDialog(null, "login failed");
- break;
- }
- for(int k=1;k<=2;k++){
- raf.readLine();
- }
- }
- } catch (FileNotFoundException ex) {
- Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
- } catch (IOException ex) {
- Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- void countLines(){
- try {
- ln=1;
- RandomAccessFile raf = new RandomAccessFile(f+"\\database.text", "rw");
- for(int i=0;raf.readLine()!=null;i++){
- ln++;
- }
- System.out.println("number of lines:"+ln);
- } catch (FileNotFoundException ex) {
- Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
- } catch (IOException ex) {
- Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- /**button codes **/
- private void registerActionPerformed(java.awt.event.ActionEvent evt) {
- createFolder();
- readFile();
- countLines();
- addData(un.getText(),pw.getText(),em.getText());
- }
- private void clearActionPerformed(java.awt.event.ActionEvent evt) {
- un.setText("");
- pw.setText("");
- em.setText("");
- }
- private void loginActionPerformed(java.awt.event.ActionEvent evt) {
- createFolder();
- readFile();
- countLines();
- logic(un.getText(),pw.getText());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement