java


Saving a BufferedWriter instance in a class. Any problems?


I am creating a service that looks similar to this:
interface MyLogger
{
void logStuff( int id, String message );
}
The implementation basically opens a file and caches an instance of the BufferedWriter
in the class. The method is synchronized.
Is it ok to have an opened file wrapper by a BufferedWriter in a long live manner ?
A simplistic implementation of logStuff.
public void logStuff(...)
{
try
{
this.writer.write( message );
this.writer.flush();
}
catch( IOException ignored ) {}
}
Yep, it's fine, you just need to be aware that other processes/threads may not be able to open the log file for writing while your service has it open. I think other processes can still open it read-only, though I don't know if that's system-dependent.
If other threads/processes do need to be able to write to the file intermixed with your service's writes, you could do something like caching log lines in the MyLogger instance; then once you have, say, 100, open the file in append mode, write them out, and close the file. That's not a particularly elegant thing to do, though.

Related Links

How to draw three lines with three different colours in a line chart using xChart Java
Array assistance needed in program
getActivity() in fragment returns null. Issue with FragmentPagerAdapter in parent activity?
sorting java task by user-entered dates
java caesar cipher code
JsonGenerationException: Null key for a Map not allowed in JSON (use a converting NullKeySerializer?)
Response from UrlConnection Java
Using ProcessBuilder with argument list as single String
Spring MVC default controller
Maven projects needs to be built to compile?
Filling background color in excel rows using Java apache POI
Append current date to log file name log4j
What is the difference between Fish tuna and Fish tuna = new Fish() in Java [duplicate]
Apache CXF Plugin - results in empty target node
How can I eliminate sonar qube issue when calling the run method of FutureTask?
How to add data to JavaFX Table rows/cells

Categories

HOME
ms-access
sendgrid
cloud
testng
webpack
isabelle
fluentd
mfc
path-finding
getelementsbytagname
elm
cmd
yarn
jxls
google-project-tango
wamp
datastax-java-driver
upload
multiple-records
python-unittest
kentor-authservices
u-sql
resize
jplayer
event-handling
normalizr
rst2pdf
ping
atlassian-crucible
google-sites-2016
wpf-controls
io-redirection
devextreme
event-driven
service-discovery
impersonation
sfdc
gulp-sourcemaps
eigenvalue
heightmap
angular2-meteor
auto-update
outlook-2013
multipeer-connectivity
python-c-api
pnotify
tactic
diagnostics
magma
snmptrapd
ifs
query-performance
angular-strap
azure-sdk
slicknav
login-control
flash-cs5
ready-api
dataview
itextpdf
associative-array
tcpserver
security-testing
graph-drawing
iiviewdeckcontroller
clipperlib
facebook-graph-api-v2.4
phalanger
xcode-6.2
mechanize-ruby
algebraic-data-types
monomac
intentservice
c18
dexterity
seed
http-unit
trusted
word-processor
hgsubversion
window-management
doh
zpt
coredump
xmlspy
lpeg
yslow
h.323
compiler-specific
firefox4
exchange-server-2003
privilege
spec#
genealogy
django-notification
signal-handling

Resources

Encrypt Message