简体   繁体   English

如何使用PrintWriter和flush()在文本文件上打印内容?

[英]How to use PrintWriter and flush() to print something on a text file?

I am using Multi-threading to calculate an image. 我正在使用多线程计算图像。 Every Thread calculates a line, when a thread is already calculating a line , should the next thread calculate the line after that one . 每个线程计算一条线,当一个线程已经计算的线路 ,如果一个线程计算出一个后线。 But I want to be sure that every line get calculated JUST one time, and to make that, I could make a System.out.println(CalculatedLineNumber) and make the output in a text file, so that when i open it with a text editor, i will directly see if the number of lines printed is the same as the one on the textfile. 但是我想确保每一行都只计算一次,要做到这一点,我可以制作一个System.out.println(CalculatedLineNumber)并在文本文件中进行输出,以便当我用文本打开它时编辑器,我将直接查看打印的行数是否与文本文件上的行数相同。 But how should I do that? 但是我该怎么做呢? Here is my code fragment for the run() method where the calculating is done: 这是我的run()方法的代码片段,其中的计算已完成:

public void run() {

                int myRow;
                while ( (myRow = getNextRow()) < getHeight() ) {
                    image.setRGB(0, myRow, getWidth(), 1, renderLine(myRow), 0, 0);
                }
            }

Someone told me that I should use a PrintWriter and flush() or something like that, but i dont know how to use that.. Could anyone help me with that? 有人告诉我,我应该使用PrintWriter和flush()之类的东西,但是我不知道该如何使用。有人可以帮助我吗? ("myRow" is the line number that i want to be writed on the text file, and everyone in a different line) (“ myRow”是我要写在文本文件上的行号,每个人都在不同的行上)

Thankyou so much!! 非常感谢!!

I want to be sure that every line get calculated JUST one time, 我想确保每一行都只计算一次,

I would suggest that you use a ExecutorService and submit each row as a image job to a pool of threads. 我建议您使用ExecutorService ,并将每行作为映像作业提交到线程池。 See the bottom for a code sample. 请参阅底部的代码示例。 If you do this right then you don't need to worry about how many output lines there are going to be. 如果正确执行此操作,则无需担心会有多少条输出线。

I could make a System.out.println(CalculatedLineNumber) 我可以做一个System.out.println(CalculatedLineNumber)

I don't quite understand the need for this. 我对此不太了解。 Is this some sort of accounting file to help you ensure that all of the images have been processed? 这是某种会计文件,可以帮助您确保所有图像都已处理吗?

Someone told me that I should use a PrintWriter and flush() 有人告诉我,我应该使用PrintWriter和flush()

You don't need to flush a PrintWriter since it is already synchronized underneath. 您不需要flush PrintWriter因为它已经在下面同步了。 Just print out the results at the end of each job and if you submitted X row jobs to your threadPool then you will have X lines of output. 只需在每个作业的末尾打印出结果,如果您将X行作业提交到threadPool ,则将有X行输出。

All you need to do to use PrintWriter is: 使用PrintWriter所需要做的就是:

PrintWriter printWriter = new PrintWriter(new File("/tmp/outputFile.txt"));
// each thread can do:
writer.println("Some sort of output: " + myRow);

Here's some sample code to show how to use ExecutorService thread pools. 这是一些示例代码,以显示如何使用ExecutorService线程池。

PrintWriter outputWriter = ...;
// create a thread pool with 10 workers
ExecutorService threadPool = Executors.newFixedThreadPool(10);
// i'm not sure exactly how to build the parameter for each of your rows
for (int myRow : rows) {
    // something like this, not sure what input you need to your jobs
    threadPool.submit(new ImageJob(outputWriter, myRow, getHeight(), getWidth()));
}
// once we have submitted all jobs to the thread pool, it should be shutdown
threadPool.shutdown();
...
public class ImageJob implements Runnable {
    private PrintWriter outputWriter;
    private int myRow;
    private int height;
    private int width;
    public MyJobProcessor(PrintWriter outputWriter, int myRow, int height,
            int width, ...) {
        this.outputWriter = outputWriter;
        this.myRow = myRow;
        this.height = height;
        this.width = width;
    }
    public void run() {
        image.setRGB(0, myRow, width, 1, renderLine(myRow), 0, 0);
        outputWriter.print(...);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM