Results 1 to 4 of 4
  1. #1
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default How do you make a file save with a default file extension?

    Alright I'm attempting to make a simple notepad clone, just to expand my knowledge. Everything I have works so far, but I'm trying to figure out that when I use my save as menu item how to make the file automatically save as a .txt. Just for clarification, I don't want the user to have to type in .txt to save it as a text file. I know in some programs it starts default as *.txt, this method would be acceptable also. However I cannot figure out how to do either one of them.

    Here is how my save menu listener is setup.

    Java Code:
    public class saveMenuListener implements ActionListener
    {
         public void actionPerformed(ActionEvent ev)
         {
               JFileChooser fileSave = new JFileChooser();
               fileSave.showSaveDialog(frame);
               saveFile(fileSave.getSelectedFile());
         }
    }
    
    private void saveFile(File file)
    {
         try
         {
              Buffered Writer writer = new BufferedWriter(new FileWriter(file));
              writer.write(textArea.getText());
              writer.close()
         } catch(IOException ex) {
              System.out.println("Couldn't write to file.");
              ex.printStackTrace();
         }
    }
    I've been looking through google and I haven't found a solution yet.
    Last edited by Dark; 06-21-2011 at 04:15 PM.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    Just append .txt to the file name after the user enters the name. You could also validate the user entered name and see if it does end in .txt, if not append the extension.
    Java Code:
    File file = new File(userEnteredFile.getAbsolutePath() + ".txt");

  3. #3
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Hmm... That seems like a useable idea. Brainstorm time, what if the file was able to be saved in .rtf or .txt? How could I accomplish this without limiting the file extension can save too. You know how when you use saveas there is the file type drop down window, how can I set those options? I guess that's my ultimate goal.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  4. #4
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    I think I found what I was trying to do, however it is a little more advanced that I originally thought so I am not going to attempt it just yet. I implemented doWhile's method and it works just fine with a little personal tweaking.

    However this thread right here may solve other peoples problems if they have a similar question and that is why I'm including it.
    java - How to save file using JFileChooser?? - Stack Overflow
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

Similar Threads

  1. save file based on file extension
    By masa in forum AWT / Swing
    Replies: 4
    Last Post: 05-11-2010, 12:17 PM
  2. Replies: 2
    Last Post: 03-22-2010, 09:55 AM
  3. how to save Interface (file extension)
    By shaggyoo7 in forum New To Java
    Replies: 9
    Last Post: 01-10-2009, 10:07 AM
  4. Replies: 0
    Last Post: 04-05-2008, 11:13 AM
  5. Replies: 1
    Last Post: 08-07-2007, 06:09 AM

Posting Permissions

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