Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple debugger class

 
Barry Andrews
Ranch Hand
Posts: 523
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,
I want to write a simple debugger class that I can call to write debugging information to a file. I am getting some weird results from this code. Here is an example of something I am tried to send to a file:

I am only getting the last line in the debug.txt file. The other 3 lines above it do not get written. Does anyone know what I am doing wrong? Many thanks!
Barry

 
Lewin Chan
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's because you're creating a new printwriter in the debug class every time you invoke write(). It may be, that even if you auto-flush, the file isn't really synced to disk, until the printwriter is closed;
I would change your class so that the the printwriter is created during the constructor
public Debug()
{
pw = new PrintWriter(new FileWriter("fred.txt", true),true);
}

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic