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: FileWrite Over-Writing txt Files. D:

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

    Default FileWrite Over-Writing txt Files. D:

    Is There anyway to prevent this from happening?

        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 (Exception e) {
                e.printStackTrace();
        }


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: FileWrite Over-Writing txt Files. D:


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

    Default Re: FileWrite Over-Writing txt Files. D:

    Throws: IOException ?

  4. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: FileWrite Over-Writing txt Files. D:

    Yes, the FileWriter constructors that take a File or String arguments can all throw an IOException. The documentation I linked to explains when - eg you specify a directory rather than a file, or you don't have file write permissions etc.

    This is over and above the FileNotFoundException which you are already dealing with. You have to deal with IOException as well.

    At the moment you are catching a general Exception. If you want to deal specifically with the IOException, do so after FileNotFoundException because IOException is more general.

    -----

    Or do you mean your program is throwing an IOException at runtime? If so, post the code you are currently using and the full stack trace.

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

    Default Re: FileWrite Over-Writing txt Files. D:

    Like i'm making a dashboard for logging in to a another window
    i don't know if it should be before are after, what do you think?

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

    Default Re: FileWrite Over-Writing txt Files. D:

    Quote Originally Posted by pbrockway2 View Post
    Yes, the FileWriter constructors that take a File or String arguments can all throw an IOException. The documentation I linked to explains when - eg you specify a directory rather than a file, or you don't have file write permissions etc.

    This is over and above the FileNotFoundException which you are already dealing with. You have to deal with IOException as well.

    At the moment you are catching a general Exception. If you want to deal specifically with the IOException, do so after FileNotFoundException because IOException is more general.

    -----

    Or do you mean your program is throwing an IOException at runtime? If so, post the code you are currently using and the full stack trace.
    sorry i read that wrong, no its working but it writesover like

    username: lol
    password: lol

    If i login with a password like lol2 the txt file will say
    username: lol
    password: lol2

  7. #7
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: FileWrite Over-Writing txt Files. D:

    What code are you using now?

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

    Default Re: FileWrite Over-Writing txt Files. D:

    Same Code I Posted Earlier.

    Im not sure how to add a throw? o.o

  9. #9
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: FileWrite Over-Writing txt Files. D:

    Then my advice remains what it was: use an appropriate FileWriter consructor.

    Did you read the documentation I linked to? Say if there's anything there that isn't clear. That form of the constructor will work with your current code because you are already catching Exception (which means basically anything that can go wrong.

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

    Default Re: FileWrite Over-Writing txt Files. D:

    Yes but im using it for a button and not a class

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,610
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: FileWrite Over-Writing txt Files. D:

    Continued on a new thread:
    What Wrong With My Code: IOException.

Similar Threads

  1. Reading In Then Writing Out Massive Files
    By aussiemcgr in forum Java Theory & Questions
    Replies: 3
    Last Post: July 28th, 2011, 07:00 PM
  2. Writing to files
    By nitwit3 in forum Java Theory & Questions
    Replies: 3
    Last Post: July 25th, 2011, 04:00 AM
  3. Seraching through files in a folder for a pattern match inside the files.
    By dazzabiggs in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 2nd, 2011, 08:35 AM
  4. Replies: 1
    Last Post: March 22nd, 2011, 06:59 PM
  5. Reading and Writing Text Files
    By kappasig84 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 1st, 2010, 06:16 PM