java - flush() does not flush on a Process OutputStream -
i tried multiple ways that: printing process-outputstream 1 second delay after each "flush()". flush doesn't seem work it's supposed to. can tell me why? 1 combined string
when "close()" being called. there won't printed before "close()" comes action.
context: should shut down minecraft server , run backup afterwards.
bufferedwriter w = new bufferedwriter(new outputstreamwriter(process.getoutputstream())); w.write("say serverbackup begins in 3..."); w.flush(); thread.sleep(1000); w.write("say 2..."); w.flush(); thread.sleep(1000); w.write("say 1..."); w.flush(); thread.sleep(1000); w.write("say game over!!!!!!!!!!!!!..."); w.flush(); w.write("stop"); w.flush(); w.close(); process.waitfor(10, timeunit.seconds); //...
as sanket gupte answered, have add '\n' @ end of messages, since read process's input stream .readline() (i suppose), print if \n appears, regardless thread.sleep(..) calls. , flush works supposed to. writes buffer stream.
- i suggest not problem process, since don't know
what running in background started bash , output via echo
processbuilder processbuilder = new processbuilder("/bin/bash"); processbuilder.redirecterrorstream(true); try { final process bash = processbuilder.start(); bufferedwriter bufferedwriter = new bufferedwriter(new outputstreamwriter(bash.getoutputstream())); new thread(new runnable() { public void run() { bufferedreader bufferedreader = new bufferedreader(new inputstreamreader(bash.getinputstream())); string line; try { while ((line = bufferedreader.readline()) != null){ system.out.println(line); } } catch (ioexception e) { e.printstacktrace(); } } }).start(); bufferedwriter.write("echo serverbackup begins in 3...\n"); bufferedwriter.flush(); thread.sleep(2000); bufferedwriter.write("echo 2\n"); bufferedwriter.close(); } catch (ioexception e) { e.printstacktrace(); } catch (interruptedexception e) { e.printstacktrace(); }
serverbackup begins in 3...
...(2 sec)
say 2
Comments
Post a Comment