jQuery in Action, 2nd edition*
The moose likes I/O and Streams and the fly likes how can i compare a username and delete it from a text file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "how can i compare a username and delete it from a text file " Watch "how can i compare a username and delete it from a text file " New topic
Author

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

gagandeep singhdhodi
Greenhorn

Joined: Jan 28, 2005
Posts: 1
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

Joined: Oct 29, 2001
Posts: 8836
    
    7

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!


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
gaurav chhabra
Ranch Hand

Joined: Jul 24, 2004
Posts: 109
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
 
Don't get me started about those stupid light bulbs.
 
subject: how can i compare a username and delete it from a text file
 
Similar Threads
Problem with Continue button to go Next Frame?
client hangs after getting data from server
Password verification program
How to create array of JButton and JLabel ?
help regarding batch file