简体   繁体   中英

PrintWriter flush is not working on Windows Server 2012

I use PrintWriter to write file with autoFlush = true: PrintWriter pw = new PrintWriter(new FileOutputStream(file), autoFlush);

Without calling pw.flush(), every time I call pw.println(...), content is write to output file.

It work fine on Win7 and Win Server 2008 but not Win Server 2012. I tried using same JDK, for Win Server 2012 env, file only flush out after pw.close() is called in finally block (eg reach the end of program or exception happen).

According to javadoc: https://docs.oracle.com/javase/7/docs/api/java/io/Writer.html#flush()

If the intended destination of this stream is an abstraction provided by the underlying operating system, for example a file, then flushing the stream guarantees only that bytes previously written to the stream are passed to the operating system for writing; it does not guarantee that they are actually written to a physical device such as a disk drive.

Seem like OS issue. Any help on that?

Here the code:

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;

public class TestPW {
    public static void main(String[] args) throws Exception {

        File file = null;
        PrintWriter pw = null;

        try {
            boolean autoFlush = true;

            file = new File("C:\\NotBackedUp\\test.txt");
            pw = new PrintWriter(new FileOutputStream(file), autoFlush);

            int loop = 100000000;

            while (loop > 0) {
                pw.println("Test: " + loop);
                loop--;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (pw != null) {
                    pw.close();
                }
            } catch (Exception e) {

            }
        }

    }
}

The workaround is close the pw after some number of loop processed and re-init the pw object again as below....

import java.io.*;

public class TestPSMaxLine {
    public static void main(String[] args) throws Exception {

        File file = null;
        PrintStream ps = null;

        int maxLinesToWriteBeforeClose = 100000;

        try {
            boolean appendFile = true;
            boolean autoFlush = true;

            file = new File("C:\\NotBackedUp\\test.txt");
            ps = new PrintStream(new FileOutputStream(file, appendFile), autoFlush);

            int loop = 100000000;
            int lineNum = 0;

            while (loop > 0) {
                ps.println("Test: " + loop);
                loop--;
                lineNum++;
                if(lineNum % maxLinesToWriteBeforeClose ==0){
                    closeFile(ps);
                    ps = new PrintStream(new FileOutputStream(file, appendFile), autoFlush);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeFile(ps);
        }
    }

    private static void closeFile(PrintStream ps){
        try{
            if (ps != null) {
                ps.close();
            }   
        } catch (Exception e) { 
        }
    }
}

There was an exception, but PrintWriter swallows exceptions. You need to check for them manually. Or else use BufferedWriter , which doesn't swallow exceptions.

I have a similar problem.My OS is win2010.

In a multi-threaded application, when I close a PrintWriter in one thread, and then open it again in another thread for outputing lines.I have used synchronized for opening and closing, but line data mixing can be found occasionally.

I try to add flush before PrintWriter closing, but it is still exists.

And if the Number of concurrently executing threads is 1, the data mixing disappear. In one thread mode, the open-closing to one files will also happen several times but the interval will be longer than mutithreads mode.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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