java


BufferedReader print input while reading in realtime


my java method looks like this:
private String executeCommand(String command) {
StringBuffer output = new StringBuffer();
String line = "";
Process proc;
try {
proc = Runtime.getRuntime().exec(command);
InputStream inputStream = proc.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
proc.waitFor();
} catch (Exception e) {
e.printStackTrace();
}
return output.toString();
}
If I send small commands like "ls -l" it executes the command and print the results. But I need this function to read the output from an application wich will run for days and it will print frequently output while running. So I can't wait until the application is "done", I need the output in realtime. Anyone knows how to print the output without delay? thanks for help..
The simplest way is to redirect the output of the command directly to a file:
ls -l > ./ls.output
In this case you don't need to wait the end of the command. It will be the operating system to handle the output redirection to a file.
If you need also to write the output for the error you need a command like the following:
ls -l > ./ls.output 2> ./ls.err
where ./ls.output is the normal output and ./ls.err is the output for errors

Related Links

How to process XML file with repeated fields in Java
Service crashes on app close
How to generate steps of cucumber features in STS (Eclipse)?
Buttons created in a loop are all doing the same thing when clicked
Why “java.lang.ClassNotFoundException: weka.core.NominalAttributeInfo” error in Java weka Android Studio?
spring-data-solr java.lang.IllegalArgumentException: Not a managed type
Nothing is getting copied to my system clipboard by using this code
Fragment showing blank but has loaded layout
Java function accepting garbage values for variable which is returned from c++ function using JNA
Convert string to json doesn't work java
Restemplate getforobject to java class
Replace characters from string using scala
Prevent further executions until next schedule time in Spring
java replace word document bookmarks and convert to pdf
Alfresco - How to add more options in a radio button dynamically?
Java Class file Encryption Tool <Does anyone know this program?

Categories

HOME
compiler-construction
openlayers
atom-editor
drivers
hashmap
fft
dxl
binary-tree
ip
baqend
mouse
android-youtube-api
slick-slider
windows-phone-7
flask-wtforms
ups
msp430
circuit
ghc
pc
google-cloud-speech
jtextfield
reverse-proxy
firebase-crash-reporting
p-value
opennlp
numerical-methods
pepper
django-storage
virtualdub
spring-mybatis
typo3-6.2.x
compatibility
facebook-apps
bpel
bluestacks
javascriptcore
avcapturesession
picasso
suricata
dosbox
objectlistview
greenrobot-eventbus
graphenedb
ensembles
mapzen
sql-server-agent
pdf-reactor
fault
dotcover
zip4j
yii2-extension
nativeapplication
slickedit
media-player
windows-mobile-6.5
epson
titanium-android
synchronous
firmata
rotativa
asp.net-4.5
plottable.js
storekit
hover-over
associative-array
r-tree
django-unittest
cakephp-3.1
ami
pick
rdl
starcluster
mysql-error-1062
flexmojos
mysqltuner
oam
dir
soundtouch
quartz-graphics
xamlparseexception
limejs
delphi-6
trusted
manchester-syntax
uiviewanimation-curve
php-parser
gnu-prolog
infobox
xmemcached
appender
site.master
filtered-index
rescale
iweb
uimenucontroller
pascal-fc
django-notification
paul-graham
lzh

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App