Results 1 to 5 of 5
  1. #1
    victorgb is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default FileWriter issue

    Hi everyone, I have a problem and need some help.

    As I've understood id, the following code creates a file named "filename":
    PrintWriter write = new PrintWriter(new BufferedWriter(new FileWriter("filename")));

    And what I want to know is how I can detect if there allready exists a file named "filename".
    And if so, how to have a messagedialog pop up saying that there allready exists a file with that name.

    Thanks in advance. (:

  2. #2
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: FileWriter issue

    FileWriter will throw an IOException if the file does not exist (see FileWriter (Java Platform SE 6))

    You can use the "catch" to show the dialog box.

  3. #3
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: FileWriter issue

    I was partially incorrect about that. If the file can be created, it won't throw an IOException.

  4. #4
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: FileWriter issue

    You can use this technique to check if a file exists:

    Java Code:
    File f = new File("myFile.txt");
        if (!f.exists())  {
            // Code for your error message here.		
        }
    
        PrintWriter write = new PrintWriter(new BufferedWriter(fw));  // Edited to correct using fw instead of f.
    You can learn how to do message dialogs from this tutorial Home Page > Creating a GUI With JFC/Swing > Using Swing Components > How to Make Dialogs
    Last edited by jlczuk; 04-18-2012 at 07:37 PM.

  5. #5
    victorgb is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default Re: FileWriter issue

    Okey, it worked, thanks a lot for the help. :D

Similar Threads

  1. Initializing FileWriter
    By loopsnhoops in forum Advanced Java
    Replies: 1
    Last Post: 05-31-2011, 02:28 AM
  2. FileWriter error
    By bczm8703 in forum JCreator
    Replies: 0
    Last Post: 03-04-2011, 04:10 AM
  3. New line in FileWriter
    By luc@$ in forum New To Java
    Replies: 3
    Last Post: 01-31-2011, 04:45 AM
  4. BufferedWriter, FileWriter
    By ladykrimson in forum New To Java
    Replies: 4
    Last Post: 12-06-2010, 02:35 AM
  5. issue with FileReader and FileWriter
    By Uma R in forum Advanced Java
    Replies: 6
    Last Post: 07-16-2010, 01:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •