Example usage for java.io PrintStream PrintStream

List of usage examples for java.io PrintStream PrintStream

Introduction

In this page you can find the example usage for java.io PrintStream PrintStream.

Prototype

public PrintStream(File file) throws FileNotFoundException 

Source Link

Document

Creates a new print stream, without automatic line flushing, with the specified file.

Usage

From source file:Main.java

private static String getStringFromException(Throwable e) {
    String result = "";
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    PrintStream ps = new PrintStream(bos);
    e.printStackTrace(ps);//from  ww w. j av  a 2s.c o m
    try {
        result = bos.toString("UTF-8");
    } catch (UnsupportedEncodingException e1) {
        // won't happen
    }
    return result;
}

From source file:Main.java

public static String getExceptionCauseString(final Throwable ex) {
    final ByteArrayOutputStream bos = new ByteArrayOutputStream();
    final PrintStream ps = new PrintStream(bos);

    try {// w  ww .  j ava 2s .co m
        // print directly
        Throwable t = ex;
        while (t.getCause() != null) {
            t = t.getCause();
        }
        t.printStackTrace(ps);
        return toVisualString(bos.toString());
    } finally {
        try {
            bos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

/**
 * Returns stacktrace of current thread represented as String
 * /*from  w w w.j a  va 2 s .  com*/
 * @return stacktrace of current thread represented as String
 */
public static String getStackTrace() {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PrintStream printStream = new PrintStream(baos);
    // noinspection ThrowableInstanceNeverThrown
    new Exception("Stack trace").printStackTrace(printStream);
    printStream.close();
    return new String(baos.toByteArray());
}

From source file:Main.java

/**
 * @param throwable//  w w w . j  a  v  a  2 s .c  o m
 * @return return the stack-trace
 */
public static String getStackTrace(Throwable throwable) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PrintStream pstream = new PrintStream(baos);
    String stack = null;
    throwable.printStackTrace(pstream);
    pstream.flush();
    stack = baos.toString();
    return stack;
}

From source file:Main.java

public static String getErrorReport(String message, Throwable e) {
    if (message != null && message.length() > 0) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        e.printStackTrace(new PrintStream(byteArrayOutputStream));
        return String.format(ERROR_REPORT, message, byteArrayOutputStream.toString());
    } else {/* w w w. j  av  a  2 s.co  m*/
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        e.printStackTrace(new PrintStream(byteArrayOutputStream));
        return byteArrayOutputStream.toString();
    }

}

From source file:Main.java

public void run() {
    try {/*from w  ww .j  av a  2s.  c o m*/
        PrintStream pstream = new PrintStream(csocket.getOutputStream());
        for (int i = 10; i >= 0; i--) {
            pstream.println(i);
        }
        pstream.close();
        csocket.close();
    } catch (IOException e) {
        System.out.println(e);
    }
}

From source file:Main.java

private static void destroyPid(int pid) {

    Process suProcess = null;/*from  w  w w  . j  a  v a 2  s.c  o  m*/
    PrintStream outputStream = null;
    try {
        suProcess = Runtime.getRuntime().exec("su");
        outputStream = new PrintStream(new BufferedOutputStream(suProcess.getOutputStream(), 8192));
        outputStream.println("kill " + pid);
        outputStream.println("exit");
        outputStream.flush();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (outputStream != null) {
            outputStream.close();
        }
        if (suProcess != null) {
            try {
                suProcess.waitFor();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:MultiThreadServer.java

public void run() {
    try {/*  ww w .  ja  v a  2s  .  c  om*/
        PrintStream pstream = new PrintStream(csocket.getOutputStream());
        for (int i = 100; i >= 0; i--) {
            pstream.println(i + " bottles of beer on the wall");
        }
        pstream.close();
        csocket.close();
    } catch (IOException e) {
        System.out.println(e);
    }
}

From source file:Main.java

public static void printNode(Node node, String fn) {
    try {/*from www  .  j  ava  2 s .  c om*/
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer;
        transformer = tFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        DOMSource source = new DOMSource(node);
        PrintStream out = System.out;
        if (fn != null)
            out = new PrintStream(new FileOutputStream(fn));
        StreamResult result = new StreamResult(out);
        transformer.transform(source, result);
        out.close();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {

        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/** Converts a stack trace to a string */
public static String stackTraceToString(Exception e) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PrintStream ps = new PrintStream(baos);
    e.printStackTrace(ps);//from  w  w  w . j  a v  a  2 s .  c o m
    return baos.toString();
}