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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:39:44+00:00 2026-05-18T20:39:44+00:00

import java.io.FileNotFoundException; import java.lang.SecurityException; import java.util.Formatter; import java.util.FormatterClosedException; import java.util.NoSuchElementException; import javax.swing.JOptionPane; public class

  • 0
import java.io.FileNotFoundException;
import java.lang.SecurityException;
import java.util.Formatter;
import java.util.FormatterClosedException;
import java.util.NoSuchElementException;
import javax.swing.JOptionPane;

public class CreateTextFile {
    private Formatter output; //object used to output text to file
    private Order order;
    //enable user to open file 
    public void openFile()
    {
        try
        {
            output=new Formatter("src\\Orders.txt");//pen the file
        }//end try
        catch(SecurityException securityException)
        {
           JOptionPane.showMessageDialog(null,"You do not have the write access to this file.","Error", JOptionPane.ERROR_MESSAGE);
            System.exit(1);//terminate the program
        }//end catch
        catch(FileNotFoundException filenotfoundexception)
        {
           JOptionPane.showMessageDialog(null,"Error opening or creating file.","Error", JOptionPane.ERROR_MESSAGE);
            System.exit(1);//terminate the program
        }//end catch
    }//end method openFile

    //add records to file
    public void addOrderstoSalesmen(String Description,String Date, double poso,int salesman,Employee currentEmployee)
    {
        try //output values to file
        {
            //retrieve data to be ouput
                    order=new Order(Description,Date,poso,salesman);
                   Salesman employee=(Salesman) currentEmployee;

                    employee.addOrder(order);
                    employee.setGrossSales(salesman);
                    output.format("%s,%s,%.0f\n",order.getDescription(),order.getDate(),order.getSales());
        }//end try
        catch(FormatterClosedException formatterclosedexception)
        {
           JOptionPane.showMessageDialog(null,"Error writing to file.","Error", JOptionPane.ERROR_MESSAGE);
            return;
        }//end catch
        catch(NoSuchElementException elementexception)
        {
           JOptionPane.showMessageDialog(null,"Invalid input.Please try again.","Error", JOptionPane.ERROR_MESSAGE);
        }//end catch

    }//end method addRecords
    public void CloseFile()
    {
        if(output!=null)
            output.close();

    }//end method closeFile

}//end class CreateTextFile
  • 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-18T20:39:45+00:00Added an answer on May 18, 2026 at 8:39 pm

    You’re probably opening, writing, closing the file and then repeating. Each time you open a file for writing, its contents are wiped clean. You can get around this by either:

    1. Opening, writing, writing, …, writing and only closing the file once you’ve done all the writing.

    2. Open the file in append mode, so the old contents aren’t erased.

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

Sidebar

Related Questions

package jtextareatest; import java.io.FileInputStream; import java.io.IOException; import javax.swing.*; public class Jtextareatest { public static
Take the following generics example import java.util.List; import java.util.ArrayList; public class GenericsTest { private
import java.util.Scanner; public class Test { private static int decimalNum = 0; private static
import java.util.Arrays; public class Test { public static void main(String... args) { String[] strings
This is the code : import java.awt.*; import javax.swing.*; class tester { JFrame fr;
import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import java.io.FileReader; public class Main { public static void main(String[]
So I've got a JPanel implementing MouseListener and MouseMotionListener : import javax.swing.*; import java.awt.*;
This is the code : import java.io.*; class tester { public static void main(String
Using the standard java logging API (import java.util.logging.Logger), after the construction: Logger l =
Can you please explain why it is possible to do: import java.util.ArrayList; import java.util.List;

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.