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:com.netcrest.pado.internal.security.AESCipher.java

private static byte[] getUserPrivateKey() throws IOException {
    byte[] privateKey = null;

    String estr;//w  w  w. j av a2s  .co m
    String certificateFilePath = PadoUtil.getProperty(Constants.PROP_SECURITY_AES_USER_CERTIFICATE,
            "security/user.cer");
    if (certificateFilePath.startsWith("/") == false) {

        // TODO: Make server files relative to PADO_HOME also.
        if (PadoUtil.isPureClient()) {
            String padoHome = PadoUtil.getProperty(Constants.PROP_HOME_DIR);
            certificateFilePath = padoHome + "/" + certificateFilePath;
        }
    }
    File file = new File(certificateFilePath);
    if (file.exists() == false) {
        FileWriter writer = null;
        try {
            privateKey = AESCipher.getPrivateKey();
            Base64 base64 = new Base64(0); // no line breaks
            estr = base64.encodeToString(privateKey);
            writer = new FileWriter(file);
            writer.write(estr);
        } finally {
            if (writer != null) {
                writer.close();
            }
        }
    } else {
        FileReader reader = null;
        try {
            reader = new FileReader(file);
            StringBuffer buffer = new StringBuffer(2048);
            int c;
            while ((c = reader.read()) != -1) {
                buffer.append((char) c);
            }
            estr = buffer.toString();
        } finally {
            if (reader != null) {
                reader.close();
            }
        }
    }
    Base64 base64 = new Base64(0); // no line breaks
    privateKey = base64.decode(estr);
    return privateKey;
}

From source file:com.alibaba.wasp.ZNodeClearer.java

/**
 * Logs the errors without failing on exception.
 *///from  ww w.j  a  va  2 s.c o  m
public static void writeMyEphemeralNodeOnDisk(String fileContent) {
    String fileName = ZNodeClearer.getMyEphemeralNodeFileName();

    if (fileName == null) {
        LOG.warn("No filename given to save the znode used, it won't be saved "
                + "(Environment variable WASP_ZNODE_FILE is not set).");
        return;
    }

    FileWriter fstream;
    try {
        fstream = new FileWriter(fileName);
    } catch (IOException e) {
        LOG.warn("Can't write znode file " + fileName, e);
        return;
    }

    BufferedWriter out = new BufferedWriter(fstream);

    try {
        try {
            out.write(fileContent + "\n");
        } finally {
            try {
                out.close();
            } finally {
                fstream.close();
            }
        }
    } catch (IOException e) {
        LOG.warn("Can't write znode file " + fileName, e);
    }
}

From source file:com.technofovea.packbsp.AppModel.java

public static void savePackList(final Map<String, File> items, final File packListFile) throws IOException {

    logger.info("Packing list will be written to: {}", packListFile.getAbsolutePath());
    FileWriter fw = new FileWriter(packListFile, false);
    BspZipController.writePackingList(items, fw);
    fw.close();

}

From source file:com.thejustdo.util.Utils.java

/**
 * Given some data it creates a new file inside the servlet context.
 *
 * @param location Where to create the file.
 * @param ctx Gives us the real paths to be used.
 * @param content What to write.//from   ww w  .java 2  s.  c  o  m
 * @param filename What name will have the new file.
 * @return The created file.
 * @throws IOException If any errors.
 */
public static File createNewFileInsideContext(String location, ServletContext ctx, String content,
        String filename) throws IOException {
    String real = ctx.getContextPath();
    String path = real + location + filename;
    log.info(String.format("File to save: %s", path));
    File f = new File(path);

    // 1. Creating the writers.
    FileWriter fw = new FileWriter(f);
    BufferedWriter writer = new BufferedWriter(fw);

    // 2. Writing contents.
    writer.write(content);

    // 3. Closing stream.
    writer.flush();
    writer.close();
    fw.close();

    return f;
}

From source file:SwiftWork.java

