| | |
Regarding PrintStream class
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2008
Posts: 34
Reputation:
Solved Threads: 0
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?
How can we achieve this?
Java Syntax (Toggle Plain Text)
import java.io.*; import java.nio.*; import java.util.logging.FileHandler; import java.util.logging.Level; import java.util.logging.Logger; import java.util.logging.SimpleFormatter; public class MyLogger { ByteBuffer b1; public void logMessage(String msg,byte[] b) { int len=b.length; b1=b1.wrap(b); FileOutputStream out; PrintStream p; try { out = new FileOutputStream("c:\\MyLogFile.log") p = new PrintStream( out ); for (int i=0;i<len;i++ ) { p.print (b1.get(i)); p.print(" "); } p.println (" "); p.close(); } catch (Exception e) { System.err.println ("Error writing to file"); } Logger logger = Logger.getLogger("MyLog"); FileHandler fh; String strFilePath ="c:\\MyLogFile.log"; try { fh = new FileHandler(strFilePath,true); logger.addHandler(fh); logger.setLevel(Level.ALL); SimpleFormatter formatter = new SimpleFormatter(); fh.setFormatter(formatter); logger.log(Level.INFO,msg); } catch (SecurityException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
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
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
![]() |
Similar Threads
- Round-Robin Scheduling Algorithm (Java)
- Instantiating an Object of a Class Confusion (Java)
- Text Or Binary (Java)
- Finding length (Java)
- Stupid question about java.exe (Java)
- Cannot resolve symbol (Java)
- Alternative to PrintWriter (Java)
- Scheduling Tasks (Java)
- How can i call my FileOutput method to output the array ?? really confused... (Java)
- PLS Help newbie (Java)
Other Threads in the Java Forum
- Previous Thread: Parsing String
- Next Thread: network monitoring
| Thread Tools | Search this Thread |
Tag cloud for Java
actionlistener android api apple applet application apps arguments array arrays automation balls binary bluetooth card chat class classes clear client code component consumer database draw eclipse ee error event exception fractal free game gameprogramming gis givemetehcodez graphics gui html ide image input integer j2me j2seprojects java javaprojects jni jpanel julia jvm key linux list loop machine map method methods migrate mobile mobiledevelopmentcreatejar myaggfun netbeans newbie nextline nls notdisplaying number oracle output print problem program programming project recursion recursive scanner screen security server set size sms socket sort spamblocker sql sqlite string sun swing terminal test threads time tree trolltech windows






