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:forge.util.FileUtil.java

/**
 * <p>//from   w  w w.ja  va2s  .  co  m
 * writeFile.
 * </p>
 * 
 * @param file
 *            a {@link java.io.File} object.
 * @param data
 *            a {@link java.util.List} object.
 */
public static void writeFile(File file, Collection<?> data) {
    try {
        PrintWriter p = new PrintWriter(file);
        for (Object o : data) {
            p.println(o);
        }
        p.close();
    } catch (final Exception ex) {
        throw new RuntimeException("FileUtil : writeFile() error, problem writing file - " + file + " : " + ex);
    }
}

From source file:com.medlog.webservice.lifecycle.Security.java

public static String getStackTrace(Throwable t) {
    String stackTrace = null;//  w  w w . j a v a 2  s  .c  o  m
    try {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        t.printStackTrace(pw);
        pw.close();
        sw.close();
        stackTrace = sw.getBuffer().toString();
    } catch (Exception ex) {
    }
    return stackTrace;
}

From source file:fr.inria.maestro.lga.graph.model.impl.nodenamer.NodeNamerImpl.java

/**
 * writes the file from a given List/* w  w  w  .jav  a2s .  c o m*/
 *
 * @param file
 * @param lines
 * @throws IOException
 */
public static void save(final File file, final List<String> lines) throws IOException {
    final PrintWriter os = new PrintWriter(file, Encoding.DEFAULT_CHARSET_NAME); //default charset is utf-8
    try {
        for (final String line : lines) {
            os.println(line);
        }
    } finally {
        os.close();
    }
}

From source file:fr.jayasoft.ivy.Main.java

private static void outputCachePath(Ivy ivy, File cache, ModuleDescriptor md, String[] confs, String outFile) {
    try {/*from   w  ww . j a  va 2  s . co  m*/
        String pathSeparator = System.getProperty("path.separator");
        StringBuffer buf = new StringBuffer();
        XmlReportParser parser = new XmlReportParser();
        Collection all = new LinkedHashSet();
        for (int i = 0; i < confs.length; i++) {
            Artifact[] artifacts = parser.getArtifacts(md.getModuleRevisionId().getModuleId(), confs[i], cache);
            all.addAll(Arrays.asList(artifacts));
        }
        for (Iterator iter = all.iterator(); iter.hasNext();) {
            Artifact artifact = (Artifact) iter.next();
            buf.append(ivy.getArchiveFileInCache(cache, artifact).getCanonicalPath());
            if (iter.hasNext()) {
                buf.append(pathSeparator);
            }
        }
        PrintWriter writer = new PrintWriter(new FileOutputStream(outFile));
        writer.println(buf.toString());
        writer.close();
        System.out.println("cachepath output to " + outFile);

    } catch (Exception ex) {
        throw new RuntimeException("impossible to build ivy cache path: " + ex.getMessage(), ex);
    }
}

From source file:com.arsdigita.util.parameter.ParameterPrinter.java

private static void writeXML(final PrintWriter out) {
    out.write("<?xml version=\"1.0\"?>");
    out.write("<records>");

    final Iterator records = s_records.iterator();

    while (records.hasNext()) {
        writeRecord(((ParameterContext) records.next()), out);
    }/*from  w  ww .  ja  v a  2 s  .c  o m*/

    out.write("</records>");
    out.close();
}

From source file:com.sldeditor.tool.html.ExportHTML.java

/**
 * Save all html to folder./*from  w w  w . j  a va2s.c  o  m*/
 *
 * @param destinationFolder the destination folder
 * @param filename the filename
 * @param sldDataList the sld data list
 * @param backgroundColour the background colour
 */
