Example usage for java.io PrintWriter PrintWriter

List of usage examples for java.io PrintWriter PrintWriter

Introduction

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

Prototype

public PrintWriter(File file) throws FileNotFoundException 

Source Link

Document

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

Usage

From source file:JGraphLoader.GraphSaver.java

public static void saveG(String filename, Graph g) {

    final AbstractLayout layout = new StaticLayout(g);

    GraphMLWriter<node, edge> graphWriter = new GraphMLWriter<node, edge>();
    try {/*  w  ww. j  av  a  2 s. c o  m*/
        PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(filename)));

        graphWriter.addEdgeData("id", null, "0", new Transformer<edge, String>() {
            @Override
            public String transform(edge i) {
                return i.getValue();
            }
        });
        /*
         graphWriter.addVertexData("y", null, "0",
         new Transformer<node, String>() {
         public String transform(node v) {
         return Double.toString(layout.getY(v));
         }
         }
         );*/

        graphWriter.save(g, out);
    } catch (IOException ex) {
        Logger.getLogger(GraphSaver.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:Main.java

public static void startLogger(String file) {
    if (writer != null) {
        stopLogger();/*from   w w  w  .  j a va 2  s.  c  om*/
    }
    try {
        writer = new PrintWriter(new FileWriter(file));
    } catch (Exception e) {
        e.printStackTrace();
        if (writer != null) {
            writer.close();
        }
    }
}

From source file:Main.java

private static void overwriteXmlFile(File xmlFile, Document document, Transformer transformer)
        throws FileNotFoundException, TransformerException {
    StreamResult result = new StreamResult(new PrintWriter(new FileOutputStream(xmlFile, false)));
    DOMSource source = new DOMSource(document);
    transformer.transform(source, result);
}

From source file:com.dianping.lion.util.ThrowableUtils.java

public static String extractStackTrace(Throwable t, int maxLen) {
    StringWriter me = new StringWriter();
    PrintWriter pw = new PrintWriter(me);
    t.printStackTrace(pw);/*from w  w w. j av  a 2 s.c  o m*/
    pw.flush();
    return StringUtils.substring(me.toString(), 0, maxLen);
}

From source file:Main.java

/**
 * Return the absolute path to the hub database.
 *
 * @return path The path to the db as a string
 *//*from  ww w. ja  v a  2 s.c o  m*/
private static String getDbPath() {
    if (Debug)
        Log.i(TAG, "getDBPath() called.");

    String path = Environment.getExternalStorageDirectory().getPath() + "/" + BASE_DIR;

    File dbDir = new File(path);
    if (!dbDir.isDirectory()) {
        try {
            if (Debug)
                Log.i(TAG, "Trying to create " + path);
            dbDir.mkdirs();
        } catch (Exception e) {
            final Writer result = new StringWriter();
            final PrintWriter printWriter = new PrintWriter(result);
            e.printStackTrace(printWriter);
            Log.e(TAG, result.toString());
        }
    }
    return path;
}

From source file:Main.java

static public String outputTextDoc(Node outputNode) {
    StringWriter out = new StringWriter();
    PrintWriter pOut = new PrintWriter(out);
    outputNode(outputNode, pOut, 0);/*from ww w  .  jav  a2 s . co m*/
    return (out.toString());
}

From source file:Main.java

/**
 * @param thread a thread/*from  w w  w. j a v  a2  s  .  c om*/
 * @return a human-readable representation of the thread's stack trace
 */
public static String formatStackTrace(Thread thread) {
    Throwable t = new Throwable(
            String.format("Stack trace for thread %s (State: %s):", thread.getName(), thread.getState()));
    t.setStackTrace(thread.getStackTrace());
    StringWriter sw = new StringWriter();
    t.printStackTrace(new PrintWriter(sw));
    return sw.toString();
}

From source file:Main.java

public static String toString(Throwable e) {
    StringWriter w = new StringWriter();
    PrintWriter p = new PrintWriter(w);
    p.print(e.getClass().getName() + ": ");
    if (e.getMessage() != null) {
        p.print(e.getMessage() + "\n");
    }/*w  w  w  . j  a va2 s .com*/
    p.println();
    try {
        e.printStackTrace(p);
        return w.toString();
    } finally {
        p.close();
    }
}

From source file:at.peppol.smp.client.console.SMPClient.java

public static void main(final String[] args) throws Exception {
    if (false) {//ww  w. jav  a 2  s. co m
        // Enable this section in development mode, if you want to trust all HTTPS
        // certificates
        final SSLContext aSSLContext = SSLContext.getInstance("SSL");
        aSSLContext.init(null, new TrustManager[] { new DoNothingTrustManager() },
                VerySecureRandom.getInstance());
        HttpsURLConnection.setDefaultSSLSocketFactory(aSSLContext.getSocketFactory());
    }

    final SMPClientOptions aOptions = new SMPClientOptions();
    final CommandLine cmd = new PosixParser().parse(aOptions, args);

    ECommand eAction = null;
    boolean bGoodCmd = true;
    String cert = null;

    if (!cmd.hasOption("h")) {
        s_aLogger.error("No Host specified use -h to specify Host");
        bGoodCmd = false;
    }

    if (!cmd.hasOption("u")) {
        s_aLogger.error("No Username specified use -u to specify username");
        bGoodCmd = false;
    }

    if (!cmd.hasOption("p")) {
        s_aLogger.error("No Password specified use -p to specify password");
        bGoodCmd = false;
    }

    if (!cmd.hasOption("c")) {
        s_aLogger.error("No Action specified please use -c parameter to specify command("
                + ECommand.getAllAsString() + ")");
        bGoodCmd = false;
    } else {
        final String sCommand = cmd.getOptionValue("c");
        eAction = ECommand.getFromNameOrNull(sCommand);
        if (eAction == null) {
            s_aLogger.error("Illegal Action specified:" + sCommand + " allowed commands("
                    + ECommand.getAllAsString() + ")");
            bGoodCmd = false;
        } else
            switch (eAction) {
            case ADDGROUP:
                if (!cmd.hasOption("b")) {
                    s_aLogger.error(
                            "No Business/Participant ID specified use -b to specify Business/Participant ID");
                    bGoodCmd = false;
                }
                break;
            case DELGROUP:
                if (!cmd.hasOption("b")) {
                    s_aLogger.error(
                            "No Business/Participant ID specified use -b to specify Business/Participant ID");
                    bGoodCmd = false;
                }
                break;
            case ADD:
                if (!cmd.hasOption("a")) {
                    s_aLogger.error("No Accesspoint URL defined use -a to Specifify AP-URL");
                    bGoodCmd = false;
                }
                if (!cmd.hasOption("b")) {
                    s_aLogger.error(
                            "No Business/Participant ID specified use -b to specify Business/Participant ID");
                    bGoodCmd = false;
                }
                if (!cmd.hasOption("d")) {
                    s_aLogger.error("No DocumentType ID specified use -d to specify Document Type ID");
                    bGoodCmd = false;
                }
                if (!cmd.hasOption("r")) {
                    s_aLogger.error("No Process ID specified use -r to specify Process ID");
                    bGoodCmd = false;
                }
                if (!cmd.hasOption("e")) {
                    s_aLogger.error("No Certificate PEM file specified use -e to specify Certificate PEM file");
                    bGoodCmd = false;
                } else {
                    cert = SimpleFileIO.readFileAsString(new File(cmd.getOptionValue('e')),
                            CCharset.CHARSET_ISO_8859_1);
                }
                break;
            case DEL:
                if (!cmd.hasOption("b")) {
                    s_aLogger.error(
                            "No Business/Participant ID specified use -b to specify Business/Participant ID");
                    bGoodCmd = false;
                }
                if (!cmd.hasOption("d")) {
                    s_aLogger.error("No Document Type ID specified use -d to specify Document Type ID");
                    bGoodCmd = false;
                }
            }
    }

    if (!bGoodCmd) {
        final NonBlockingStringWriter aSW = new NonBlockingStringWriter();
        new HelpFormatter().printHelp(new PrintWriter(aSW), HelpFormatter.DEFAULT_WIDTH,
                CGStringHelper.getClassLocalName(SMPClient.class), null, aOptions,
                HelpFormatter.DEFAULT_LEFT_PAD, HelpFormatter.DEFAULT_DESC_PAD, null);
        s_aLogger.info(aSW.getAsString());
        System.exit(-3);
    }

    final SMPClient client = new SMPClient(new URI(cmd.getOptionValue('h')), cmd.getOptionValue('u'),
            cmd.getOptionValue('p'), cmd.getOptionValue('b'), cmd.getOptionValue('d'), cmd.getOptionValue('r'),
            cmd.getOptionValue('a'), cert);

    switch (eAction) {
    case ADDGROUP:
        client._createServiceGroup();
        break;
    case DELGROUP:
        client._deleteServiceGroup();
        break;
    case ADD:
        client._addDocument();
        break;
    case DEL:
        client._deleteDocument();
        break;
    case LIST:
        client._listDocuments();
        break;
    default:
        throw new IllegalStateException();
    }
}

From source file:Main.java

/**
 * Print the stack trace for a SQLException to STDERR.
 *
 * @param e SQLException to print stack trace of
 *///from w w  w.  j av  a 2  s  . c om
public static void printStackTrace(SQLException e) {
    printStackTrace(e, new PrintWriter(System.err));
}