Example usage for java.io PrintWriter println

List of usage examples for java.io PrintWriter println

Introduction

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

Prototype

public void println(Object x) 

Source Link

Document

Prints an Object and then terminates the line.

Usage

From source file:Main.java

public static void writeToExternalFile(String data, String logTag, String fileName) {

    if (!isExternalStorageWritable()) {
        Log.e(logTag, "failed to find external storage");
    } else {/* w  ww . j  av  a  2 s  .c o  m*/
        File path = Environment.getExternalStorageDirectory();
        File dir = new File(path.getAbsolutePath() + "/SDNController");
        if (!dir.isDirectory()) {
            if (!dir.mkdirs()) {
                Log.e(logTag, "sdn directory can not be created");
                return;
            }
        }

        File file = new File(dir, fileName);
        try {
            FileOutputStream f = new FileOutputStream(file, true);
            PrintWriter pw = new PrintWriter(f);
            pw.println(data);
            pw.flush();
            pw.close();
            f.close();
        } catch (FileNotFoundException e) {
            Log.e(logTag, "can not find indicated file");
            e.printStackTrace();
        } catch (IOException e) {
            Log.e(logTag, "failed to write SDNController/result.txt");
            e.printStackTrace();
        }
    }
}

From source file:ThreadLister.java

/** Display information about a thread. */
private static void printThreadInfo(PrintWriter out, Thread t, String indent) {
    if (t == null)
        return;/*  w  ww .  j  ava 2  s . c  om*/
    out.println(indent + "Thread: " + t.getName() + "  Priority: " + t.getPriority()
            + (t.isDaemon() ? " Daemon" : "") + (t.isAlive() ? "" : " Not Alive"));
}

From source file:dk.netarkivet.harvester.harvesting.frontier.FrontierReportCsvExport.java

/**
 * Outputs the report as CSV, using the given writer and the given field separator. Note that writer is not closed
 * by this method.//from   w w w  .jav  a 2  s  .  com
 *
 * @param pw the writer to output to
 * @param separator the field separator.
 */
public static void outputAsCsv(InMemoryFrontierReport report, PrintWriter pw, String separator) {

    pw.println(FIELD.genHeaderLine(separator));
    for (FrontierReportLine l : report.getLines()) {
        pw.println(FIELD.genLine(l, separator));
    }

}

From source file:Main.java

private static void printStackTrace(PrintWriter writer, Thread thread, StackTraceElement[] trace) {
    try {/*from w w  w  . j av a2  s.  c o  m*/
        writer.println(thread.toString() + ":");
        for (int i = 0; i < trace.length; i++)
            writer.println("\tat " + trace[i]);
    } catch (Exception e) {
        writer.println("\t*** Exception printing stack trace: " + e);
    }
    writer.flush();
}

From source file:FileTableHTML.java

public static String makeHTMLTable(String dirname) {
    // Look up the contents of the directory
    File dir = new File(dirname);
    String[] entries = dir.list();

    // Set up an output stream we can print the table to.
    // This is easier than concatenating strings all the time.
    StringWriter sout = new StringWriter();
    PrintWriter out = new PrintWriter(sout);

    // Print the directory name as the page title
    out.println("<H1>" + dirname + "</H1>");

    // Print an "up" link, unless we're already at the root
    String parent = dir.getParent();
    if ((parent != null) && (parent.length() > 0))
        out.println("<A HREF=\"" + parent + "\">Up to parent directory</A><P>");

    // Print out the table
    out.print("<TABLE BORDER=2 WIDTH=600><TR>");
    out.print("<TH>Name</TH><TH>Size</TH><TH>Modified</TH>");
    out.println("<TH>Readable?</TH><TH>Writable?</TH></TR>");
    for (int i = 0; i < entries.length; i++) {
        File f = new File(dir, entries[i]);
        out.println("<TR><TD>" + (f.isDirectory() ? "<a href=\"" + f + "\">" + entries[i] + "</a>" : entries[i])
                + "</TD><TD>" + f.length() + "</TD><TD>" + new Date(f.lastModified()) + "</TD><TD align=center>"
                + (f.canRead() ? "x" : " ") + "</TD><TD align=center>" + (f.canWrite() ? "x" : " ")
                + "</TD></TR>");
    }/*from w  w  w .  j av a 2 s  .  c  om*/
    out.println("</TABLE>");
    out.close();

    // Get the string of HTML from the StringWriter and return it.
    return sout.toString();
}

From source file:WriteFile.java

private static void writeMovie(Product m, PrintWriter out) {
    String line = m.title;//from   w  w  w.  j ava 2 s.  c o m
    line += "\t" + Integer.toString(m.year);
    line += "\t" + Double.toString(m.price);
    out.println(line);
}

From source file:Main.java

/**
 * Display information about a thread./*from www  . j  a  v a 2 s  .  c  o  m*/
 */
private static void printThreadInfo(PrintWriter out, Thread t, String indent) {
    if (t == null) {
        return;
    }
    out.println(indent + "Thread: " + t.getName() + "  Priority: " + t.getPriority()
            + (t.isDaemon() ? " Daemon" : "") + (t.isAlive() ? "" : " Not Alive"));
}

From source file:com.thoughtworks.go.agent.launcher.ServerCall.java

public static ServerResponseWrapper invoke(HttpMethod method) throws Exception {
    HashMap<String, String> headers = new HashMap<String, String>();
    HttpClient httpClient = new HttpClient();
    httpClient.setConnectionTimeout(HTTP_TIMEOUT_IN_MILLISECONDS);
    try {//  w  w w.  j  a  va 2 s.c  o  m
        final int status = httpClient.executeMethod(method);
        if (status == HttpStatus.SC_NOT_FOUND) {
            StringWriter sw = new StringWriter();
            PrintWriter out = new PrintWriter(sw);
            out.println("Return Code: " + status);
            out.println("Few Possible Causes: ");
            out.println("1. Your Go Server is down or not accessible.");
            out.println(
                    "2. This agent might be incompatible with your Go Server.Please fix the version mismatch between Go Server and Go Agent.");
            out.close();
            throw new Exception(sw.toString());
        }
        if (status != HttpStatus.SC_OK) {
            throw new Exception("Got status " + status + " " + method.getStatusText() + " from server");
        }
        for (Header header : method.getResponseHeaders()) {
            headers.put(header.getName(), header.getValue());
        }
        return new ServerResponseWrapper(headers, method.getResponseBodyAsStream());
    } catch (Exception e) {
        String message = "Couldn't access Go Server with base url: " + method.getURI() + ": " + e.toString();
        LOG.error(message);
        throw new Exception(message, e);
    } finally {
        method.releaseConnection();
    }
}

From source file:Main.java

/**
 * Generates the XML open tag as follows:
 * <ul>//from  w w  w .  j  av a2  s .c om
 * <li>If the isNull - &lt;fieldName/&gt;
 * <li>otherwise - &lt;fieldName&gt; a new line is inserted after this tag
 * if endWithNewLine = true
 * </ul>
 *
 * @param fieldName
 * @param isNull if true generates a self closing tag
 * @param pw
 * @param endWithNewLine if true then will add a new line after '>' tag
 * @throws Exception
 */
static public void writeXMLStart(String fieldName, boolean isNull, PrintWriter pw, boolean endWithNewLine) {
    pw.print('<');
    pw.print(fieldName);
    if (isNull) {
        pw.println("/>");
    } else {
        if (endWithNewLine)
            pw.println('>');
        else
            pw.print('>');
    }
}

From source file:Main.java

public static boolean appendFile(String strThrift, String filePath) {
    PrintWriter out = null;
    try {/*from  ww w .j  av  a 2s. c om*/
        out = new PrintWriter(new BufferedWriter(new FileWriter(filePath, true)));
        out.println(strThrift);
        out.flush();
        out.close();
    } catch (IOException e) {
        return false;
    }
    return true;

}