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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:40:04+00:00 2026-06-06T11:40:04+00:00

I have a file (file.txt), and I need to empty his current content, and

  • 0

I have a file (file.txt), and I need to empty his current content, and then to append some text multiple times.

Example: file.txt current content is:

aaa

bbb

ccc

I want to remove this content, and then to append the first time:

ddd

The second time:

eee

And so on…

I tried this:

// empty the current content
fileOut = new FileWriter("file.txt");
fileOut.write("");
fileOut.close();

// append
fileOut = new FileWriter("file.txt", true);

// when I want to write something I just do this multiple times:
fileOut.write("text");
fileOut.flush();

This works fine, but it seems inefficient because I open the file 2 times just for remove the current content.

  • 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-06T11:40:06+00:00Added an answer on June 6, 2026 at 11:40 am

    When you open up the file to write it with your new text, it will overwrite whatever is in the file already.

    A good way to do this is

    // empty the current content
    fileOut = new FileWriter("file.txt");
    fileOut.write("");
    fileOut.append("all your text");
    fileOut.close();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a template file (say myTemplate.txt) and I need to make some edits
i have stored the txt file to sql server database . i need to
What I have is a txt file that is huge, 60MB. I need to
I have a text file (txt) containing formatted text (just line breaks, carriage returns
I have zip file inside a Text file (file.txt inside a file.zip) and I
I need help about how to numeration text in file. I have also linux
I have a file name abc.txt . On that file have some lines like
I have a text file named abc.txt . The file have info like this
Ok so I have a text file that will change regularly that I need
I have a txt file as a string, and I need to find words

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.