Example usage for java.io FileWriter close

List of usage examples for java.io FileWriter close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Usage

From source file:gov.nih.nci.caadapter.ws.AddNewScenario.java

/**
* Save the modified .map file.//from  www  .j  a  va 2s . c o  m
*
* @param domDoc .map file's dom tree
* @param outputFileName the target .map file you want save
*/
private void outputXML(Document domDoc, String outputFileName) throws JDOMException, IOException {
    // Create new DOMBuilder, using default parser
    DOMBuilder builder = new DOMBuilder();
    org.jdom.Document jdomDoc = builder.build(domDoc);

    XMLOutputter outputter = new XMLOutputter();
    File file = new File(outputFileName);
    FileWriter writer = new FileWriter(file);
    outputter.output(jdomDoc, writer);
    writer.close();
}

From source file:com.photon.phresco.plugins.DrupalPackage.java

private void writeBuildInfo(boolean isBuildSuccess) throws MojoExecutionException {

    try {// w  w w . j  a  v  a 2s.  com
        if (buildNumber != null) {
            buildNo = Integer.parseInt(buildNumber);
        }

        PluginUtils pu = new PluginUtils();
        BuildInfo buildInfo = new BuildInfo();
        List<String> envList = pu.csvToList(environmentName);
        if (buildNo > 0) {
            buildInfo.setBuildNo(buildNo);
        } else {
            buildInfo.setBuildNo(nextBuildNo);
        }
        buildInfo.setTimeStamp(getTimeStampForDisplay(currentDate));
        if (isBuildSuccess) {
            buildInfo.setBuildStatus(SUCCESS);
        } else {
            buildInfo.setBuildStatus(FAILURE);
        }
        buildInfo.setBuildName(zipName);
        buildInfo.setEnvironments(envList);
        buildInfoList.add(buildInfo);
        Gson gson = new Gson();
        FileWriter writer = new FileWriter(buildInfoFile);
        gson.toJson(buildInfoList, writer);
        writer.close();
    } catch (IOException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    }
}

From source file:com.cyberway.issue.crawler.scope.SeedCachingScopeTest.java

public void testNoScheme() throws IOException {
    final String NOSCHEME = "x.y.z";
    FileWriter fw = new FileWriter(this.seedsfile, true);
    // Write to new (last) line the URL.
    fw.write("\n");
    fw.write(NOSCHEME);//w w  w.  ja  va  2 s.  c om
    fw.flush();
    fw.close();
    boolean found = false;
    SeedCachingScope sl = new UnitTestSeedCachingScope(seedsfile);
    for (Iterator i = sl.seedsIterator(); i.hasNext();) {
        UURI uuri = (UURI) i.next();
        if (uuri.getHost() == null) {
            continue;
        }
        if (uuri.getHost().equals(NOSCHEME)) {
            found = true;
            break;
        }
    }
    assertTrue("Did not find " + NOSCHEME, found);
}

From source file:controllers.ControllerServlet.java

