943,836 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1242
  • Java RSS
Jan 14th, 2009
0

Regarding PrintStream class

Expand Post »
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?


Java Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 9
Solved Threads: 0
Light Poster
srs_grp is offline Offline
34 posts
since Sep 2008
Jan 14th, 2009
0

Re: Regarding PrintStream class

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
Reputation Points: 935
Solved Threads: 356
Nearly a Posting Maven
stultuske is offline Offline
2,497 posts
since Jan 2007
Jan 14th, 2009
0

Re: Regarding PrintStream class

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].
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Parsing String
Next Thread in Java Forum Timeline: network monitoring





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC