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:Base64.java

private static void writeChars(final File file, final char[] data) {
    try {//from w  ww .ja  v a2 s .  c o m
        final Writer fos = new FileWriter(file);
        final Writer os = new BufferedWriter(fos);
        os.write(data);
        os.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.moss.appsnap.keeper.freedesktop.FreeDesktopAppHandler.java

private static void write(String text, File dest) throws IOException {
    Writer writer = new FileWriter(dest);
    writer.write(text);/*from   w ww.j av  a2 s .  c  om*/
    writer.close();
}

From source file:ie.pars.bnc.preprocess.MainBNCProcess.java

private static void getZippedFile() throws IOException, ArchiveException, Exception {
    String taggerPath = "edu/stanford/nlp/models/pos-tagger/english-left3words/english-left3words-distsim.tagger";
    String parseModel = LexicalizedParser.DEFAULT_PARSER_LOC;

    InputStream is = new FileInputStream(pathInput);
    TarArchiveInputStream tarStream = (TarArchiveInputStream) new ArchiveStreamFactory()
            .createArchiveInputStream("tar", is);
    TarArchiveEntry entry = null;/*  w w w.  j a  va  2 s.  c o m*/
    int countfiles = 0;
    while ((entry = (TarArchiveEntry) tarStream.getNextEntry()) != null) {
        //     for(File lf: listFiles){ 
        if (!entry.isDirectory()) {

            byte[] content = new byte[(int) entry.getSize()];
            int offset = 0;
            tarStream.read(content, offset, content.length - offset);
            String id = entry.getName().split("/")[entry.getName().split("/").length - 1].split(".xml")[0];

            if (!filesProcesed.contains(id) && id.startsWith(letter.toUpperCase())) {
                if (countfiles++ % 10 == 0) {
                    tagger = new MaxentTagger(taggerPath);
                    m = new Morphology();
                    parser = ParserGrammar.loadModel(parseModel);
                    parser.loadTagger();
                }
                System.out.print("Entry " + entry.getName());

                InputStream bis = new ByteArrayInputStream(content);
                StringBuilder parseBNCXML = ProcessNLP.parseBNCXML(bis, m, tagger, parser);
                bis.close();
                OutputStream out = new FileOutputStream(pathOutput + File.separatorChar + id + ".vert");
                Writer writer = new OutputStreamWriter(out, "UTF-8");

                writer.write("<text id=\"" + id + "\">\n");
                writer.write(parseBNCXML.toString());
                writer.write("</text>\n");
                writer.close();
                out.close();
            } else {
                System.out.println(">> Bypass Entry " + entry.getName());
            }
            //break;
        }

    }
    is.close();
    System.out.println("There are " + countfiles);
    //    tarStream.close();

}

From source file:cat.tv3.eng.rec.recomana.lupa.visualization.RecommendationToJson.java

private static void saveResults(JSONArray recomendations, String id) {
    Writer out;
    try {/* w  ww. j a v  a 2  s .  c o m*/
        out = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream("data_toVisualize/data_recommendation/recommendation_" + id + ".json"),
                "UTF-8"));
        try {
            out.write(recomendations.toJSONString());
            out.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

From source file:com.linkedin.restli.datagenerator.csharp.CSharpRythmGenerator.java

private static void writeToFile(File file, String content) throws IOException {
    Writer fileWriter = null;
    try {//from ww  w  . ja v  a2s. c om
        fileWriter = new BufferedWriter(new FileWriter(file));
        fileWriter.write(content);
    } finally {
        if (fileWriter != null) {
            fileWriter.close();
        }
    }
}

From source file:com.magnet.mmx.tsung.GenTestScript.java

public static void generateLoginScript(Settings settings) throws IOException, TemplateException {

    String xmlMessage = StringEscapeUtils.escapeXml(generateDevRegMessageStanza());

    Configuration cfg = new Configuration();

    cfg.setDirectoryForTemplateLoading(new File(settings.templateDir));
    cfg.setObjectWrapper(new DefaultObjectWrapper());

    cfg.setDefaultEncoding("UTF-8");
    cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);

    Map root = new HashMap();
    root.put("mmxHostname", settings.hostname);
    root.put("mmxPort", settings.port);
    Template temp = cfg.getTemplate(LOGIN_TEMPLATE_FILE);

    File file = new File(settings.outputDir + "/loadtest_login.xml");
    Writer out = new OutputStreamWriter(new FileOutputStream(file));
    temp.process(root, out);//  ww w  . j ava2 s  .c o  m
    out.close();
}

From source file:jsdp.app.lotsizing.sS_jsdp.java

public static void plotCostFunction(int targetPeriod, double fixedOrderingCost, double proportionalOrderingCost,
        double holdingCost, double penaltyCost, Distribution[] distributions, double minDemand,
        double maxDemand, boolean printCostFunctionValues, boolean latexOutput,
        sS_StateSpaceSampleIterator.SamplingScheme samplingScheme, int maxSampleSize) {

    sS_BackwardRecursion recursion = new sS_BackwardRecursion(distributions, minDemand, maxDemand,
            fixedOrderingCost, proportionalOrderingCost, holdingCost, penaltyCost, samplingScheme,
            maxSampleSize);//from  ww  w.  jav  a  2  s. co m
    recursion.runBackwardRecursion(targetPeriod);
    XYSeries series = new XYSeries("(s,S) policy");
    for (double i = sS_State.getMinInventory(); i <= sS_State.getMaxInventory(); i += sS_State.getStepSize()) {
        sS_StateDescriptor stateDescriptor = new sS_StateDescriptor(targetPeriod, sS_State.inventoryToState(i));
        series.add(i, recursion.getExpectedCost(stateDescriptor));
        if (printCostFunctionValues)
            System.out.println(i + "\t" + recursion.getExpectedCost(stateDescriptor));
    }
    XYDataset xyDataset = new XYSeriesCollection(series);
    JFreeChart chart = ChartFactory.createXYLineChart(
            "(s,S) policy - period " + targetPeriod + " expected total cost", "Opening inventory level",
            "Expected total cost", xyDataset, PlotOrientation.VERTICAL, false, true, false);
    ChartFrame frame = new ChartFrame("(s,S) policy", chart);
    frame.setVisible(true);
    frame.setSize(500, 400);

    if (latexOutput) {
        try {
            XYLineChart lc = new XYLineChart("(s,S) policy", "Opening inventory level", "Expected total cost",
                    new XYSeriesCollection(series));
            File latexFolder = new File("./latex");
            if (!latexFolder.exists()) {
                latexFolder.mkdir();
            }
            Writer file = new FileWriter("./latex/graph.tex");
            file.write(lc.toLatex(8, 5));
            file.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

From source file:com.magnet.mmx.tsung.GenTestScript.java

public static void generateDevRegScript(Settings settings) throws IOException, TemplateException {

    String xmlMessage = StringEscapeUtils.escapeXml(generateDevRegMessageStanza());

    Configuration cfg = new Configuration();

    cfg.setDirectoryForTemplateLoading(new File(settings.templateDir));
    cfg.setObjectWrapper(new DefaultObjectWrapper());

    cfg.setDefaultEncoding("UTF-8");
    cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);

    Map root = new HashMap();
    root.put("mmxHostname", settings.hostname);
    root.put("mmxPort", settings.port);
    root.put("devreg_msg", xmlMessage);
    Template temp = cfg.getTemplate(DEVREG_TEMPLATE_FILE);

    File file = new File(settings.outputDir + "/loadtest_devreg.xml");
    Writer out = new OutputStreamWriter(new FileOutputStream(file));
    temp.process(root, out);/*from  ww  w. j  a va2s . c o m*/
    out.close();
}

From source file:com.magnet.mmx.tsung.GenTestScript.java

public static void generateSendMessageScript(Settings settings) throws IOException, TemplateException {

    String xmlMessage = StringEscapeUtils.escapeXml(generateSendMessageStanza());

    Configuration cfg = new Configuration();

    cfg.setDirectoryForTemplateLoading(new File(settings.templateDir));
    cfg.setObjectWrapper(new DefaultObjectWrapper());

    cfg.setDefaultEncoding("UTF-8");
    cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);

    Map root = new HashMap();
    root.put("mmxHostname", settings.hostname);
    root.put("mmxPort", settings.port);
    root.put("mmx_stanza", xmlMessage);
    Template temp = cfg.getTemplate(MMX_STANZA_TEMPLATE_FILE);

    File file = new File(settings.outputDir + "/loadtest_send_message.xml");
    Writer out = new OutputStreamWriter(new FileOutputStream(file));
    temp.process(root, out);//from  w w  w . j ava  2 s  .c  o m
    out.close();
}

From source file:Main.java

/**
 * Copy a reader to writer./* www . j  a  v a  2s .  c  om*/
 */
private static void copy(Reader in, Writer out) throws IOException {
    try {
        int bytesRead;
        char[] buffer = new char[COPY_BUFFER_SIZE];

        while ((bytesRead = in.read(buffer)) != -1) {
            out.write(buffer, 0, bytesRead);
        }

        out.flush();
    } finally {
        in.close();
        out.close();
    }
}