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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:05:56+00:00 2026-05-29T04:05:56+00:00

I’m learning java and I have made simple program that simply reads value from

  • 0

I’m learning java and I have made simple program that simply reads value from JTextField and saves it to file using FileOutputStream.
My question is: is it normal for data to be unreadable (using same program with FileInputStream) after restarting it? If i read it without terminating program it works fine.
How can I make data wrote to file permament?
Edit:
It seems the file is being cleaned when starting the program.
Here is the code:

public class Test extends JFrame
{
JTextField field;
JButton write;
JButton read;
File file;
FileOutputStream fOut;
FileInputStream fIn;
int x;

Test() throws IOException
{
    setAlwaysOnTop(true);
    setLayout(new BorderLayout());
    field = new JTextField(4);
    write = new JButton("Write");
    read = new JButton("Read");
    file = new File("save.txt");
    if(!file.exists())
    {
        file.createNewFile();
    }
    fOut = new FileOutputStream(file);
    fIn = new FileInputStream(file);
    add(field);
    add(write, BorderLayout.LINE_START);
    add(read, BorderLayout.LINE_END);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(160,60);
    write.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            x = Integer.parseInt(field.getText());
            try
            {
                fOut.write(x);
                System.out.println("Saving completed.");
                fOut.flush();
            }
            catch(Exception exc)
            {
                System.out.println("Saving failed.");
            }

        }
    });
    read.addActionListener(new ActionListener() 
    {
        public void actionPerformed(ActionEvent e) 
        {
            try
            {
                x = fIn.read();
                fIn.close();
            }
            catch(Exception exc)
            {
                System.out.println("Reading failed.");
            }
        }
    });
}
public static void main(String[] args) throws IOException
{
    new Test();
}
}
  • 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-29T04:05:57+00:00Added an answer on May 29, 2026 at 4:05 am

    This fOut = new FileOutputStream(file); will overwrite the file, you need to use fOut = new FileOutputStream(file, true); to append to it.

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I have thousands of HTML files to process using Groovy/Java and I need to
I have a reasonable size flat file database of text documents mostly saved in
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a bunch of posts stored in text files formatted in yaml/textile (from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.