Regarding PrintStream class

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2008
Posts: 34
Reputation: srs_grp is an unknown quantity at this point 
Solved Threads: 0
srs_grp srs_grp is offline Offline
Light Poster

Regarding PrintStream class

 
0
  #1
Jan 14th, 2009
We want to write byte array to a file.The code given below works correctly for only one byte array.When we try to write more than one byte array,the previous byte array gets overwritten.
How can we achieve this?


  1. import java.io.*;
  2. import java.nio.*;
  3. import java.util.logging.FileHandler;
  4. import java.util.logging.Level;
  5. import java.util.logging.Logger;
  6. import java.util.logging.SimpleFormatter;
  7.  
  8. public class MyLogger {
  9.  
  10. ByteBuffer b1;
  11. public void logMessage(String msg,byte[] b)
  12. {
  13. int len=b.length;
  14. b1=b1.wrap(b);
  15. FileOutputStream out;
  16. PrintStream p;
  17. try
  18. {
  19. out = new FileOutputStream("c:\\MyLogFile.log")
  20. p = new PrintStream( out );
  21. for (int i=0;i<len;i++ )
  22. {
  23. p.print (b1.get(i));
  24. p.print(" ");
  25. }
  26. p.println (" ");
  27. p.close();
  28. }
  29. catch (Exception e)
  30. {
  31. System.err.println ("Error writing to file");
  32. }
  33.  
  34.  
  35. Logger logger = Logger.getLogger("MyLog");
  36. FileHandler fh;
  37. String strFilePath ="c:\\MyLogFile.log";
  38. try
  39. {
  40. fh = new FileHandler(strFilePath,true);
  41. logger.addHandler(fh);
  42. logger.setLevel(Level.ALL);
  43. SimpleFormatter formatter = new SimpleFormatter();
  44. fh.setFormatter(formatter);
  45. logger.log(Level.INFO,msg);
  46. } catch (SecurityException e) {
  47. e.printStackTrace();
  48. } catch (IOException e) {
  49. e.printStackTrace();
  50. }
  51. }
  52. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: Regarding PrintStream class

 
0
  #2
Jan 14th, 2009
a solution would be to look into the classes used to write and search for append.
an other, if you want to keep most of your code, is to (before you write) read the existing data, put that data and your new data in one big char array, and write that one, but I would suggest the first option
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,635
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 469
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Regarding PrintStream class

 
0
  #3
Jan 14th, 2009
Why would you want to dump a byte array in a log file? Anyways, use a FileWriter in append mode and if it is *really* required, write out the byte array as a string [by looping over individual bytes and converting them to their String representation; use a StringBuilder].
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC