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 5 of 5

Thread: How to add file extension to JFileChooser.showSaveDialog() ?

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to add file extension to JFileChooser.showSaveDialog() ?

    I'm building this basic, basic text editor. And the saving works fine, but I don't know how to automatically add a file extension to the output. Here's what the method looks like so far

    private void saveActionPerformed(java.awt.event.ActionEvent evt) {                                     
     
            JFileChooser c = new JFileChooser();
            int x = c.showSaveDialog(null);
            if (x == JFileChooser.APPROVE_OPTION) {
                try {
     
                    String s = textAREA.getText();
                    BufferedWriter file = new BufferedWriter(new FileWriter(c.getSelectedFile()));
                    file.write(s);
                    file.close();
     
     
     
                } catch (IOException ex) {
                    Logger.getLogger(Window.class.getName()).log(Level.SEVERE, null, ex);
                }
            }


    how can I add a .txt to the file once u save it without typing it in myself?

    --thanks for all the help


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How to add file extension to JFileChooser.showSaveDialog() ?

    Simply modify the file object you get back:

    File f = c.getSelectedFile();
    String filePath = f.getPath();
    if(!filePath.toLowerCase().endsWith(".txt"))
    {
        f = new File(filePath + ".txt");
    }
    // create your buffered writer from f

  3. #3
    Junior Member
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to add file extension to JFileChooser.showSaveDialog() ?

    I guess that works. But other than appending the file path. Isn't there like a graphical option. When you save a word document it lets you chose the file type at the bottom "File Format: All Files". I know there's file filters in openDialog so I'm assuming there's something alike that for saving.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How to add file extension to JFileChooser.showSaveDialog() ?

    Yep, there is.

    See: http://www.javaprogrammingforums.com...e-filters.html
    However, I'm not sure if it will automatically add the extension on saves, if not you'll need to get the current file filter selected using getFileFilter() and attaching on the appropriate ending using the method above.

  5. #5
    Junior Member
    Join Date
    May 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to add file extension to JFileChooser.showSaveDialog() ?

    someone do it in a "rather" complex way, but works as you want...
    check in this link java - How to save file using JFileChooser? - Stack Overflow

Similar Threads

  1. Replies: 10
    Last Post: January 12th, 2011, 05:48 AM
  2. Can't get it to save stuff with JFileChooser
    By javapenguin in forum What's Wrong With My Code?
    Replies: 35
    Last Post: August 11th, 2010, 05:13 AM
  3. Using JFileChooser to open and display an image
    By JavaN0ob in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 31st, 2010, 07:01 PM
  4. JFileChooser
    By FretDancer69 in forum AWT / Java Swing
    Replies: 2
    Last Post: February 3rd, 2010, 06:35 AM
  5. Replies: 2
    Last Post: March 6th, 2009, 03:00 PM