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.sec.ose.osi.util.tools.FileOperator.java

public static boolean saveFile(String filePath, String contents) {

    File writeFile = new File(filePath);

    PrintWriter writer = null;

    try {//from w w  w  . j  ava 2  s  .  c  o  m
        writer = new PrintWriter(new FileWriter(writeFile));
        writer.println(contents);

    } catch (IOException e) {
        // TODO Auto-generated catch block
        log.warn(e);
        return false;
    } finally {

        if (writer != null) {
            writer.flush();
            writer.close();
        }
        writer = null;
    }

    return true;
}

From source file:sample.server.NativeGemFireServer.java

private static void writeStringTo(File file, String fileContents) {
    PrintWriter fileWriter = null;

    try {// ww  w  . ja  va2 s .c om
        fileWriter = new PrintWriter(new BufferedWriter(new FileWriter(file, true)), true);
        fileWriter.println(fileContents);
        fileWriter.flush();
    } catch (IOException e) {
        throw new RuntimeException(String.format("Failed to write [%s] to file [%s]", fileContents, file), e);
    } finally {
        if (fileWriter != null) {
            fileWriter.close();
        }
    }
}

From source file:Main.java

/**
 * dump bipartite community affiliation into a text file with node names
 * //  ww w  .ja v  a2 s.  com
 * @param OutFNm
 * @param CmtyVV
 * @param NIDNmH
 */
static void dumpCmtyVV(final String OutFNm, Vector<Vector<Integer>> CmtyVV, Hashtable<Integer, String> NIDNmH) {
    PrintWriter f;
    try {
        f = new PrintWriter(OutFNm);

        for (int c = 0; c < CmtyVV.size(); c++) {
            for (int u = 0; u < CmtyVV.get(c).size(); u++) {
                if (NIDNmH.containsKey(CmtyVV.get(c).get(u))) {
                    f.printf("%s\t", NIDNmH.get(CmtyVV.get(c).get(u)));
                } else {
                    f.printf("%d\t", (int) CmtyVV.get(c).get(u));
                }
            }
            f.printf("\n");
        }
        f.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

From source file:com.google.gsa.valve.modules.utils.HTTPAuthZProcessor.java

/**
 * If the document is HTML, this method processes its content in order to 
 * rewrite the URLs it includes//w  w  w  .j a va 2s.  c om
 * 
 * @param response HTTP response
 * @param method HTTP method
 * @param url document url
 * @param loginUrl login url
 * @param contenType content Type
 * 
 * @throws IOException
 * @throws ParserException
 */
public static void processHTML(HttpServletResponse response, HttpMethodBase method, String url, String loginUrl,
        String contentType) throws IOException, ParserException {
    logger.debug("Processing an HTML document");

    String stream = null;
    Parser parser = null;
    NodeVisitor visitor = null;

    // Retrieve HTML stream
    stream = readFully(new InputStreamReader(method.getResponseBodyAsStream()));

    // Protection
    if (stream != null) {
        logger.debug("Stream content size: " + stream.length());
        // Parse HTML stream to replace any links to include the path to the valve
        parser = Parser.createParser(stream, null);

        // Instantiate visitor
        visitor = new HTTPVisitor(url, loginUrl);
        // Parse nodes
        parser.visitAllNodesWith(visitor);

        // Get writer
        PrintWriter out = response.getWriter();

        // Push HTML content
        if (out != null) {
            out.flush();
            out.print(((HTTPVisitor) visitor).getModifiedHTML());
            out.close();
            logger.debug("Wrote: " + ((HTTPVisitor) visitor).getModifiedHTML().length());
        }

        response.setHeader("Content-Type", contentType);

        //  Garbagge collect
        stream = null;
        parser = null;
        visitor = null;
    }
}

From source file:io.apiman.tools.i18n.TemplateScanner.java

/**
 * Output the sorted map of strings to the specified output file.
 * @param strings//from  w ww . j  ava 2s.c  o  m
 * @param outputFile
 * @throws FileNotFoundException
 */
private static void outputMessages(TreeMap<String, String> strings, File outputFile)
        throws FileNotFoundException {
    PrintWriter writer = new PrintWriter(new FileOutputStream(outputFile));
    for (Entry<String, String> entry : strings.entrySet()) {
        String key = entry.getKey();
        String val = entry.getValue();
        writer.append(key);
        writer.append('=');
        writer.append(val);
        writer.append("\n");
    }
    writer.flush();
    writer.close();
}

From source file:com.google.gsa.valve.modules.utils.HTTPAuthZProcessor.java

/**
 * Includes the HTML document in the response
 * /*from w  ww . ja  v a  2  s .  c  o m*/
 * @param response HTTP response
 * @param method HTTP method
 * @param url document url
 * @param loginUrl login url
 * @param contentType content type
 * 
 * @throws IOException
 */
public static void returnHTML(HttpServletResponse response, HttpMethodBase method, String url, String loginUrl,
        String contentType) throws IOException {

    logger.debug("Returning an HTML document");

    //Get writer
    PrintWriter out = null;

    try {
        out = response.getWriter();
        if (out != null) {
            //set content
            out.print(method.getResponseBodyAsString());
            //close writer
            out.close();
            response.setHeader("Content-Type", contentType);
        }
    } catch (Exception e) {
        logger.error("Error when returning HTML content: " + e.getMessage(), e);
        //protection
        if (out != null) {
            out.close();
        }
    }

}

From source file:eu.annocultor.tools.GeneratorOfXmlSchemaForConvertersDoclet.java

static void printRuleDocEnd(PrintWriter out) throws Exception {
    out.println(" </body>");
    out.println("</document>");
    out.println();/*from  w ww  .java 2s. com*/
    out.flush();
    out.close();
}

From source file:mx.unam.ecologia.gye.coalescence.app.CreateMicsatInput.java

protected static final void writeMicsat(List leaves, String fname) {
    try {/*from w  w  w.j ava2 s .c o m*/

        FileOutputStream fout = new FileOutputStream(fname);
        PrintWriter pw = new PrintWriter(fout);
        for (int i = 0; i < leaves.size(); i++) {
            UniParentalGene upgene = (UniParentalGene) leaves.get(i);
            CompoundSequence h = upgene.getCompoundSequence();
            int numloc = h.getSequenceCount();
            for (int n = 0; n < numloc; n++) {
                Sequence s = h.get(n);
                pw.print(s.getSize());
                pw.print(" ");
            }
            pw.println();
        }
        pw.flush();
        pw.close();
        fout.close();
    } catch (IOException ex) {
        log.error("writeMicsat()", ex);
    }

}

From source file:jenkins.plugins.coverity.CoverityUtils.java

/**
 * Gets the stacktrace from an exception, so that this exception can be handled.
 *//*w w w .j  a v  a2 s  .  c o m*/
public static String getStackTrace(Exception e) {
    StringWriter writer = new StringWriter();
    PrintWriter printWriter = new PrintWriter(writer);
    e.printStackTrace(printWriter);
    printWriter.flush();
    String stackTrace = writer.toString();
    try {
        writer.close();
        printWriter.close();
    } catch (IOException e1) {
    }
    return stackTrace;
}

From source file:com.wavemaker.common.util.StringUtils.java

public static String toString(Throwable th) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    th.printStackTrace(pw);/* www.j  a v a  2s .  c  o m*/
    String rtn = sw.toString();
    pw.close();
    return rtn;
}