Example usage for java.io PrintWriter close

List of usage examples for java.io PrintWriter close

Introduction

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

Prototype

public void close() 

Source Link

Document

Closes the stream and releases any system resources associated with it.

Usage

From source file:com.opendoorlogistics.core.utils.strings.Strings.java

public static void writeToFile(String s, File file) {
    try {/*  w  ww .  j av  a  2 s. c  o m*/
        PrintWriter out = new PrintWriter(file);
        out.append(s);
        out.close();
    } catch (Throwable e) {
        throw new RuntimeException(e);
    }
}

From source file:MainClass.java

public static void premain(final Instrumentation inst) {
    Runtime.getRuntime().addShutdownHook(new Thread() {
        public void run() {
            try {
                PrintWriter out = new PrintWriter(System.err);

                ThreadMXBean tb = ManagementFactory.getThreadMXBean();
                out.printf("Current thread count: %d%n", tb.getThreadCount());
                out.printf("Peak thread count: %d%n", tb.getPeakThreadCount());

                List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
                for (MemoryPoolMXBean pool : pools) {
                    MemoryUsage peak = pool.getPeakUsage();
                    out.printf("Peak %s memory used: %,d%n", pool.getName(), peak.getUsed());
                    out.printf("Peak %s memory reserved: %,d%n", pool.getName(), peak.getCommitted());
                }//from   w  ww.  j  a  va2 s  . c  om

                Class[] loaded = inst.getAllLoadedClasses();
                out.println("Loaded classes:");
                for (Class c : loaded)
                    out.println(c.getName());
                out.close();
            } catch (Throwable t) {
                System.err.println("Exception in agent: " + t);
            }
        }
    });
}

From source file:com.cloudera.cli.validator.components.CommandLineOptions.java

/**
 * Writes usage message to outputstream.
 *
 * @param appName the application name./*from ww  w . jav a2 s  .c o  m*/
 * @param stream output stream.
 * @throws UnsupportedEncodingException
 */
public static void printUsageMessage(String appName, OutputStream stream) throws UnsupportedEncodingException {
    PrintWriter writer = new PrintWriter(
            new BufferedWriter(new OutputStreamWriter(stream, Constants.CHARSET_UTF_8)));
    try {
        String header = "Validates Cloudera Manager Schemas";
        String footer = "";
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp(writer, HelpFormatter.DEFAULT_WIDTH, appName, header, OPTIONS,
                HelpFormatter.DEFAULT_LEFT_PAD, HelpFormatter.DEFAULT_DESC_PAD, footer, true); // auto-usage: whether to also show
                                                                                                                                                                                  // the command line args on the usage line.
    } finally {
        writer.close();
    }
}

From source file:io.datalayer.conf.HierarchicalIniConfigurationTest.java

/**
 * Writes a test ini file.// w  w  w.  j a v  a2  s  .  com
 *
 * @param content the content of the file
 * @throws IOException if an error occurs
 */
private static void writeTestFile(String content) throws IOException {
    PrintWriter out = new PrintWriter(new FileWriter(TEST_FILE));
    try {
        out.println(content);
    } finally {
        out.close();
    }
}

From source file:i18n.Language.java

public static void sauver(String nomDuFichier) {
    if (jo != null) {
        try {/*  www .j av a  2  s.c o m*/
            FileWriter fw = new FileWriter(nomDuFichier);
            BufferedWriter bw = new BufferedWriter(fw);
            PrintWriter fichierSortie = new PrintWriter(bw);
            fichierSortie.println(jo.toString());
            fichierSortie.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:com.headswilllol.basiclauncher.Launcher.java

public static void createExceptionLog(String s, boolean gameThread) {
    String log = getLogHeader(gameThread);
    log += s;/* ww  w . j  a  v  a  2  s. c o m*/
    log += "\n-----------------END ERROR LOG-----------------\n";
    Calendar cal = Calendar.getInstance();
    String minute = cal.get(Calendar.MINUTE) + "";
    if (minute.length() < 2)
        minute = "0" + minute;
    String second = cal.get(Calendar.SECOND) + "";
    if (second.length() < 2)
        second = "0" + second;
    String time = cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-"
            + cal.get(Calendar.DAY_OF_MONTH) + "_" + cal.get(Calendar.HOUR_OF_DAY) + "-" + minute + "-" + second
            + "-" + cal.get(Calendar.MILLISECOND);
    try {
        if (!new File(appData(), FOLDER_NAME + File.separator + "errorlogs").exists())
            new File(appData(), FOLDER_NAME + File.separator + "errorlogs").mkdir();
        new File(appData(), FOLDER_NAME + File.separator + "errorlogs" + File.separator + time + ".log")
                .createNewFile();
        System.out.println("Saved error log to " + appData() + File.separator + FOLDER_NAME + File.separator
                + "errorlogs" + File.separator + time + ".log");
        PrintWriter writer = new PrintWriter(appData() + File.separator + FOLDER_NAME + File.separator
                + "errorlogs" + File.separator + time + ".log", "UTF-8");
        writer.print(log);
        writer.close();
    } catch (Exception ex) {
        // Well, shit.
        ex.printStackTrace();
        Launcher.progress = "An exception occurred while saving an exception log.";
        Launcher.fail = "Errors occurred; see console for details.";
    }
}

From source file:com.erudika.para.rest.RestUtils.java

/**
 * A generic JSON response handler/*from   w  ww.j  a  va2  s . co m*/
 * @param response the response to write to
 * @param status status code
 * @param message error message
 */
public static void returnStatusResponse(HttpServletResponse response, int status, String message) {
    if (response == null) {
        return;
    }
    PrintWriter out = null;
    try {
        response.setStatus(status);
        response.setContentType(MediaType.APPLICATION_JSON);
        out = response.getWriter();
        ParaObjectUtils.getJsonWriter().writeValue(out,
                getStatusResponse(Response.Status.fromStatusCode(status), message).getEntity());
    } catch (Exception ex) {
        logger.error(null, ex);
    } finally {
        if (out != null) {
            out.close();
        }
    }
}

From source file:com.adito.util.Utils.java

public static String getStackTrace(Throwable t) {
    StringWriter s = new StringWriter();
    PrintWriter p = new PrintWriter(s);
    t.printStackTrace(p);/* w w  w  .  ja  v  a2  s . c o m*/
    p.close();
    return s.toString();
}

From source file:com.dien.upload.server.UploadServlet.java

protected static void renderJSONResponse(HttpServletRequest request, HttpServletResponse response,
        String message) throws IOException {

    //json????//from w  w  w .  j a  va  2s .  c  o  m

    response.setContentType("text/html;charset=utf-8");
    PrintWriter out;
    try {
        out = response.getWriter();
        out.write(message);
        out.flush();
        out.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:com.adito.util.Utils.java

/**
 * @deprecated use org.apache.commons.lang.StringUtils.join()
 **//*from ww w .j a  v a2 s . com*/
public static String commaList(Iterator i) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    printCommaList(pw, i);
    pw.close();
    return sw.toString();
}