Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7020329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:18:17+00:00 2026-05-27T23:18:17+00:00

I have a method that get text from a JTextArea, create a file and

  • 0

I have a method that get text from a JTextArea, create a file and write text on it as code below:

public void createTxt() {

    TxtFilter txt = new TxtFilter();

    JFileChooser fSave = new JFileChooser();

    fSave.setFileFilter(txt);
    int result = fSave.showSaveDialog(this);
    if(result == JFileChooser.APPROVE_OPTION) {
        File sFile = fSave.getSelectedFile();
        FileFilter selectedFilter = fSave.getFileFilter();

        String file_name = sFile.getName();
        String file_path = sFile.getParent();

        try{
            if(!sFile.exists()) {
                sFile.createNewFile();
                BufferedWriter out = new BufferedWriter(new FileWriter(sFile));
                out.write(jTextArea1.getText());
                out.close();
                JOptionPane.showMessageDialog(null, "Warning file • " + file_name + " • created succesfully in \n" + file_path);    
            } else {
                String message = "File • " + file_name + " • already exist in \n" + file_path + ":\n" + "Do you want to overwrite?";
                String title = "Warning";
                int reply = JOptionPane.showConfirmDialog(null, message, title, JOptionPane.YES_NO_OPTION);
                if(reply == JOptionPane.YES_OPTION){
                    sFile.delete();
                    sFile.createNewFile();
                    BufferedWriter out = new BufferedWriter(new FileWriter(sFile));
                    out.write(jTextArea1.getText());
                    out.close();
                    JOptionPane.showMessageDialog(null, "File • " + file_name + " • overwritten succesfully in \n" + file_path);

                }
            }
        }
       catch(IOException e) {
           System.out.println("Error");
       }
    }
}

and a txt file filter

public class TxtFilter extends FileFilter{
    @Override
    public boolean accept(File f){
        return f.getName().toLowerCase().endsWith(".txt")||f.isDirectory();
    }
    @Override
    public String getDescription(){
        return "Text files (*.txt)";
    }
}

The file filter for txt works fine but what I want is to add “.txt” extension when I type file name.

How to I have to modify my code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:18:17+00:00Added an answer on May 27, 2026 at 11:18 pm

    UPDATE

    You pointed me out that the check for existing files doesn’t work. I’m sorry, I didn’t think of it when I suggested you to replace the BufferedWriter line.
    Now, replace this:

    File sFile = fSave.getSelectedFile();
    

    with:

    File sFile = new File(fSave.getSelectedFile()+".txt");
    

    With this replacement, it isn’t now needed to replace the line of BufferedWriter, adding .txt for the extension. Then, replace that line with the line in the code you posted (with BufferedWriter out = new BufferedWriter(new FileWriter(sFile)); instead of BufferedWriter out = new BufferedWriter(new FileWriter(sFile+".txt"));).

    Now the program should work as expected.

    I forgot to mention that you have to comment the line:

    sFile.createNewFile();
    

    In this way, you’re creating an empty file, with the class File.

    Just after this line, there is: BufferedWriter out = new BufferedWriter(new FileWriter(sFile));.
    With this line, you are creating again the same file. The writing procedure is happening two times! I think it’s useless to insert two instructions that are doing the same task.

    Also, on the BufferedWriter constructor, you can append a string for the file name (it isn’t possible on File constructor), that’s the reason why I added +".txt" (the extension) to sFile.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code in method C that will get executed based upon the
I have a program that makes use of the following method to get a
I have an application (ASP.NET MVC) that uses a Next New method to get
I Have activity that get some data from the internet, and shows it to
I want to get text from file, and show it on TextView sorted. I
I was reviewing some of my old code and I have a method that
I have to write some sort of parser that get a String and replace
I have a method that currently returns a string converted from a byte array:
I have code similar to the below <form action=/u method=post> <div class=reply> <input type=hidden
I have an object that I can get a String from during debugging. That

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.