public static void save(File destinationFolder, String filename, List<SLDDataInterface> sldDataList,
        Color backgroundColour) {
    if (!destinationFolder.exists()) {
        destinationFolder.mkdirs();
    }

    InputStream inputStream = ExportHTML.class.getResourceAsStream(HTML_TEMPLATE);

    if (inputStream == null) {
        ConsoleManager.getInstance().error(ExportHTML.class, "Failed to find html template");
    } else {
        String htmlTemplate = null;
        BufferedReader reader = null;
        try {
            File file = stream2file(inputStream);

            reader = new BufferedReader(new FileReader(file));
            String line = null;
            StringBuilder stringBuilder = new StringBuilder();
            String ls = System.getProperty("line.separator");

            while ((line = reader.readLine()) != null) {
                stringBuilder.append(line);
                stringBuilder.append(ls);
            }
            htmlTemplate = stringBuilder.toString();
        } catch (Exception e) {
            ConsoleManager.getInstance().exception(ExportHTML.class, e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    ConsoleManager.getInstance().exception(ExportHTML.class, e);
                }
            }
        }

        StringBuilder sb = new StringBuilder();
        sb.append("  <tr>\n");
        sb.append("    <th>Layer Name</th>\n");
        sb.append("    <th>Legend</th>\n");
        sb.append("  </tr>\n");

        for (SLDDataInterface sldData : sldDataList) {
            StyleWrapper styleWrapper = sldData.getStyle();

            String layerName = styleWrapper.getStyle();
            sb.append("  <tr>\n");

            sb.append(String.format("    <td>%s</td>\n", layerName));

            StyledLayerDescriptor sld = SLDUtils.createSLDFromString(sldData);

            if (sld != null) {
                String showHeading = null;
                String showFilename = null;

                List<String> legendFileNameList = new ArrayList<String>();

                boolean result = LegendManager.getInstance().saveLegendImage(backgroundColour, sld,
                        destinationFolder, layerName, showHeading, showFilename, legendFileNameList);

                if (result) {
                    String legendFilename = legendFileNameList.get(0);
                    sb.append(String.format("    <td><img src=\"%s\" alt=\"%s\" ></td>\n", legendFilename,
                            layerName));
                }
            }
            sb.append("  </tr>\n");
        }

        if (htmlTemplate != null) {
            htmlTemplate = htmlTemplate.replace(TEMPLATE_INSERT_CODE, sb.toString());

            PrintWriter out;
            try {
                File f = new File(destinationFolder, filename);
                out = new PrintWriter(f);
                out.println(htmlTemplate);
                out.close();
            } catch (FileNotFoundException e) {
                ConsoleManager.getInstance().exception(ExportHTML.class, e);
            }
        }
    }
}

From source file:Main.java

/**
 * make throwable to string.//from  w w  w .jav  a  2s.  c o  m
 */
public static String toString(Throwable throwable) {
    StringWriter stringWriter = new StringWriter();
    PrintWriter printWriter = new PrintWriter(stringWriter);
    printWriter.print(throwable.getClass().getName() + ": ");
    if (throwable.getMessage() != null) {
        printWriter.print(throwable.getMessage() + "\n");
    }
    printWriter.println();
    try {
        throwable.printStackTrace(printWriter);
        return stringWriter.toString();
    } finally {
        printWriter.close();
    }
}

From source file:com.ibm.zurich.Main.java

private static void writeToFile(String s, String fileName) throws FileNotFoundException {
    PrintWriter writer = null;
    try {// w ww. ja  v a 2 s.c om
        writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(fileName), StandardCharsets.UTF_8),
                true);
        writer.print(s);
    } finally {
        if (writer != null) {
            writer.close();
        }
    }
}

From source file:com.vmware.identity.samlservice.Shared.java

/**
 * Send HTTP response//ww  w. j  av a  2  s .  co  m
 * @param response
 * @param contentType
 * @param str
 * @throws IOException
 */
public static void sendResponse(HttpServletResponse response, String contentType, String str)
        throws IOException {
    response.setContentType(contentType);
    PrintWriter out = response.getWriter();
    out.println(str);
    out.close();
}

From source file:info.hieule.framework.laravel.utils.LaravelSecurityString.java

public static void updateSecurityString(FileObject configFile) throws IOException {
    List<String> lines = configFile.asLines();
    String newKey = RandomStringUtils.randomAlphanumeric(32);
    OutputStream outputStream = configFile.getOutputStream();
    PrintWriter pw = new PrintWriter(new OutputStreamWriter(outputStream, "UTF-8"), true); // NOI18N
    try {//from  w  w  w . j ava2s.com
        for (String line : lines) {
            if (line.contains(_SECURITY_STRING_LINE)) {
                line = String.format(_SECURITY_STRING_FORMAT, newKey);
            }
            pw.println(line);
        }
    } finally {
        outputStream.close();
        pw.close();
    }
}