Example usage for org.apache.commons.io FileUtils writeStringToFile

List of usage examples for org.apache.commons.io FileUtils writeStringToFile

Introduction

In this page you can find the example usage for org.apache.commons.io FileUtils writeStringToFile.

Prototype

public static void writeStringToFile(File file, String data, String encoding) throws IOException 

Source Link

Document

Writes a String to a file creating the file if it does not exist.

Usage

From source file:com.thoughtworks.go.config.ConfigMigrator.java

public static String migrate(String configXml) throws IOException {
    File tempFile = TestFileUtil.createTempFile("cruise-config.xml");
    FileUtils.writeStringToFile(tempFile, configXml, UTF_8);
    migrate(tempFile);/*from ww w. j  a va  2  s .c o  m*/
    String newConfigXml = FileUtils.readFileToString(tempFile, UTF_8);
    tempFile.delete();
    return newConfigXml;
}

From source file:com.ppcxy.cyfm.showcase.demos.utilities.io.IODemo.java

@Test
public void workWithFileContent() {
    File file = new File("woop.txt");
    File destFile = new File("bar.txt");
    try {// w  w w  . j  a  v a 2 s  . c  om
        // text -> file, Collection<String>, byte[] ->file
        FileUtils.writeStringToFile(file, "Hey sailor!\nHaha\n", "UTF-8");

        // inputstream -> file
        InputStream source = IOUtils.toInputStream("Hej", "UTF-8");
        FileUtils.copyInputStreamToFile(source, file);

        // ///////////////////////////
        // file -> outputstream
        System.out.println("copy File to outputstream:");
        FileUtils.copyFile(file, System.out);

        // file -> file
        FileUtils.copyFile(file, destFile);

        // file -> string
        System.out.println("File to String:");
        System.out.println(FileUtils.readFileToString(file, "UTF-8"));

        // file -> list<string>
        System.out.println("File to List<String>:");
        List<String> lines = FileUtils.readLines(file, "UTF-8");
        for (String string : lines) {
            System.out.println(string);
        }
    } catch (IOException e) {
        Exceptions.unchecked(e);
    }
}

From source file:com.xxg.jdeploy.util.FileUtil.java

/**
 * Writes a file from a string with a specified encoding.
 *
 * @param path//from w  w w  . j a  v  a  2  s .  c  o m
 * @param name
 * @param encoding
 * @param s
 * @throws IOException
 */
public static void writeString(String path, String name, String encoding, String s) throws IOException {
    String fileName = getPatchedFileName(path, name);
    if (UtilValidate.isEmpty(fileName)) {
        throw new IOException("Cannot obtain buffered writer for an empty filename!");
    }

    try {
        FileUtils.writeStringToFile(new File(fileName), s, encoding);
    } catch (IOException e) {
        throw e;
    }
}

From source file:mpstyle.mwat.model.filesystem.file.FileBook.java

/**
 * The method saves the <i>content</i> in a file with name <i>fileName</i>,
 * in the folder path <i>folderPath</i> using the encode <i>fileEncode</i>.<br>
 * If it is necessary, it creates the path.<br>
 * It returns <i>true</i> if the task is completed without errors, otherwise
 * <i>false</i>.// w ww  .ja v  a  2 s. c om
 *
 * @param content The content of the file.
 * @param fileName The name of the file to save.
 * @param folderPath The folder of the path where to save the file.
 * @param fileEncode The encode to use to save the file.
 * @return The result of the operation.
 *
 * @throws Exception
 */
public static boolean saveFile(String content, String fileName, String folderPath, String fileEncode) {
    try {
        FolderBook.createFolder(new File(folderPath));
        final File f = new File(folderPath + File.separator + fileName);
        FileUtils.writeStringToFile(f, content, fileEncode);

        return true;
    } catch (Exception e) {
        LOGGER.debug(e);
    }

    return false;
}

From source file:com.thoughtworks.go.config.IdFileService.java

public void store(String data) {
    try {//w w w.  ja  va 2s.  c  om
        file.delete();
        FileUtils.writeStringToFile(file, data, StandardCharsets.UTF_8);
    } catch (IOException ioe) {
        throw bomb(String.format("Couldn't save %s to filesystem", file.getName()), ioe);
    }
}

From source file:com.mvdb.etl.consumer.OrderJsonFileConsumer.java

public void consume(Object object) {
    Order order = (Order) object;/*from   w  w  w  . ja va2s .  c  om*/
    try {
        FileUtils.writeStringToFile(new File(file, Order.class.getCanonicalName() + ".json"),
                order.toString() + System.getProperty("line.separator"), true);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:com.junoyoon.BullsUtil.java

/**
 * Write file//from   ww  w  .ja  v a2 s. c  om
 * 
 * @param path
 *            the path
 * @param content
 *            content
 */
public static void writeToFile(File path, String content) {
    try {
        FileUtils.writeStringToFile(path, content, "UTF-8");
    } catch (IOException e) {
        BullsHtml.printErrorAndExit(e);
    }
}

From source file:com.bluexml.tools.miscellaneous.ConvertFileContentIntoJSString.java

protected static void writeFiles(File input, File output) {
    try {/*from   ww  w .jav a 2 s  .  c  o m*/
        String readFileToString = FileUtils.readFileToString(input);

        readFileToString = readFileToString.replaceAll("\"", "\\\\\"");

        readFileToString = readFileToString.replaceAll("\n", "\\\\n\\\\\\\n");
        if (readFileToString.endsWith("\n")) {
            readFileToString = readFileToString.substring(0, readFileToString.length() - 2);
        }

        readFileToString = nativeToAscii("\"" + readFileToString + "\"");

        System.out.println(readFileToString);

        FileUtils.writeStringToFile(output, readFileToString, "UTF-8");

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:com.handany.base.generator.FreemarkerUtil.java

public static void outputProcessResult(String outputFile, Template template, Map<String, Object> varMap) {
    String resultString;//  w w  w  .  j  av a2 s . co m
    ByteArrayOutputStream baos = null;
    Writer writer = null;

    try {
        baos = new ByteArrayOutputStream();
        writer = new OutputStreamWriter(baos, CHARSET);
        template.process(varMap, writer);
        resultString = new String(baos.toByteArray(), CHARSET);
        FileUtils.writeStringToFile(new File(outputFile), resultString, CHARSET);
    } catch (UnsupportedEncodingException ex) {
        log.error(ex.getMessage(), ex);
    } catch (IOException | TemplateException ex) {
        log.error(ex.getMessage(), ex);
        throw new RuntimeException(ex);
    } finally {
        if (null != baos) {
            try {
                baos.close();
            } catch (IOException ex) {
                log.warn(ex.getMessage(), ex);
            }
        }

        if (null != writer) {
            try {
                writer.close();
            } catch (IOException ex) {
                log.warn(ex.getMessage(), ex);
            }
        }
    }
}

From source file:is.iclt.icenlp.core.utils.FileOperations.java

/**
 * Writes a String to a file. Complains to system out and prints stack trace 
 * if an IOException occurs./*from  w w w  .  ja v  a2 s  . c  om*/
 * @param filename Name of the file to be written.
 * @param data The String data that will be written to the file.
 */
public static void stringToFile(String filename, String data) {
    try {
        //FileUtils.writeStringToFile(new File(filename), data);
        FileUtils.writeStringToFile(new File(filename), data, FileEncoding.ENCODING);
    } catch (IOException ex) {
        System.out.println("Could not write to file '" + filename + "'!");
        ex.printStackTrace();
    }
}