private static void appendToLocalTmpList(String file, boolean close) {
    File logFile = new File(getBenchITTemp(), LOCAL_TEMP_LOG_FILE);
    try {/*from   w ww . j  av  a  2 s  .c o  m*/
        FileWriter fileWriter = new FileWriter(logFile, true);
        fileWriter.append(file + '\n');
        if (close)
            fileWriter.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:SwiftWork.java

private static void appendToRemoteTmpList(String object, boolean close) {
    File logFile = new File(getBenchITTemp(), REMOTE_TEMP_LOG_FILE);
    try {//from  ww  w  .j ava  2s  . c om
        FileWriter fileWriter = new FileWriter(logFile, true);
        fileWriter.append(object + '\n');
        if (close)
            fileWriter.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:it.intecs.pisa.toolbox.util.URLReader.java

public static void writeURLContentToXML(String host, String url, int port, String fileName) throws Exception {
    try {/*from w  w  w  . j av  a 2 s .c o m*/
        String fileContent = (String) getURLContent(host, url, port);
        if (fileContent == null)
            return;
        fileContent = fileContent.substring(fileContent.indexOf("<"), fileContent.lastIndexOf(">") + 1);
        File newFile = new File(fileName);
        FileWriter fw = new FileWriter(newFile);
        fw.write(fileContent);
        fw.close();
    } catch (Exception e) {
        e.printStackTrace(System.out);
        return;
    }
}

From source file:com.dc.util.file.FileSupport.java

public static void writeTextFile(String folderPath, String fileName, String data, boolean isExecutable)
        throws IOException {
    File dir = new File(folderPath);
    if (!dir.exists()) {
        dir.mkdirs();/* w w  w . j a  v  a2s  .  c  o m*/
    }
    File file = new File(dir, fileName);
    FileWriter fileWriter = null;
    BufferedWriter bufferedWriter = null;
    try {
        fileWriter = new FileWriter(file);
        bufferedWriter = new BufferedWriter(fileWriter);
        bufferedWriter.write(data);
        bufferedWriter.flush();
        if (isExecutable) {
            file.setExecutable(true);
        }
    } finally {
        if (fileWriter != null) {
            fileWriter.close();
        }
        if (bufferedWriter != null) {
            bufferedWriter.close();
        }
    }
}

From source file:com.gumtreescraper.scraper.GumtreeScraper.java

public static void writeToCsvFile(List<Gumtree> gumtreesNeedToWrite, String outputCsvFileName) {
    try {//from  w w w.  j  a  va  2s  . c  o  m
        boolean isAppend = true;
        FileWriter writer = new FileWriter(outputCsvFileName, isAppend);
        for (Gumtree gumtree : gumtreesNeedToWrite) {
            writer.append(gumtree.toString()).append("\n");
        }

        writer.flush();
        writer.close();
    } catch (IOException ex) {
        Logger.getLogger(GumtreeScraper.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:com.intuit.tank.proxy.Main.java

static SSLContextSelector getSSLContextSelector() throws GeneralSecurityException, IOException {
    File ks = new File("auto_generated_ca.p12");
    String type = "PKCS12";
    char[] password = "password".toCharArray();
    String alias = "CA";
    if (ks.exists()) {
        try {/*from  ww  w  .  j  a  va 2s  . c o m*/
            return new AutoGeneratingContextSelector(ks, type, password, password, alias);
        } catch (GeneralSecurityException e) {
            System.err.println("Error loading CA keys from keystore: " + e.getLocalizedMessage());
        } catch (IOException e) {
            System.err.println("Error loading CA keys from keystore: " + e.getLocalizedMessage());
        }
    }
    System.err.println("Generating a new CA");
    X500Principal ca = new X500Principal(
            "cn=OWASP Custom CA for Tank,ou=Tank Custom CA,o=Tank,l=Tank,st=Tank,c=Tank");
    AutoGeneratingContextSelector ssl = new AutoGeneratingContextSelector(ca);
    try {
        ssl.save(ks, type, password, password, alias);
    } catch (GeneralSecurityException e) {
        System.err.println("Error saving CA keys to keystore: " + e.getLocalizedMessage());
    } catch (IOException e) {
        System.err.println("Error saving CA keys to keystore: " + e.getLocalizedMessage());
    }
    FileWriter pem = null;
    try {
        pem = new FileWriter("auto_generated_ca.pem");
        pem.write(ssl.getCACert());
    } catch (IOException e) {
        System.err.println("Error writing CA cert : " + e.getLocalizedMessage());
    } finally {
        if (pem != null)
            pem.close();
    }
    return ssl;
}