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

Unsorted SparseArray
Use Java Regex to find multiple matching words in a sentence
Externalise REST endpoint in Websphere Application Server
Array of objects in Memory
NullPointerException for BinaryTree created through ObjectInputStream
Android: Need to save checkbox state in a custom adapter listview
Returning values from radio buttons, checkboxes
Equal distributing algorithm
maze algorithm to finding all path (and shortest path)
writing new file using filewriter to csv file
How do I check if one string contains characters from another string?
How to break URL's in java?
algorithms average case time complexity
try-with-resource not supported in -source1.5 [duplicate]
How can I change the background color of a jPanel containing JRadioButtons?
How to get current location?

Categories

HOME
log4j
google-chrome-extension
wso2-am
date
homebrew
relative-path
hp-exstream
gis
razor
filtering
serverspec
fancybox
uber-api
webrequest
add
medical
metatrader4
visual-studio-2005
graphlab
text-rendering
mustache.php
core-text
jndi
crystal-reports-2010
p-value
h2db
tdd
apache-commons-io
wpfdatagrid
fluentvalidation
jaxb2
libssl
tasklet
rotational-matrices
mmenu
hexo
revolution-slider
hockeyapp
libraries
objectlistview
reportbuilder
hue
elasticsearch-plugin
azure-ml
mesos-chronos
mapzen
sfdc
ncalc
node-gyp
node-sass
cubic-spline
xml-documentation
parentheses
yii2-extension
knockout-components
android-cursor
linode
iso8601
jspdf-autotable
azure-sdk
infix-notation
autorest
qpid
multiple-regression
fputcsv
pickadate
hexdump
wapiti
ios8-today-widget
endeca-workbench
comobject
wireshark-dissector
iis-arr
sailfish-os
generic-programming
unity5.2.3
tween
dd
issuu
bitcoinj
openexr
funcunit
didselectrowatindexpath
mbr
monomac
farseer
inbox
drools-planner
rdoc
google-email-migration
newtonscript
cufon
whoosh
stage
cinema-4d
table-footer
dmx512
nhibernate.search
photoshop-cs4
sector
.net-1.0
lzh

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App