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 8385567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:42:12+00:00 2026-06-09T17:42:12+00:00

This is my code.. It can only write a line in a file when

  • 0

This is my code.. It can only write a line in a file when I press the button but, If I press It again with a new set of Characters, it only delete the old ones and store the new set of char. I don’t know how to make it write Characters in a different lines everytime I press the button without deleting the other lines

package datasaving;

import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.*;


public class Datasaving {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        JPanel panel = new JPanel();
        JFrame frame = new JFrame();
        final JTextField input = new javax.swing.JTextField(20);

        JButton save = new javax.swing.JButton("Write");

        frame.add(panel);
        frame.setSize(200,200);
        panel.add(input);
        panel.add(save);


        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setVisible(true);

        save.addActionListener(new ActionListener() {
                            @Override
                            public void actionPerformed(ActionEvent e) {
                                 File file = new File("data.dat");

    try {
            try (FileWriter writer = new FileWriter(file)) {

                writer.write(input.getText()+"\n");

            }
        System.out.println("Game saved");
    } catch (IOException | HeadlessException z) {
        JOptionPane.showMessageDialog(null, e);
    }

                            } 
                        });


    }
}
  • 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-09T17:42:13+00:00Added an answer on June 9, 2026 at 5:42 pm

    Wow, thats a lot happening in one statement. Not my favorite way to handle that, but that wasn’t your question.. 🙂 When you create a new FileWriter it creates a new file so you only see one line of text ever..

    Look at the API – there’s a constructor for FileWriter which takes in a boolean for whether to append or not.. You want to use that instead.

    Another option is to open the file once, and then just write to it each press.. If you want the file opened/closed on each write, then using the other ctor is the better way.

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

Sidebar

Related Questions

With this code I can download the file but I should know the file
Is there anyway this code can be refactored? The only difference is the order
Trying to figure out this pseudo code. The following is assumed.... I can only
example: at this domain http://www.example.com/234234/go.html is only one iframe-code how can i get the
This works when I have only one state code as a parameter. How can
With this code we can create a simple word document but I need to
How can I write only every third item in a char buffer to file
I have a file as a resource. I can only use read() , write()
I am using this code and it keeps only getting to the output file
Hey guys, I can only apologise for asking such a noob question, but this

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.