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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:15:21+00:00 2026-05-31T16:15:21+00:00

I cant append text to a text file, it only overwrites the previous text.

  • 0

I cant append text to a text file, it only overwrites the previous text. My code:

//using JFileChooser to select where to save file
PrintStream outputStream = MyFrame.ShowSaveDialog();
    if(outputStream!=null){
        outputStream.append(input);
        outputStream.close();
    } 

Edited:
The ShowSaveDialog returns a PrintStream. Here is the code for that method:

public static PrintStream ShowSaveDialog(){
    JFileChooser chooser = new JFileChooser();
    FileNameExtensionFilter filter = new FileNameExtensionFilter(
            "Tekst filer", "txt");
    chooser.setFileFilter(filter);

    int returnVal = chooser.showSaveDialog(null);
    try{
        if(returnVal == JFileChooser.APPROVE_OPTION){

            return new PrintStream(chooser.getSelectedFile());              
        }
        else{
            return null;
        } 
    }
    catch(FileNotFoundException e){
        JOptionPane.showMessageDialog(null, "Ugyldig Fil!",
                   "error", JOptionPane.ERROR_MESSAGE);
    }
    return null;

}
  • 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-31T16:15:22+00:00Added an answer on May 31, 2026 at 4:15 pm

    What does MyFrame.ShowSaveDialog(); return? The key is to create a FileOutputStream with the appropriate constructor (the second parameter should be the boolean true) which will make it an appending FileOutputStream, and then construct your PrintStream using this FileOutputStream object.

    For instance, if showSaveDialog() (note that method and variable names should begin with lower case letters) returns the name of a file or a File object, you could do something like so:

    try {
      File file = myFrame.showSaveDialog(); // if this method returns a File!!!!!
      FileOutputStream fos = new FileOutputStream(file, true);
      PrintStream printStream = new PrintStream(fos);
      //.... etc
    } catch(....) {
      // ....
    }
    

    Edit:
    To apply this to your posted code above, do something like so:

       public static PrintStream showSaveDialog() {
          JFileChooser chooser = new JFileChooser();
          FileNameExtensionFilter filter = new FileNameExtensionFilter(
                "Tekst filer", "txt");
          chooser.setFileFilter(filter);
    
          int returnVal = chooser.showSaveDialog(null);
          try {
             if (returnVal == JFileChooser.APPROVE_OPTION) {
    
                //  ******* note changes below *****
                File file = chooser.getSelectedFile();
    
                FileOutputStream fos = new FileOutputStream(file, true);
                return new PrintStream(fos);
             } else {
                return null;
             }
          } catch (FileNotFoundException e) {
             JOptionPane.showMessageDialog(null, "Ugyldig Fil!", "error",
                   JOptionPane.ERROR_MESSAGE);
          }
          return null;
    
       }
    

    The crux would be these lines here:

                File file = chooser.getSelectedFile();
                FileOutputStream fos = new FileOutputStream(file, true);
                return new PrintStream(fos);
    

    The true in the FileOutputStream constructor creates a FileOutputStream that appends to the existing file. Please check out the FileOutputStream API for the details on this.

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

Sidebar

Related Questions

Ive got an annoying problem, I cant append any text to the text file.
I am trying to append a css file using $('head').append('<link rel=stylesheet href=style2.css type=text/css />');
I have this code: <script type=text/javascript> $(document).ready(function() { $(#iframeId).contents().find(body).append($(#test)); }); </script> <iframe id=iframeId name=iframeId
I'm new to python and I'm writing a code to upload a file using
I have some Python code that pulls strings out of a text file: [2.467188005806714e-05,
I cant post the code (proprietary issues) but does anyone know what types of
Why cant i use code nuggets to set a control property??For example a validationgroup
I am trying to serialize a list of dictionaries to a csv text file
the following script text.pl (described below) define to append the $insert text between $first_line
Is there a simple way to export a WPF DataGrid to a text file

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.