2011-01-25 16 views
30

मैंने निम्न कोड का उपयोग कर सिस्टम.आउट को/dev/null पर अस्थायी रूप से रीडायरेक्ट करने का प्रयास किया है लेकिन यह काम नहीं करता है।जावा में, मैं System.out को शून्य पर फिर से रीडाउट करने के लिए कैसे रीडायरेक्ट कर सकता हूं?

System.out.println("this should go to stdout"); 

PrintStream original = System.out; 
System.setOut(new PrintStream(new FileOutputStream("/dev/null"))); 
System.out.println("this should go to /dev/null"); 

System.setOut(original); 
System.out.println("this should go to stdout"); // This is not getting printed!!! 

किसी के पास कोई विचार है?

+0

मैं अपने सिस्टम पर दोनों लाइनों को ठीक देखता हूं। मैं जावा 6 अपडेट 22 का उपयोग कर रहा हूं। –

+2

बीटीडब्लू: एकाधिक थ्रेड द्वारा समवर्ती इंटरैक्शन को रोकने के लिए सिस्टम सिंक्रनाइज़ेशन के बिना इस तरह से सिस्टम को मैनिपुलेट करने से सावधान रहें। –

उत्तर

43

मैन, यह इतना अच्छा नहीं है, क्योंकि जावा क्रॉस-प्लेटफ़ॉर्म है और '/ dev/null' यूनिक्स विशिष्ट है (जाहिर है विंडोज़ पर एक विकल्प है, टिप्पणियां पढ़ें)। तो आउटपुट अक्षम करने के लिए कस्टम आउटपुटस्ट्रीम बनाने का आपका सबसे अच्छा विकल्प है।

try { 
    System.out.println("this should go to stdout"); 

    PrintStream original = System.out; 
    System.setOut(new PrintStream(new OutputStream() { 
       public void write(int b) { 
        //DO NOTHING 
       } 
      })); 
    System.out.println("this should go to /dev/null, but it doesn't because it's not supported on other platforms"); 

    System.setOut(original); 
    System.out.println("this should go to stdout"); 
} 
catch (Exception e) { 
    e.printStackTrace(); 
} 
+3

विंडोज़, एनयूएल डिवाइस पर वैकल्पिक विकल्प है; आईआईआरसी यह "एनयूएल:" एक "फ़ाइल नाम" के रूप में है। –

+0

ओह वाह, यह वास्तव में पहली बार है जब मैं इसके बारे में सुनता हूं! पोस्ट संपादित करने वाला। धन्यवाद! –

+3

लेकिन +1, क्योंकि यह सही मंच-स्वतंत्र उत्तर है। हालांकि, मैं दक्षता के लिए लिखने (बाइट [], int, int) को ओवरराइड भी करता हूं। –

15

आप नीचे दिए गए के रूप में वर्ग NullPrintStream उपयोग कर सकते हैं:

PrintStream original = System.out; 
System.setOut(new NullPrintStream()); 
System.out.println("Message not shown."); 
System.setOut(original); 

और वर्ग NullPrintStream है ...

import java.io.ByteArrayOutputStream; 
import java.io.IOException; 
import java.io.OutputStream; 
import java.io.PrintStream; 

public class NullPrintStream extends PrintStream { 

    public NullPrintStream() { 
    super(new NullByteArrayOutputStream()); 
    } 

    private static class NullByteArrayOutputStream extends ByteArrayOutputStream { 

    @Override 
    public void write(int b) { 
     // do nothing 
    } 

    @Override 
    public void write(byte[] b, int off, int len) { 
     // do nothing 
    } 

    @Override 
    public void writeTo(OutputStream out) throws IOException { 
     // do nothing 
    } 

    } 

} 
+0

मुझे नलप्रिंटस्ट्रीम कहां मिल सकता है? कौन सी लाइब्रेरी? या मुझे कक्षा बनाने की ज़रूरत है? –

+0

मुझे वास्तव में मेरे कोड के लिए इसकी आवश्यकता है ... यह निश्चित रूप से stdout silencing के लिए एक जीवन बचतकर्ता है। –

2

पुराना सवाल है, मुझे पता है, लेकिन इस छोटे लाइन करना होगा विंडोज पर चाल?

System.setOut(new PrintStream(new File("NUL")));

बहुत कम कोड और मेरे पास बहुत सीधा लग रहा है।

-3

मुझे लगता है System.setOut (शून्य); भी काम करना चाहिए। कम से कम यह मेरे लिए काम किया।

+1

System.out.anything पर आने वाली किसी भी कॉल को NullPointerException को फेंकने का कारण बन जाएगा। अगर यह आपके लिए काम करता है, तो आपको कहीं अपवाद पकड़ना होगा। –

 संबंधित मुद्दे

  • कोई संबंधित समस्या नहीं^_^