Example usage for java.io Writer close

List of usage examples for java.io Writer close

Introduction

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

Prototype

public abstract void close() throws IOException;

Source Link

Document

Closes the stream, flushing it first.

Usage

From source file:org.opennms.ng.services.snmpconfig.SnmpPeerFactory.java

public static void saveToFile(final File file)
        throws UnsupportedEncodingException, FileNotFoundException, IOException {
    // Marshal to a string first, then write the string to the file. This
    // way the original config
    // isn't lost if the XML from the marshal is hosed.
    final String marshalledConfig = marshallConfig();

    SnmpPeerFactory.getWriteLock().lock();
    FileOutputStream out = null;//from  ww  w  .  j a  va 2  s. c  o m
    Writer fileWriter = null;
    try {
        if (marshalledConfig != null) {
            out = new FileOutputStream(file);
            fileWriter = new OutputStreamWriter(out, "UTF-8");
            fileWriter.write(marshalledConfig);
            fileWriter.flush();
            fileWriter.close();
        }
    } finally {
        IOUtils.closeQuietly(fileWriter);
        IOUtils.closeQuietly(out);
        SnmpPeerFactory.getWriteLock().unlock();
    }
}

From source file:com.discursive.jccook.net.SMTPExample.java

public void start() throws SocketException, IOException {
    SMTPClient client = new SMTPClient();
    client.connect("www.discursive.com");
    int response = client.getReplyCode();
    if (SMTPReply.isPositiveCompletion(response)) {
        client.setSender("tobrien@discursive.com");
        client.addRecipient("tobrien@iesabroad.org");
        Writer message = client.sendMessageData();
        message.write("This is a test message");
        message.close();
        boolean success = client.completePendingCommand();
        if (success) {
            System.out.println("Message sent");
        }/*from   www . j  ava  2 s  . co m*/
    } else {
        System.out.println("Error communicating with SMTP server");
    }
    client.disconnect();
}

From source file:com.erudika.para.utils.Utils.java

/**
 * Compiles a mustache template with a given scope (map of fields and values).
 * @param scope a map of fields and values
 * @param template a Mustache template//from w  ww .ja v  a  2  s  .  c om
 * @return the compiled template string
 */
public static String compileMustache(Map<String, Object> scope, String template) {
    if (scope == null || StringUtils.isBlank(template)) {
        return "";
    }
    Writer writer = new StringWriter();
    try {
        mustache.compile(new StringReader(template), MD5(template)).execute(writer, scope);
    } finally {
        try {
            writer.close();
        } catch (IOException e) {
            logger.error(null, e);
        }
    }
    return writer.toString();
}

From source file:Main.java

/**
 * Write the entire contents of the supplied string to the given writer. This method always flushes and closes the writer when
 * finished.//from w  w  w  .j a  v  a 2  s .  co  m
 * 
 * @param input the content to write to the writer; may be null
 * @param writer the writer to which the content is to be written
 * @throws IOException
 * @throws IllegalArgumentException if the writer is null
 */
public static void write(Reader input, Writer writer) throws IOException {
    boolean error = false;
    try {
        if (input != null) {
            char[] buffer = new char[1024];
            try {
                int numRead = 0;
                while ((numRead = input.read(buffer)) > -1) {
                    writer.write(buffer, 0, numRead);
                }
            } finally {
                input.close();
            }
        }
    } catch (IOException e) {
        error = true; // this error should be thrown, even if there is an error flushing/closing writer
        throw e;
    } catch (RuntimeException e) {
        error = true; // this error should be thrown, even if there is an error flushing/closing writer
        throw e;
    } finally {
        try {
            writer.flush();
        } catch (IOException e) {
            if (!error)
                throw e;
        } finally {
            try {
                writer.close();
            } catch (IOException e) {
                if (!error)
                    throw e;
            }
        }
    }
}

From source file:miage.ecom.web.security.ExtJsAuthenticationSuccessHandler.java

@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
        Authentication authentication) throws IOException, ServletException {

    HttpServletResponseWrapper responseWrapper = new HttpServletResponseWrapper(response);

    Writer out = responseWrapper.getWriter();

    out.write("{success:true}");
    out.close();

}

From source file:modnlp.capte.AlignerUtils.java

public static void reWriteAlignment(String sourcename, String targetname, Vector<Object[]> d) {

    try {//w  w w  .  j av a2 s .com
        FileOutputStream op = new FileOutputStream(sourcename);
        Writer out = new OutputStreamWriter(op, "UTF-8");
        FileOutputStream sp = new FileOutputStream(targetname);
        Writer sout = new OutputStreamWriter(sp, "UTF-8");
        String source = "";
        String target = "";
        Object[] stemp;
        for (int i = 0; i < d.size(); i++) {
            stemp = d.get(i);
            source = (String) stemp[0];
            target = (String) stemp[1];
            source = clean(source);
            target = clean(target);
            out.write(source);
            out.write("\n");
            sout.write(target);
            sout.write("\n");
            System.out.println(source);
            System.out.println(target);

        }
        out.close();
        sout.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.dlecan.agreg.AgregResultsBot.java

public void neutraliserAlgo() throws Exception {
    Writer writer = new FileWriter(LOCK_FILE);
    writer.close();
}

From source file:de.atomfrede.tools.evalutation.tools.plot.util.PlotUtil.java

public static void saveChartAsSVG(File file, JFreeChart chart, int width, int height) throws Exception {
    Writer out = null;
    try {/*from   w  w w .ja  va  2 s .c o  m*/
        DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();

        String svgNS = "http://www.w3.org/2000/svg";
        org.w3c.dom.Document document = domImpl.createDocument(svgNS, "svg", null);

        SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
        svgGenerator.getGeneratorContext().setPrecision(6);

        chart.draw(svgGenerator, new Rectangle2D.Double(0, 0, width, height), null);

        boolean useCSS = true;
        out = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
        svgGenerator.stream(out, useCSS);

    } catch (UnsupportedEncodingException enc) {
        log.error(enc);
    } catch (FileNotFoundException fnf) {
        log.error(fnf);
    } catch (SVGGraphics2DIOException e) {
        log.error(e);
    } catch (DOMException dome) {
        log.error(dome);
    } finally {
        if (out != null)
            out.close();
    }

}

From source file:ca.uhn.fhir.rest.server.servlet.ServletRestfulResponse.java

@Override
public final Object sendWriterResponse(int status, String contentType, String charset, Writer writer)
        throws IOException {
    writer.close();
    return null;/*from  ww w  .j a v  a  2 s .c o m*/
}

From source file:com.ewcms.publication.task.generator.TemplateTaskBase.java

private void closeWriterQuietly(Writer writer) {
    try {//ww w .j a  v a  2s  .com
        writer.close();
    } catch (IOException e) {
        //Not instance
    }
}