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

  • Home
  • SEARCH
  • 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 9228479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:20:02+00:00 2026-06-18T05:20:02+00:00

Could you possible help me solving the next problem? I’m trying to use PrintStream

  • 0

Could you possible help me solving the next problem?
I’m trying to use PrintStream with my Threads to make them writing something to different outputs.
The problem is: System.out works perfect, but when I try to add

File f1 = new File("src/task7/simple/1.txt"); 
PrintStream filePRinPrintStream = new PrintStream(f1)

my Thread doesn’t want to write anything, it throws no warning or exception while ordinary printing is main() method is OK.

filePRinPrintStream.println("PREVED");

Here are my code extracts for your information.

public class NamePrinterThread implements Runnable, NamePrinterIF{

    private String name;
    private PrintStream outputStream;
    private long interval;
    private int count;

    @Override
    public void setPrintName(String name) {
        this.name = name;
    }

    @Override
    public void setStream(PrintStream stream) {
        if(stream == null) throw new NullPointerException("PrintStream is null");
        this.outputStream = stream;
    }

    @Override
    public void setInterval(long ms) {
        if(ms <= 0) throw new IllegalArgumentException("Printing interval can't be negative");
        this.interval = ms;
    }

    @Override
    public void setCount(int count) {
        if(count <= 0) throw new IllegalArgumentException("Printing count can't be negative");
        this.count = count;
    }

    @Override
    public void run() {
        for (int i = 0; i < this.count; i++) {
            try {
                outputStream.println(this.name);
                Thread.sleep(interval);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }       
    }

    public void startThread(){
        new Thread(this, name).start();
    }

}

and main method from another class

public static void main(String[] args) {

        File f1 = new File("src/task7/simple/1.txt");

        try(PrintStream filePRinPrintStream = new PrintStream(f1)){ 

            //filePRinPrintStream.println("PREVED");
            NamePrinterIF thread2 = new NamePrinterThread();
            thread2.setCount(20);
            thread2.setInterval(350);
            thread2.setStream(filePRinPrintStream);
            thread2.setPrintName("thread2");

            thread2.startThread();

            filePRinPrintStream.flush();
        } catch(IOException e){
            e.printStackTrace();
        }

        NamePrinterIF thread1 = new NamePrinterThread();
        thread1.setCount(10);
        thread1.setInterval(200);
        thread1.setStream(System.out);
        thread1.setPrintName("thread1");

        thread1.startThread();
    }
  • 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-18T05:20:04+00:00Added an answer on June 18, 2026 at 5:20 am

    Close your printstream in main if you are done.
    It would be a good idea if the main terminates after all your threads have terminated, and that you cleanly close the stream then.

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

Sidebar

Related Questions

Possible Duplicate: Need help solving Project Euler problem 200 Similar to this question Project
Could anyone tell me if it is possible to use the flex 4 framework
Excuse me, how could I possible do something like this? I have a DIV,
I need your help in solving the following problem: There're 3 girls (Ann,Susan,Alice) who
Maybe I'm having a really bad day, but could someone possibly help me to
I would like to know if it could be possible to add a file
One possible solution could be simple HTTP get or post request, but that wouldn't
Possible Duplicate: Could anyone explain these undefined behaviors (i = i++ + ++i ,
Possible Duplicate: Could anyone explain these undefined behaviors (i = i++ + ++i ,
I am curious to know that could it be possible that in the below

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.