} catch (IOException e){ System.err.println("File already exist: " + myFile);
Its Still Overwrites The File?
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
} catch (IOException e){ System.err.println("File already exist: " + myFile);
Its Still Overwrites The File?
Err what?
Please elaborate.
Please use [highlight=Java]//code goes here...[/highlight] tags when posting your 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();
}
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>?
not using filewriter as a classFrom your code.FileWriter fw = new FileWriter(myFile);
That looks like you are using the FileWriter class.
What does the following mean:
im using it as a try on a login button
Is this the same problem?
http://www.javaprogrammingforums.com...t-files-d.html
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?
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.
Last edited by copeg; January 8th, 2012 at 06:14 PM.