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 create a sort of abstract super test class in JUnit 4?
Differences in safe publishing between volatile,final and synchronized
Java Class Exception [closed]
JTable changing cells colour's from a given ArrayList
Mapping JSON from URL with Jackson
Advantages of parallelStream in Java SE8 [duplicate]
Canceling a request from manager using it's cache key not working
Exception in thread “main” java.lang.ArrayIndexOutofBounds Exception : 0 at runtime
parsing vagrant file inside java
how big a json/xml response is alright
Spring 4 Webservice HTTP 500 - IllegalStateException: The mapped controller method class is not an instance of the actual controller bean
Including all files in folder in the HDFS path
Limiting generic type
How to get query in Sql query to JPA
Java EXE on server accessed by client machines
how to configure scope model driven interceptor in struts2

Categories

HOME
xamarin
vbscript
atom-editor
ngrx
heroku
drivers
kde
google-play
hp-exstream
vmware
kalman-filter
at-command
rsync
sharepoint-designer
rubygems
rdf
blueprintjs
upload
portia
android-youtube-api
windows-10-universal
imacros
rascal
modx-revolution
hex-editors
percona
jsprit
netflix
fatal-error
aurigma
ml
scriptcs
jasonette
cloudhub
facebook-instant-articles
firebase-crash-reporting
qhull
internet-explorer-9
phpfox
assistant
compatibility
ecto
fog
calibre
typed.js
catch-all
mixture-model
theano.scan
preconditions
ruby-on-rails-3.1
grid.mvc
mime
total-commander
strptime
prolog-setof
python-c-api
alphabet
typescript1.8
jxcore
google-perftools
nsarray
sybase-asa
knpmenubundle
zendesk-app
lync-client-sdk
parallel-data-warehouse
multiple-regression
jwplayer7
simplewebrtc
lua-5.1
dataview
historian
kendonumerictextbox
qdialog
wireshark-dissector
security-testing
microbenchmark
umbraco6
c3
camanjs
heisenbug
navigationservice
kgdb
balanced-payments
ember-charts
.aspxauth
soundtouch
ember-app-kit
clipper
manchester-syntax
fireworks
gnu-prolog
qt-jambi
hirefire
celltable
telerik-scheduler
cxxtest
ext3
firefox-5
database-management
web-architecture
brewmp
javap
multiple-languages
document-conversion

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