2009-02-03 10 views
11

जावा नौसिखिया सवाल कैप्चरिंग:जावा - कैप्चर System.err.println या एक PrintStream

मैं पाठ एक 3 पार्टी घटक द्वारा एक printStream लिखा जा रहा कब्जा करने के लिए की जरूरत है।

प्रिंटस्ट्रीम System.err पर डिफॉल्ट किया गया है, लेकिन इसे दूसरे प्रिंटस्ट्रीम में बदला जा सकता है।

दस्तावेज़ों के माध्यम से देखकर, मुझे एक स्ट्रिंग लेखक/बफर में प्रिंटस्ट्रीम की सामग्री को निर्देशित करने का एक आसान तरीका नहीं मिला।

क्या कोई मदद कर सकता है?

+0

क्या आपने ByteArrayOutputStream की कोशिश की है? – IAdapter

उत्तर

8
PipedOutputStream pipeOut = new PipedOutputStream(); 
PipedInputStream pipeIn = new PipedInputStream(pipeOut); 
System.setOut(new PrintStream(pipeOut)); 
// now read from pipeIn 
+3

यह अनुशंसित नहीं है, प्रलेखन कहता है "डेटा एक पिपेडऑटपुटस्ट्रीम ऑब्जेक्ट को एक थ्रेड द्वारा लिखा जाता है और किसी अन्य थ्रेड द्वारा कनेक्टेड पाइपड इनपुटस्ट्रीम से डेटा पढ़ा जाता है। एक ही थ्रेड से दोनों ऑब्जेक्ट्स का उपयोग करने का प्रयास करने की अनुशंसा नहीं की जाती है क्योंकि यह डेडलॉक हो सकता है धागा। " http://docs.oracle.com/javase/1.4.2/docs/api/java/io/PipedOutputStream.html – Pieter

5

आप किसी अन्य आउटपुटस्ट्रीम के आसपास प्रिंटस्ट्रीम बना सकते हैं।

एक है कि स्मृति में एक बफर के लिए चला जाता होगा बनाने के लिए सबसे आसान तरीका:

PrintStream p = new PrintStream(new ByteArrayOutputStream()) 

तो फिर तुम पढ़ सकते हैं और जो कुछ भी आप की तरह अंक पर बाइट सरणी की सामग्री को रीसेट कर सकते हैं।

पाइप का उपयोग करने की एक और संभावना होगी।

InputStream third_party_output = new PipedInputStream(); 
PrintStream p = new PrintStream(new PipedOutputStream(third_party_output)); 

फिर आप लाइब्रेरी द्वारा लिखे गए पाठ को प्राप्त करने के लिए तीसरे_party_output स्ट्रीम से पढ़ सकते हैं।

3

क्या आप इस तरह कुछ ढूंढ रहे हैं?

OutputStream redirect = System.err; 
    PrintStream myPrintStream = new PrintStream(redirect); 
    myPrintStream.println("hello redirect"); 

आप 3 पार्टी आवेदन करने के लिए myPrintStream पारित कर सकते हैं, तो आप इसे अनुप्रेषित कर सकते हैं कहीं भी आप चाहते हैं।

8
import java.io.*; 

public class Test { 
    public static void main(String[] args) { 
     FileOutputStream fos = null; 
     try { 
      fos = new FileOutputStream("errors.txt"); 
     } catch(IOException ioe) { 
      System.err.println("redirection not possible: "+ioe); 
      System.exit(-1); 
     } 
     PrintStream ps = new PrintStream(fos); 
     System.setErr(ps); 
     System.err.println("goes into file"); 
    } 
} 
2

मैं फ़ाइलों (जहां xxx-001.log सबसे हाल ही में है) घूर्णन का एक सेट करने के लिए System.out और System.err लॉग इन करने के निम्न वर्ग का उपयोग करें। इसमें उपयोगिता विधियों के लिए कुछ कॉल शामिल हैं, जिन्हें संकलित करने से पहले आपको लागू करने की आवश्यकता होगी - उन्हें आत्म-स्पष्टीकरण होना चाहिए।

import java.io.*; 
import java.lang.reflect.*; 