private void save() {
    String filePath;/*from w  w w  .j  a  v  a2s .  c  o  m*/
    filePath = "/Users/ivan/Desktop/ufns.json";
    JSONObject obj = UFNS.getInstance().getJSONObject();
    try {

        FileWriter file = new FileWriter(filePath);
        file.write(obj.toJSONString());
        file.flush();
        file.close();

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

}

From source file:de.pdark.dsmp.ProxyDownload.java

/**
 * Do the download.// w  ww  .  j  a  v a2  s.  co m
 * 
 * @throws IOException
 * @throws DownloadFailed
 */
public void download() throws IOException, DownloadFailed {
    if (!config.isAllowed(url)) {
        throw new DownloadFailed(
                "HTTP/1.1 " + HttpStatus.SC_FORBIDDEN + " Download denied by rule in DSMP config");
    }

    // If there is a status file in the cache, return it instead of trying it again
    // As usual with caches, this one is a key area which will always cause
    // trouble.
    // TODO There should be a simple way to get rid of the cached statuses
    // TODO Maybe retry a download after a certain time?
    File statusFile = new File(dest.getAbsolutePath() + ".status");
    if (statusFile.exists()) {
        try {
            FileReader r = new FileReader(statusFile);
            char[] buffer = new char[(int) statusFile.length()];
            int len = r.read(buffer);
            r.close();
            String status = new String(buffer, 0, len);
            throw new DownloadFailed(status);
        } catch (IOException e) {
            log.warn("Error writing 'File not found'-Status to " + statusFile.getAbsolutePath(), e);
        }
    }

    mkdirs();

    HttpClient client = new HttpClient();

    String msg = "";
    if (config.useProxy(url)) {
        Credentials defaultcreds = new UsernamePasswordCredentials(config.getProxyUsername(),
                config.getProxyPassword());
        AuthScope scope = new AuthScope(config.getProxyHost(), config.getProxyPort(), AuthScope.ANY_REALM);
        HostConfiguration hc = new HostConfiguration();
        hc.setProxy(config.getProxyHost(), config.getProxyPort());
        client.setHostConfiguration(hc);
        client.getState().setProxyCredentials(scope, defaultcreds);
        msg = "via proxy ";
    }
    log.info("Downloading " + msg + "to " + dest.getAbsolutePath());

    GetMethod get = new GetMethod(url.toString());
    get.setFollowRedirects(true);
    try {
        int status = client.executeMethod(get);

        log.info("Download status: " + status);
        if (0 == 1 && log.isDebugEnabled()) {
            Header[] header = get.getResponseHeaders();
            for (Header aHeader : header)
                log.debug(aHeader.toString().trim());
        }

        log.info("Content: " + valueOf(get.getResponseHeader("Content-Length")) + " bytes; "
                + valueOf(get.getResponseHeader("Content-Type")));

        if (status != HttpStatus.SC_OK) {
            // Remember "File not found"
            if (status == HttpStatus.SC_NOT_FOUND) {
                try {
                    FileWriter w = new FileWriter(statusFile);
                    w.write(get.getStatusLine().toString());
                    w.close();
                } catch (IOException e) {
                    log.warn("Error writing 'File not found'-Status to " + statusFile.getAbsolutePath(), e);
                }
            }
            throw new DownloadFailed(get);
        }

        File dl = new File(dest.getAbsolutePath() + ".new");
        OutputStream out = new BufferedOutputStream(new FileOutputStream(dl));
        IOUtils.copy(get.getResponseBodyAsStream(), out);
        out.close();

        File bak = new File(dest.getAbsolutePath() + ".bak");
        if (bak.exists())
            bak.delete();
        if (dest.exists())
            dest.renameTo(bak);
        dl.renameTo(dest);
    } finally {
        get.releaseConnection();
    }
}

From source file:JSON.WriteProductJSON.java

public int write() {
    products.put("products", details);
    try {/* w  w  w .j a v  a  2 s. c  om*/

        // Writing to a file  
        File file = new File("Path to products.json");
        file.createNewFile();
        FileWriter fileWriter = new FileWriter(file);
        System.out.println("Writing JSON object to file");
        System.out.println("-----------------------");
        System.out.print(products);

        fileWriter.write(products.toJSONString());
        fileWriter.flush();
        fileWriter.close();

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

From source file:de.uzk.hki.da.grid.CTIrodsCommandLineConnector.java

private void destroyTestFileOnLongTermStorage() throws IOException {
    File testFile = new File(testCollPhysicalPathOnLTA + "/urn.tar");
    FileWriter writer = new FileWriter(testFile, false);
    writer.write("Hallo Wie gehts? DESTROYED");
    writer.close();
}

From source file:de.uzk.hki.da.metadata.MetadataStructure.java

protected void writeDocumentToFile(org.jdom.Document doc, File file) throws IOException {
    logger.debug("Write Metadata Document : " + file.getAbsolutePath());
    XMLOutputter outputter = new XMLOutputter();
    outputter.setFormat(Format.getPrettyFormat());
    FileWriter fw = new FileWriter(file);
    outputter.output(doc, fw);/*from   w w w.  ja  v  a2s .  co  m*/
    fw.close();
}