Example usage for java.io BufferedWriter close

List of usage examples for java.io BufferedWriter close

Introduction

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

Prototype

@SuppressWarnings("try")
    public void close() throws IOException 

Source Link

Usage

From source file:edu.american.student.stonewall.util.Deployer.java

private static void save(String source, String name, File destination) throws IOException {
    if (!destination.exists()) {
        destination.mkdirs();// w  w  w.  j av  a 2 s .c  o  m
    }
    // Create file
    FileWriter fstream = new FileWriter(destination.getAbsolutePath() + File.separator + name);
    BufferedWriter out = new BufferedWriter(fstream);
    out.write(source);
    // Close the output stream
    out.close();
}

From source file:Main.java

public static boolean saveStringToFile(String fileName, String saveString) {

    boolean saved = false;
    BufferedWriter bw = null;

    try {/*  w w w  . j av  a 2  s .co  m*/
        bw = new BufferedWriter(new FileWriter(fileName));

        try {
            bw.write(saveString);
            saved = true;
        } finally {
            bw.close();
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }

    return saved;
}

From source file:edu.isi.webserver.FileUtil.java

public static void writeStringToFile(String string, String fileName) throws IOException {
    BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
    writer.write(string);//from  w  w w.  j a va  2s.  c  o  m
    writer.flush();
    writer.close();
}

From source file:com.joey.Fujikom.generate.Generate.java

/**
 * //www. ja va  2 s .co  m
 * @param content
 * @param filePath
 */
public static void writeFile(String content, String filePath) {
    try {
        if (FileUtils.createFile(filePath)) {
            FileOutputStream fos = new FileOutputStream(filePath);
            Writer writer = new OutputStreamWriter(fos, "UTF-8");
            BufferedWriter bufferedWriter = new BufferedWriter(writer);
            bufferedWriter.write(content);
            bufferedWriter.close();
            writer.close();
        } else {
            logger.info("??");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void WriteAllText(String path, String text) throws IOException {
    BufferedWriter writer = null;
    try {//from  w w w.  j  a  v  a  2s  . c  om
        FileWriter output = new FileWriter(path);
        writer = new BufferedWriter(output);
        writer.write(text);
    } finally {
        if (writer != null)
            writer.close();
    }
}

From source file:MainClass.java

public static void Converter(String input, String output) {
    try {/*  w w  w.  j a  v a2s  .  c o m*/
        FileInputStream fis = new FileInputStream(new File(input));
        BufferedReader in = new BufferedReader(new InputStreamReader(fis, "SJIS"));

        FileOutputStream fos = new FileOutputStream(new File(output));
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(fos, "UTF8"));

        int len = 80;
        char buf[] = new char[len];

        int numRead;
        while ((numRead = in.read(buf, 0, len)) != -1)
            out.write(buf, 0, numRead);

        out.close();
        in.close();
    } catch (IOException e) {
        System.out.println("An I/O Exception Occurred: " + e);
    }
}

From source file:com.vaadin.buildhelpers.CompileTheme.java

private static void processSassTheme(String themeFolder, String themeName, String variant, String version)
        throws Exception {

    StringBuffer cssHeader = new StringBuffer();

    version = version.replaceAll("\\.", "_");
    cssHeader.append(".v-theme-version:after {content:\"" + version + "\";}\n");
    cssHeader.append(".v-theme-version-" + version + " {display: none;}\n");

    String stylesCssDir = themeFolder + File.separator + themeName + File.separator;

    String stylesCssName = stylesCssDir + variant + ".css";

    // Process as SASS file
    String sassFile = stylesCssDir + variant + ".scss";

    ScssStylesheet scss = ScssStylesheet.get(sassFile);
    if (scss == null) {
        throw new IllegalArgumentException("SASS file: " + sassFile + " not found");
    }//from w  w w .  j a  v  a 2 s . c  o m
    scss.compile();

    BufferedWriter out = new BufferedWriter(new FileWriter(stylesCssName));
    out.write(cssHeader.toString());
    out.write(scss.toString());
    out.close();

    System.out.println("Compiled CSS to " + stylesCssName + " (" + scss.toString().length() + " bytes)");

    createSprites(themeFolder, themeName);
    File oldCss = new File(stylesCssName);
    File newCss = new File(stylesCssDir + variant + "-sprite.css");

    if (newCss.exists()) {
        // Theme contained sprites. Renamed "styles-sprite.css" ->
        // "styles.css"
        oldCss.delete();

        boolean ok = newCss.renameTo(oldCss);
        if (!ok) {
            throw new RuntimeException("Rename " + newCss + " -> " + oldCss + " failed");
        }
    }

}

From source file:Main.java

public static void writeFile(String filename, String text) throws Exception {
    File f = new File(filename);
    if (!f.exists())
        f.createNewFile();/*w ww  .  j a v  a 2  s  .  co m*/
    else {
        f.delete();
        f.createNewFile();
    }
    BufferedWriter output = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename), "utf-8"));

    // new BufferedWriter(new FileWriter(filename,
    // false),"utf-8");
    output.write(text);

    output.flush();
    output.close();
}

From source file:Main.java

public static void exportXmlFile(ArrayList<?> listObject, String rootElement, String interfaceName,
        String pathSaveFile) {/*from w w w . j  a v  a2  s .  c o  m*/
    try {
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = builderFactory.newDocumentBuilder();
        // creating a new instance of a DOM to build a DOM tree.
        Document doc = docBuilder.newDocument();

        Element root = doc.createElement(rootElement);
        // adding a node after the last child node of the specified node.
        doc.appendChild(root);

        Element interfaceElement = null;

        Element child = null;
        Text text;

        for (Object obj : listObject) {
            Class srcClass = obj.getClass();
            Field[] field = srcClass.getFields();
            interfaceElement = doc.createElement(interfaceName);
            for (int i = 0; i < field.length; i++) {
                // System.out.println(field[i].getName() + ":" +
                // field[i].get(obj));
                child = doc.createElement(field[i].getName());

                text = doc.createTextNode((field[i].get(obj)).toString());
                child.appendChild(text);

                interfaceElement.appendChild(child);
            }
            root.appendChild(interfaceElement);
        }

        // TransformerFactory instance is used to create Transformer
        // objects.
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();

        transformer.setOutputProperty(OutputKeys.INDENT, "yes");

        // create string from xml tree
        StringWriter sw = new StringWriter();
        StreamResult result = new StreamResult(sw);
        DOMSource source = new DOMSource(doc);
        transformer.transform(source, result);
        String xmlString = sw.toString();

        File file = new File(pathSaveFile);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
        bw.write(xmlString);
        bw.flush();
        bw.close();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:Main.java

public static void writeListToFile(String filepath, List<String> data) {
    try {//  w w w.  j av a2s .c o m
        File file = new File(filepath);
        if (!file.exists()) {
            file.createNewFile();
        }

        FileWriter fileWriter = new FileWriter(file);
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
        for (String line : data) {
            bufferedWriter.write(line);
            bufferedWriter.newLine();
        }

        bufferedWriter.flush();
        bufferedWriter.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}