public class LoggerOutputStream 
extends OutputStream 
{ 

// ***************************************************************************** 
// INSTANCE PROPERTIES 
// ***************************************************************************** 

private FileOutputStream    log=null;        // the base output stream 
private String       fnmBase,fnmExt;       // filename base, file extension 
private int        fnmCount,fnmLast;      // count for filename index, last filename used 
private int        logSize,totWritten;      // max log size, current number of bytes written 

// ***************************************************************************** 
// INSTANCE CONSTRUCTORS/INIT/CLOSE/FINALIZE 
// ***************************************************************************** 

public LoggerOutputStream(String baseFilename) throws IOException { 
    this(baseFilename,".log",2,1024000); 
    } 

public LoggerOutputStream(String baseFilename, String extension) throws IOException { 
    this(baseFilename,extension,2,1024000); 
    } 

public LoggerOutputStream(String baseFilename, String extension, int numberOfFiles, int maxFileSize) throws IOException { 
    fnmBase=baseFilename; 
    if(Character.isLetterOrDigit(fnmBase.charAt(fnmBase.length()-1))) { fnmBase=(fnmBase+"-"); } 
    fnmExt=extension; 
    if(!fnmExt.startsWith(".")) { fnmExt=('.'+fnmExt); } 
    fnmCount=numberOfFiles; 
    logSize=maxFileSize; 
    if(fnmCount>MAXLOGS) { fnmCount=MAXLOGS; } 

    fnmLast=0; 
    for(int xa=1; xa<=MAXLOGS; xa++) { 
     if(!new File(constructFilename(xa)).exists()) { 
      while((--xa)>fnmCount) { IoUtil.deleteFile(constructFilename(xa)); } 
      fnmLast=xa; 
      break; 
      } 
     } 
    log=null; 

    openFile(false); 

    if(numberOfFiles>MAXLOGS) { System.out.println("** Log File Count Limited To "+MAXLOGS); } 
    } 

public void close() throws IOException { 
    close(false); 
    } 

private void openFile(boolean ovrflw) throws IOException { 
    close(true); 

    if  (fnmLast< fnmCount) { fnmLast++;          } 
    else if(fnmLast==fnmCount) { IoUtil.deleteFile(constructFilename(fnmCount)); } 
    for(int xa=fnmLast; xa>0; xa--) { IoUtil.renameFile(constructFilename(xa-1),constructFilename(xa)); } 
    log=new FileOutputStream(constructFilename(1)); 
    totWritten=0; 
    } 

private String constructFilename(int index) { 
    return constructFilename(fnmBase,index,fnmExt); 
    } 

private synchronized void close(boolean ovrflw) throws IOException { 
    if(log!=null) { 
     log.flush(); 
     log.close(); 
     log=null; 
     } 
    } 

// ***************************************************************************** 
// INSTANCE METHODS - ACCESSORS 
// ***************************************************************************** 

public String getFilename() { 
    return constructFilename(1); 
    } 

public String getFilename(int idx) { 
    return constructFilename(idx); 
    } 

public synchronized void cycleLogFile() throws IOException { 
    openFile(true); 
    } 

// ***************************************************************************** 
// INSTANCE METHODS 
// ***************************************************************************** 

public synchronized void flush() throws IOException { 
    if(log!=null) { 
     log.flush(); 
     } 
    } 

public synchronized void write(int val) throws IOException { 
    if(log!=null) { 
     log.write(val); 
     totWritten++; 
     if(val=='\n') { 
      if(totWritten>logSize) { openFile(true); } 
      else     { log.flush(); } 
      } 
     } 
    } 

public synchronized void write(byte[] bytes) throws IOException { 
    if(log!=null) { 
     log.write(bytes); 
     totWritten+=bytes.length; 
     if(bytes.length>0 && bytes[bytes.length-1]=='\n') { 
      if(totWritten>logSize) { openFile(true); } 
      else     { log.flush(); } 
      } 
     } 
    } 

public synchronized void write(byte[] bytes, int str, int len) throws IOException { 
    if(log!=null) { 
     log.write(bytes,str,len); 
     totWritten+=len; 
     if(bytes.length>(str+len-1) && bytes[str+len-1]=='\n') { 
      if(totWritten>logSize) { openFile(true); } 
      else     { log.flush(); } 
      } 
     } 
    } 

// ***************************************************************************** 
// STATIC PROPERTIES 
// ***************************************************************************** 

static public final int     MAXLOGS=999;       // maximum log files allowed 

// ***************************************************************************** 
// STATIC METHODS 
// ***************************************************************************** 

static public String constructFilename(String bas, int idx, String ext) { 
    if(!bas.endsWith("-") && !bas.endsWith("_") && !bas.endsWith(".")) { bas=(bas+"-"); } 
    if(!ext.startsWith(".")           ) { ext=('.'+ext); } 
    return (bas+TextUtil.raZeros(idx,3)+ext); 
    } 

} /* END PUBLIC CLASS */ 
संबंधित मुद्दे