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.

Results 1 to 11 of 11

Thread: What Wrong With My Code: IOException.

  1. #1
    Member
    Join Date
    Jan 2012
    Posts
    107
    My Mood
    Grumpy
    Thanks
    6
    Thanked 2 Times in 1 Post

    Post What Wrong With My Code: IOException.

        }	catch (IOException e){
        		System.err.println("File already exist: " + myFile);


    Its Still Overwrites The File?


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: What Wrong With My Code: IOException.

    Err what?
    Please elaborate.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Member
    Join Date
    Jan 2012
    Posts
    107
    My Mood
    Grumpy
    Thanks
    6
    Thanked 2 Times in 1 Post

    Default Re: What Wrong With My Code: IOException.

    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.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: What Wrong With My Code: IOException.

    Read the API for FileWriter...use the constructor that accepts an append boolean argument
    FileWriter (Java Platform SE 6)

  5. #5
    Member
    Join Date
    Jan 2012
    Posts
    107
    My Mood
    Grumpy
    Thanks
    6
    Thanked 2 Times in 1 Post

    Default Re: What Wrong With My Code: IOException.

    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>?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: What Wrong With My Code: IOException.

    not using filewriter as a class
    FileWriter fw = new FileWriter(myFile);
    From your code.
    That looks like you are using the FileWriter class.

    What does the following mean:
    im using it as a try on a login button

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: What Wrong With My Code: IOException.


  8. #8
    Member
    Join Date
    Jan 2012
    Posts
    107
    My Mood
    Grumpy
    Thanks
    6
    Thanked 2 Times in 1 Post

    Default Re: What Wrong With My Code: IOException.

    Quote Originally Posted by Norm View Post
    no, i figared that out then i had a differnt problem lol

  9. #9
    Member
    Join Date
    Jan 2012
    Posts
    107
    My Mood
    Grumpy
    Thanks
    6
    Thanked 2 Times in 1 Post

    Default Re: What Wrong With My Code: IOException.

    Quote Originally Posted by Norm View Post
    FileWriter fw = new FileWriter(myFile);
    From your code.
    That looks like you are using the FileWriter class.

    What does the following mean:

    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();
    }

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: What Wrong With My Code: IOException.

    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?

  11. #11
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: What Wrong With My Code: IOException.

    Quote Originally Posted by Java Programmer View Post
    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>?
    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.

Similar Threads

  1. java.io.IOException: Premature EOF
    By CrazyVag in forum File I/O & Other I/O Streams
    Replies: 8
    Last Post: December 19th, 2012, 08:32 AM
  2. IOException while accesing files
    By Radha in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: November 29th, 2011, 06:46 AM
  3. What is wrong in the code
    By Rajiv in forum JavaServer Pages: JSP & JSTL
    Replies: 4
    Last Post: July 29th, 2011, 12:09 PM
  4. What is wrong with my code???
    By nine05 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 8th, 2011, 09:59 AM
  5. What's wrong with my code ?
    By mithani in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 5th, 2010, 08:57 AM