Code :} catch (IOException e){ System.err.println("File already exist: " + myFile);
Its Still Overwrites The File?
Printable View
Code :} catch (IOException e){ System.err.println("File already exist: " + myFile);
Its Still Overwrites The File?
Err what? :P
Please elaborate.
login = new JButton(" Login ");
add(login);
login.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
File myFile = new File(username.getText() + ".txt");
try {
FileWriter fw = new FileWriter(myFile);
PrintWriter pw = new PrintWriter(fw);
pw.println("Username: " + username.getText());
pw.println("Password: " + password.getText());
pw.close();
} catch (FileNotFoundException e) {
System.err.println("File not found: " + myFile);
} catch (IOException e){
System.err.println("File already exist: " + myFile);
} catch (Exception e) {
e.printStackTrace();
}
Its a login system, and it keeps overwriting the files that's already there.
Read the API for FileWriter...use the constructor that accepts an append boolean argument
FileWriter (Java Platform SE 6)
I've Read this twice but i dont understand how to add them, im not using filewriter as a class im using it as a try on a login button>?
Quote:
not using filewriter as a class
From your code.Code :FileWriter fw = new FileWriter(myFile);
That looks like you are using the FileWriter class.
What does the following mean:
Quote:
im using it as a try on a login button
Is this the same problem?
http://www.javaprogrammingforums.com...t-files-d.html
Code :login = new JButton(" Login "); add(login); login.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { File myFile = new File(username.getText() + ".txt"); try { FileWriter fw = new FileWriter(myFile); PrintWriter pw = new PrintWriter(fw); pw.println("Username: " + username.getText()); pw.println("Password: " + password.getText()); pw.close(); } catch (FileNotFoundException e) { System.err.println("File not found: " + myFile); } catch (IOException e){ System.err.println("File already exist: " + myFile); } catch (Exception e) { e.printStackTrace(); }
How different?
From this thread: Its Still Overwrites The File?
title of the other thread: -filewrite-over-writing-txt-files-d.html
What is the purpose of your post #9?
Code :FileWriter fw = new FileWriter(myFile);
How is this not using the FileWriter class? When you create a FileWriter, it creates a new file - overwriting any previous file UNLESS you have specified you wish to append, which you have not.
Edit: as explained here. Please keep discussions in their appropriate threads and not duplicate discussions.