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:info.magnolia.cms.security.SecurityUtil.java

public static void updateKeys(MgnlKeyPair keys) {
    // update filestore only when private key is present
    if (keys.getPrivateKey() != null) {
        String path = SystemProperty.getProperty(KEY_LOCATION_PROPERTY);
        try {//from w  w w .j a va2  s .c o  m
            Properties defaultProps = new Properties();
            defaultProps.put(PRIVATE_KEY, keys.getPrivateKey());
            defaultProps.put(PUBLIC_KEY, keys.getPublicKey());
            File keystore = new File(path);
            File parentFile = keystore.getParentFile();
            if (parentFile != null) {
                parentFile.mkdirs();
            }
            FileWriter writer = new FileWriter(keystore);
            String date = new SimpleDateFormat("dd.MMM.yyyy hh:mm").format(new Date());
            defaultProps.store(writer, "generated " + date + " by " + MgnlContext.getUser().getName());
            writer.close();
        } catch (FileNotFoundException e) {
            throw new SecurityException(
                    "Failed to store private key. Please make sure the key is located in " + path, e);
        } catch (IOException e) {
            throw new SecurityException(
                    "Failed to store private key. Please make sure the key is located in " + path, e);
        }
    }
    try {
        Session session = MgnlContext.getSystemContext().getJCRSession("config");
        session.getNode("/server/activation").setProperty("publicKey", keys.getPublicKey());
        session.save();
    } catch (RepositoryException e) {
        throw new SecurityException("Failed to store public key.", e);
    }
}

From source file:com.alibaba.rocketmq.common.MixAll.java

public static final void string2FileNotSafe(final String str, final String fileName) throws IOException {
    File file = new File(fileName);
    File fileParent = file.getParentFile();
    if (fileParent != null) {
        fileParent.mkdirs();/*from   w w  w  . j  a v  a 2 s. c  o  m*/
    }
    FileWriter fileWriter = null;

    try {
        fileWriter = new FileWriter(file);
        fileWriter.write(str);
    } catch (IOException e) {
        throw e;
    } finally {
        if (fileWriter != null) {
            try {
                fileWriter.close();
            } catch (IOException e) {
                throw e;
            }
        }
    }
}

From source file:com.sldeditor.test.SLDTestRunner.java

/**
 * Writes an InputStream to a temporary file.
 *
 * @param in the in//from  ww  w. ja  va  2 s  .co  m
 * @return the file
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static File stream2file(InputStream in) throws IOException {
    final File tempFile = File.createTempFile(PREFIX, SUFFIX);
    tempFile.deleteOnExit();
    try (FileOutputStream out = new FileOutputStream(tempFile)) {
        IOUtils.copy(in, out);
    }

    // Update the font for the operating system
    String newFont = getFontForOS();
    if (newFont.compareToIgnoreCase(DEFAULT_FONT) != 0) {
        BufferedReader br = new BufferedReader(new FileReader(tempFile));
        try {
            StringBuilder sb = new StringBuilder();
            String line = br.readLine();

            while (line != null) {
                sb.append(line.replace(DEFAULT_FONT, newFont));
                sb.append("\n");
                line = br.readLine();
            }
            try {
                FileWriter fileWriter = new FileWriter(tempFile);
                fileWriter.write(sb.toString());
                fileWriter.flush();
                fileWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        } finally {
            br.close();
        }
    }
    return tempFile;
}

From source file:uk.ac.horizon.aestheticodes.controllers.ExperienceListUpdater.java

public static void save(Context context, ExperienceListController experiences) {
    try {//from  ww  w  .  ja v a  2  s  .  c  o m
        final File experienceFile = new File(context.getFilesDir(), "experiences.json");
        final FileWriter writer = new FileWriter(experienceFile);
        final Collection<Experience> saveExperiences = new ArrayList<>();
        for (Experience experience : experiences.get()) {
            if (experience.getOp() != Experience.Operation.temp) {
                saveExperiences.add(experience);
            }
        }
        Gson gson = ExperienceParser.createParser();
        gson.toJson(saveExperiences, writer);

        writer.flush();
        writer.close();
    } catch (Exception e) {
        Log.e("", e.getMessage(), e);
    }
}

From source file:au.org.ala.layers.grid.GridCacheBuilder.java

private static void writeGroupHeader(File f, ArrayList<Grid> group) throws IOException {
    FileWriter fw = null;
    try {//  w  w  w  .  j av  a2 s.c o m
        fw = new FileWriter(f);
        for (int i = 0; i < group.size(); i++) {
            fw.write(group.get(i).filename);
            fw.write("\n");
        }
        fw.flush();
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    } finally {
        if (fw != null) {
            try {
                fw.close();
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
        }
    }
}

From source file:org.alfresco.bm.file.AbstractTestFileService.java

/**
 * Looks for a {@link #PROPERTIES_FILE properties file} containing the name of the fileset that
 * this server uses.  The fileset is therefore unique to every local data location.
 *//*from  ww  w  . j a v  a2  s  .c om*/
