• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

How to append my text data to a file

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a class A which uses FileWriter to write in to a NotePad file.

And i have class B and class C, both creates object of class A and try to write onto the same file at same time. But only one class either class B or class C is writing into the file.
But i need both the classes to write in to the file. Like i need to append to the contents of the file continuosly. you may consider like i am trying to write a log file for my application.

Help me out.
regards,
Surendar
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
make the FileWriter object in A, static
you'll probably also need a static method to close the file
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi You can try to have the following constructor method.

FileWriter fw = new FileWriter(file,true);

The boolean value true will append end of the file.
 
Ranch Hand
Posts: 257
Hibernate Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Make the Class A as singleton pattern, that is making single global instance for any access.

So Class B and Class C can write into the file at a time but in sequence.

Static will be used to make singleton in java.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
B and C need to use the same instance of A. Making A a singleton would be one solution, but not my preferred one.

You will likely also need to synchronize A properly.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic