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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:59:03+00:00 2026-05-20T11:59:03+00:00

I got a folder called DIR and a subfolder called SUB. the java file

  • 0

I got a folder called DIR and a subfolder called SUB.
the java file i am running is placed in the DIR folder, and now i want to save my small string in a .txt file in the SUB folder.

Could you help me?

  • 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-20T11:59:03+00:00Added an answer on May 20, 2026 at 11:59 am
    PrintWriter out = new PrintWriter(
        new OutputStreamWriter(
           new FileOutputStream("SUB/myfile.txt")));
    out.println("simpleString);
    out.close();
    

    This simple task seems very complicated in Java. The reason is, that you can configure everything on the way from the String to the file. You could e.g. specifiy the encoding of the output file, which defaults to platform encoding (e.g. “win1252” for Windows in most parts of the western hemisphere). To write UTF-8 files use:

    PrintWriter out = new PrintWriter(
        new OutputStreamWriter(
            new FileOutputStream("SUB/myfile.txt"),"UTF-8"));
    out.println("simpleString);
    out.close();
    

    The PrintWriter also has a “flush immediately” option, which is nice for logfiles you want to monitor in the background.

    By chaining the IO constructors, you can also increase performance when using a BufferedWriter, because this flushes only to the disk, when a minimum size is reached, or when the stream is closed.

    PrintWriter out = new PrintWriter(
       new BufferedWriter(
           new OutputStreamWriter(
               new FileOutputStream("SUB/myfile.txt"),"UTF-8")));
    out.println("simpleString);
    out.close();
    

    By having to create a FileOutputStream you specify the output should go to the disk, but you can also use any other streamable location, like e.g. an array:

    byte[] bytes = new byte[4096];
    OutputStream bout = new ByteArrayOutputStream(bout);
    PrintWriter out = new PrintWriter(
        new OutputStreamWriter(bout,"UTF-8"));
    out.println("simpleString);
    out.close();
    

    You will love Javas versatility!

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

Sidebar

Related Questions

I am trying to copy a file into a folder called profile. I got
So I've got a nice Java project, build with Ant to a /dist folder.
I have a folder called Images inside my IDE and I want to access
I've got a folder on my server called dvd and I've put an index
I have a list of PDF [myfile.pdf] files on a folder/dir called example.com/uploaded/downloads/ I
I've got a couple large checkouts where the .svn folder has become damaged so
I've got about 3 million files I need to copy from one folder to
Got a bluescreen in windows while cloning a mercurial repository. After reboot, I now
I'm quite new in C++ after few years in Java and eclipse, I got
Has anyone got EclipseLink MOXy (I'm using eclipselink 2.1.0) to work with Java 5?

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.