private static synchronized String getFileset(File mirrorDir) {
    Properties properties = new Properties();
    // See if there is a file with the properties present
    File propsFile = new File(mirrorDir, PROPERTIES_FILE);
    if (propsFile.exists()) {
        if (propsFile.isDirectory()) {
            throw new RuntimeException(
                    "Expected to find a properties file but found a directory: " + propsFile);
        }
        // Just read the server's unique key from it
        properties = new Properties();
        FileReader reader = null;
        try {
            reader = new FileReader(propsFile);
            properties.load(reader);
        } catch (IOException e) {
            throw new RuntimeException("Failed to load properties from file: " + propsFile, e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                }
            }
        }
    }

    // Read the property
    String fileset = properties.getProperty(PROPERTY_FILESET);
    if (fileset == null) {
        // We must write a value into the file
        fileset = UUID.randomUUID().toString();
        properties.put(PROPERTY_FILESET, fileset);
        // Write the properties back
        FileWriter writer = null;
        try {
            writer = new FileWriter(propsFile);
            properties.store(writer, "Auto-generated fileset name");
        } catch (IOException e) {
            throw new RuntimeException("Failed to write fileset to properties file: " + propsFile, e);
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                }
            }
        }
    }
    // Done
    return fileset;
}

From source file:Main.java

public static void writeContentToFile(String fileName, String contents) throws IOException {
    Log.d("writeContentToFile", fileName);
    File f = new File(fileName);
    f.getParentFile().mkdirs();//from  ww w . j  a  v a  2 s  . co m
    File tempFile = new File(fileName + ".tmp");
    FileWriter fw = new FileWriter(tempFile);
    BufferedWriter bw = new BufferedWriter(fw);
    int length = contents.length();
    if (length > 0) {
        bw.write(contents);
        //         int apart =  Math.min(length, 65536);
        //         int times = length / apart;
        //         for (int i = 0; i < times; i++) {
        //            bw.write(contents, i * apart, apart);
        //         }
        //         if (length % apart != 0) {
        //            bw.write(contents, times * apart, length - times * apart);
        //         }
        bw.flush();
        fw.flush();
        bw.close();
        fw.close();
        f.delete();
        tempFile.renameTo(f);
    }
}

From source file:com.blackberry.logtools.LogTools.java

public static void dosTounix(File f, File dos2unix) throws IOException {
    BufferedReader read = new BufferedReader(new FileReader(f));
    FileWriter fwri = new FileWriter(dos2unix);
    String line;/*  w w w . j a v a 2  s  .c o  m*/
    while ((line = read.readLine()) != null) {
        fwri.write(line + "\n");
    }
    fwri.flush();
    fwri.close();
    read.close();
}

From source file:gedi.util.FileUtils.java

public static void writeAllText(String text, File file) throws IOException {
    FileWriter fw = new FileWriter(file);
    fw.write(text);//from   ww w. ja  v  a 2 s . co  m
    fw.flush();
    fw.close();
}

From source file:ac.ucy.cs.spdx.license.License.java

/**
 * Saves the License passed as parameter in a text file inside the standard
 * directory.//  w ww.  java  2s .co  m
 * 
 * @param License
 */
public static void saveLicense(License l) {
    File text = new File("licensesText/" + l.getIdentifier() + ".txt");
    if (text.exists())
        return;
    FileWriter fw = null;
    BufferedWriter bw = null;
    try {
        text.createNewFile();
        fw = new FileWriter(text);
        bw = new BufferedWriter(fw);
        bw.write(l.getLicenseName() + "\n");
        bw.write(l.getIdentifier() + "\n");
        bw.write(l.getLicenseText());
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            bw.close();
            fw.close();
        } catch (IOException e) {
        }

    }

}