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

  • Home
  • SEARCH
  • 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 8125583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:52:03+00:00 2026-06-06T06:52:03+00:00

I wrote this short program which has a tiny GUI. Its supposed to allow

  • 0

I wrote this short program which has a tiny GUI. Its supposed to allow you to type text and then you can save it to a txt file or read from a previously saved file or delete an existing file.
It works, but with some strange bugs. Sometimes after a file is created, if I try to delete it, it will not delete the file.

Also, Im totally not sure if I wrote the code in a good way.
I was wondering if you can have a look at what I wrote and find weaknesses or areas that I should write differently. And also maybe you can see why there is a problem with deleting the file after it was created. I’m really at a loss and I don’t know who to ask.
Here is the code I have:

public class InputOutout extends JFrame{

    private static final long serialVersionUID = -7073762217756427192L;

    JLabel label;
    JTextField tf;
    JButton buttonAdd;
    JButton buttonDisplay;
    JButton buttonErase;

    public InputOutout() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }

        setLayout(new FlowLayout());

        label = new JLabel("Enter text");
        add(label);

        tf = new JTextField(10);
        add(tf);

        buttonAdd = new JButton("Press to save to file");
        add(buttonAdd);

        buttonDisplay = new JButton("Press to display file content");
        add(buttonDisplay);

        buttonErase = new JButton("Press to erase file");
        add(buttonErase);

        eventAdd add = new eventAdd();
        eventDisplay remove = new eventDisplay();
        eventErase erase = new eventErase();

        buttonAdd.addActionListener(add);
        buttonDisplay.addActionListener(remove);
        buttonErase.addActionListener(erase);       
    }

    private class eventAdd implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            try {
                String word = tf.getText()+ " ";
                FileWriter stream = new FileWriter("text.txt",true);
                BufferedWriter out = new BufferedWriter(stream);
                out.append(word);
                out.close();
                tf.setText("");
            } catch (Exception ex){
                JOptionPane.showMessageDialog(null,ex.getMessage());
            }

        }
    }

    private class eventDisplay implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            try {
                FileReader stream = new FileReader("text.txt");
                BufferedReader in = new BufferedReader(stream);
                String text = in.readLine();
                JOptionPane.showMessageDialog(null, text);
                stream.close();
            } catch (Exception ex){
                JOptionPane.showMessageDialog(null,ex.getMessage());
            }

        }
    }

    private class eventErase implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            try {
                File file = new File("text.txt");
                boolean success = file.delete();

                if (!success) {
                    JOptionPane.showMessageDialog(null, "File was not deleted");
                } else {
                    JOptionPane.showMessageDialog(null, "File was deleted");
                }
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null,ex.getMessage());
            }
        }
    }

    public static void main(String[] args) {

        InputOutout gui = new InputOutout();

        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setSize(350,100);
        gui.setTitle("SuperWORD 2013");
        gui.setVisible(true);       
    }
}
  • 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-06-06T06:52:05+00:00Added an answer on June 6, 2026 at 6:52 am

    The logic surrounding the file.delete() is not quite correct, as true will only be returned if it is deleted. This means that file.delete() will return false if it does not exist. Check if the file exists before attempting to delete it:

    File file = new File("text.txt");
    if (file.exists())
    {
        if (!file.delete())
        {
            JOptionPane.showMessageDialog(null, "File was not deleted");
        }
        else
        {
            JOptionPane.showMessageDialog(null, "File was deleted");
        }
    }
    else
    {
        JOptionPane.showMessageDialog(null, "File does not exist");
    }
    

    Recommend using a try {} catch() finally {} construct to ensure BufferedReader.close(), or BufferedWriter.close() is called:

    try
    {
        BufferedWriter out = new BufferedWriter(new FileWriter("text.txt",true));
        try
        {
            String word = tf.getText()+ " ";
            out.append(word);
            tf.setText("");
        }
        finally
        {
            try
            {
                out.close();
            }
            catch (IOException ex)
            {
                // Report close failure.
                JOptionPane.showMessageDialog(null, ex.getMessage());
            }
        }
    }
    catch (Exception ex)
    {
        // Report open or write failure.
        JOptionPane.showMessageDialog(null,ex.getMessage());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a short C++ program to do XOR encryption on a file, which
To help me better understand lambda I wrote this short snippet that rotates and
I have a short question i have wrote this in java. Old code: class
I wrote this program: #include <stdio.h> /*Part B Write a program that: defines an
My program uses a NetworkOutput object which can be used to write data to
I have a program which scans trough a textbox1 text and displays all words
The code I'm working with has its own smart pointer implementation which does simple
I wrote this code for zoom in/out . it works but even with one
I wrote this as a simple dice game. It works as I want except
I wrote this code for zoom in / out and it suppesed to only

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.