• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how can i compare a username and delete it from a text file

 
gagandeep singhdhodi
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am trainee in C-DOT NEW DELHI,INDIA
i m trying to write a application in java to delete
a user from a text file in the following prog

i m unable to compare the user entered in the textfield and
delete in the deluser(); function
here's the code
plz reply
thanks

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java .applet.*;
import java.io.*;



class Del extends JFrame implements ActionListener
{

JTextField CWDMUserDText = new JTextField();
JButton CWDMSecDelButton = new JButton("DELETE");
JLabel CWDMUserLabel = new JLabel("USER NAME");
JPasswordField CWDMPassDText = new JPasswordField();
JLabel CWDMPassLabel = new JLabel("PASSWORD");
//String user,pass;



/* CONSTRUCTOR*/


Del()
{
Container contentPane=getContentPane();
contentPane.setLayout(null);

setLocation(400,200);

contentPane.add(CWDMUserDText);
contentPane.add(CWDMSecDelButton);
contentPane.add(CWDMUserLabel);
contentPane.add(CWDMPassDText);
contentPane.add(CWDMPassLabel);

CWDMUserLabel.setBounds(20,70,100,25);
CWDMPassLabel.setBounds(20,100,100,25);
CWDMUserDText.setBounds(150,70,100,25);
CWDMPassDText.setBounds(150,100,100,25);
CWDMSecDelButton.setBounds(80,135,100,25);

CWDMUserDText.addActionListener(this);
CWDMPassDText.addActionListener(this);
CWDMSecDelButton.addActionListener(this);


contentPane.setBackground(Color.CYAN);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);



}

public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==CWDMSecDelButton)
{

// user = CWDMUserDText.getText();
//pass = CWDMPassDText.getText();
//showarr();




CWDMUserDText.setText("");
CWDMPassDText.setText("");

DelUser();
}
}



public static void main(String args[])

{
Del myframe1= new Del();
myframe1.setSize(300,200);
myframe1.setVisible(true);

}





public void DelUser()
{
try
{

FileReader fr = new FileReader("file.txt");
BufferedReader br = new BufferedReader(fr);
String s1,s2;
while ((s1=br.readLine())!=null)
s2 = CWDMUserDText.getText();

if(s2.equals(s1))
{
s1.delete();a
System.out.println(s1);
}
}

catch(FileNotFoundException e)
{
System.out.println("exception occured");
}

catch(IOException e)
{
System.out.println("io");

}


}
}
 
Joe Ess
Bartender
Posts: 9312
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the JavaRanch, gagandeep. We love to talk Java, but seeing as how you are just starting out and the only way to learn how to program is by reading and writing code, we're going to ask that you do the work yourself. If you don't feel comfortable with the code you have so far, your first stop should be the Java Tutorial to pick up the basics of the language. If you understand what you have so far and just need help with the file part of the code, skip to the Input/Output part of the tutorial. Be aware that you can't just remove data from a file. You need to read in the data, alter it, then write it back out.
Good luck!
 
gaurav chhabra
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
best of luck

u can do one thing that store the user name in the file like the user name wil be "ram" and while checking read from file and check it.

now i u want to delete from file u can do one thing that u can replace the text with the SPACES (" ") so that it sems to be deleted.

freely as any question u have
bye
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic