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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:14:39+00:00 2026-05-25T10:14:39+00:00

After running my program I get this beautifully formatted text: What do I need

  • 0

After running my program I get this beautifully formatted text:enter image description here

What do I need add to my code in order to get this text outputted to a .txt file exactly as is?

  • 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-25T10:14:39+00:00Added an answer on May 25, 2026 at 10:14 am

    Assuming you’re currently writing your output to the screen using something like System.out.Println(...), a comparable way to write text to a file is to use a PrintStream.

    You should be able to find many examples of how to do that if you search; here’s one.

    import java.io.*;
    
    class PrintStreamDemo {  
      public static void main(String args[]){ 
       FileOutputStream out; 
      PrintStream ps; // declare a print stream object
      try {
     // Create a new file output stream
      out = new FileOutputStream("myfile.txt");
    
      // Connect print stream to the output stream
      ps = new PrintStream(out);
    
      ps.println ("This data is written to a file:");
      System.err.println ("Write successfully");
      ps.close();
      }
      catch (Exception e){
      System.err.println ("Error in writing to file");
      }
      }
    }
    

    ps. one caution: the “beautifully formatted” part of your text probably relies on the output being displayed in a monospaced font (all characters the same width); if viewed in a non-monospaced font, the columns won’t line up. If you’re saving it as a plain .txt file, you don’t have control over what font someone else will use to display that file when they open it.


    Update:

    There are a couple of approaches you could take, if you find you’ve got a program full of System.out.println calls and you want to direct the output to a file instead.

    1) The quick & dirty way would be to open a PrintStream to your desired output file, then call System.setOut() to redirect System.out to the specified PrintStream.

    2) A perhaps cleaner way would be to rewrite all the calls of System.out.println to use your own output method. That way when you want to change how you’re handling output (for example, send it to multiple files, to both the screen and a file, or whatever), you have just one place to change. More work up front, but gives you more flexibility in the end.

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

Sidebar

Related Questions

While running my program I get this error: terminate called after throwing an instance
Similar to this question , after running the following code the browser dialog does
Is there an easy way to get a conflict summary after running a cvs
We use Sajax for adding small Ajax code to sites. After running into a
My cron job is running an hour later after end of DST. This didn't
For some reason, I can't get this program to work. I've had other CS
I am trying to get OpenGL and Glut running on Eclipse Linux FC13. After
After running a insert or update query against a SQLServer 2005 database, under what
After running some usability tests, I found that participants opened a jQuery Lightbox to
How do you automatically start a service after running an install from a Visual

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.