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.jslsolucoes.tagria.doc.generator.DocGenerator.java

public static void main(String[] args) throws IOException {

    String workspace = args[0];/*  ww  w.j  a v a  2  s. c  o  m*/
    Map<String, List<Tag>> groupments = new HashMap<>();

    String html = FileUtils.readFileToString(
            new File(workspace + "/tagria-lib/src/main/resources/META-INF/html.tld"), CHARSET);
    String ajax = FileUtils.readFileToString(
            new File(workspace + "/tagria-lib/src/main/resources/META-INF/ajax.tld"), CHARSET);
    XStream xStream = new XStream();
    xStream.processAnnotations(Taglib.class);
    Taglib taglibForHtml = (Taglib) xStream.fromXML(html);
    Taglib taglibForAjax = (Taglib) xStream.fromXML(ajax);
    List<Tag> tags = new ArrayList<Tag>();
    tags.addAll(taglibForHtml.getTags());
    tags.addAll(taglibForAjax.getTags());

    for (Tag tag : tags) {

        List<Tag> groups = groupments.get(tag.getGroup());
        if (groups == null) {
            groupments.put(tag.getGroup(), new ArrayList<>());
        }
        groupments.get(tag.getGroup()).add(tag);

        StringBuilder template = new StringBuilder(
                "<%@include file=\"../app/taglibs.jsp\"%>                              "
                        + "<html:view title=\"{title}\">                                 "
                        + "                  <html:panel>                                                      "
                        + "                     <html:panelHead label=\"" + tag.getName()
                        + "\"></html:panelHead>               "
                        + "                     <html:panelBody>                                                "
                        + "                        <html:tabPanel>                                                "
                        + "                           <html:tab label=\"{about}\" active=\"true\">                     "
                        + "                              <html:alert state=\"warning\">                              "
                        + "                                      " + tag.getDescription()
                        + "                           "
                        + "                              </html:alert>                                          "
                        + "                           </html:tab>                                                "
                        + "                           <html:tab label=\"{attributes}\">                              ");

        if (CollectionUtils.isEmpty(tag.getAttributes())) {
            template.append("<html:alert state=\"info\" label=\"{tag.empty.attributes}\"></html:alert>");
        } else {

            template.append("<html:table><html:tableLine>"
                    + "<html:tableColumn header=\"true\"><fmt:message key=\"tag.attribute\"/></html:tableColumn>"
                    + "<html:tableColumn header=\"true\"><fmt:message key=\"tag.required\"/></html:tableColumn>"
                    + "<html:tableColumn header=\"true\"><fmt:message key=\"tag.type\"/></html:tableColumn>"
                    + "<html:tableColumn header=\"true\"><fmt:message key=\"tag.description\"/></html:tableColumn>"
                    +

                    "</html:tableLine>");

            for (Attribute attribute : tag.getAttributes()) {

                template.append("<html:tableLine>" + "<html:tableColumn>" + attribute.getName()
                        + "</html:tableColumn>" + "<html:tableColumn>"
                        + (attribute.getRequired() == null ? false : true) + "</html:tableColumn>"
                        + "<html:tableColumn>" + attribute.getType() + "</html:tableColumn>"
                        + "<html:tableColumn>" + attribute.getDescription() + "</html:tableColumn>" +

                        "</html:tableLine>");
            }

            template.append("</html:table>");
        }

        template.append("                                                                        "
                + "                           </html:tab>                                                "
                + "                           <html:tab label=\"{demo}\">                                    "
                + "                              " + tag.getExample()
                + "                                          "
                + "                           </html:tab>                                                "
                + "                           <html:tab label=\"{source}\">                                 "
                + "                              <html:code>                                             "
                + "                                 &lt;html:view&gt;" + tag.getExampleEscaped()
                + "&lt;/html:view&gt;                                 "
                + "                              </html:code>                                          "
                + "                           </html:tab>                                                "
                + "                        </html:tabPanel>                                             "
                + "                     </html:panelBody>                                                "
                + "                  </html:panel>                                                      "
                + "               </html:view>                                                         ");
        FileUtils.writeStringToFile(new File(
                workspace + "/tagria-doc/src/main/webapp/WEB-INF/jsp/component/" + tag.getName() + ".jsp"),
                template.toString(), CHARSET);
    }

    for (List<Tag> values : groupments.values()) {
        Collections.sort(values, new Comparator<Tag>() {
            @Override
            public int compare(Tag o1, Tag o2) {
                return o1.getName().compareTo(o2.getName());
            }
        });
    }

    StringBuilder menu = new StringBuilder("<html:div cssClass=\"menu\"><html:listGroup>");
    for (String key : new TreeSet<String>(groupments.keySet())) {
        menu.append("<html:listGroupItem><html:collapsable label=\"" + key + "\"><html:listGroup>");
        for (Tag tag : groupments.get(key)) {
            menu.append("<html:listGroupItem><html:link label=\"" + StringUtils.capitalize(tag.getName())
                    + "\" target=\"conteudo\" url=\"/component/" + tag.getName()
                    + "\"></html:link></html:listGroupItem>");
        }
        menu.append("</html:listGroup></html:collapsable></html:listGroupItem>");
    }
    menu.append("</html:listGroup></html:div>");

    File home = new File(workspace + "/tagria-doc/src/main/webapp/WEB-INF/jsp/app/index.jsp");
    FileUtils.writeStringToFile(home, FileUtils.readFileToString(home, CHARSET)
            .replaceAll("<html:div cssClass=\"menu\">[\\s\\S]*?</html:div>", menu.toString()), CHARSET);

}

From source file:de.fraunhofer.iosb.ilt.stc.Copier.java

/**
 * @param args the command line arguments
 * @throws de.fraunhofer.iosb.ilt.sta.ServiceFailureException
 * @throws java.net.URISyntaxException/* w  w w.j  a va  2  s .  c o  m*/
 * @throws java.net.MalformedURLException
 */
public static void main(String[] args)
        throws ServiceFailureException, URISyntaxException, MalformedURLException, IOException {
    String configFileName = "configuration.json";
    if (args.length > 0) {
        configFileName = args[0];
    }
    File configFile = new File(configFileName);
    if (configFile.isFile() && configFile.exists()) {
        Copier copier = new Copier(configFile);
        copier.start();
    } else {
        LOGGER.info("No configuration found, generating sample.");
        JsonElement sampleConfig = new Copier().getConfigEditor(null, null).getConfig();
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        String configString = gson.toJson(sampleConfig);
        FileUtils.writeStringToFile(configFile, configString, "UTF-8");

        LOGGER.info("Sample configuration written to {}.", configFile);
    }
}

From source file:com.z2data.files.WriteOperations.java

/**
 * Write status Data for URL To file//from   w w  w. j  a v a 2  s . co  m
 * 
 * @param outputPath
 * @param urlData
 */
@SuppressWarnings("deprecation")
public static void writeURLData(String outputPath, URLData urlData) {

    try {
        File file = new File(outputPath);

        if (!file.exists()) {
            file.createNewFile();
        }

        FileUtils.writeStringToFile(file, urlData.getParentURL() + "\t" + urlData.getAnchorTitle() + "\t"
                + urlData.getAnchor() + "\t" + urlData.getDownloadStatus() + "\t" + urlData.getDownloadPath(),
                true);
        FileUtils.writeStringToFile(file, System.lineSeparator(), true);

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

From source file:com.github.stagirs.common.model.DocumentSerializer.java

public static void serialize(File file, Document document) throws IOException {
    StringBuilder sb = new StringBuilder();
    serialize(sb, document);//  www  . j a  v  a 2  s  . c  o  m
    FileUtils.writeStringToFile(new File(file, document.getId()), sb.toString(), "utf-8");
}

From source file:lab1_2_exercice5.Utf8File.java

public static void saveStringIntoFile(String filePath, String content) throws IOException {
    File f = new File(filePath);
    FileUtils.writeStringToFile(f, content, "UTF-8");
}

From source file:lineage2.gameserver.utils.Files.java

/**
 * Method writeFile.//from   w  w w  . j  av a  2s  .c o  m
 * @param path String
 * @param string String
 */
public static void writeFile(String path, String string) {
    try {
        FileUtils.writeStringToFile(new File(path), string, "UTF-8");
    } catch (IOException e) {
        _log.error("Error while saving file : " + path, e);
    }
}

From source file:ee.ioc.phon.android.inimesed.MyFileUtils.java

public static void saveFile(File f, String content) throws IOException {
    File dir = f.getParentFile();
    if (!dir.exists() && !dir.mkdirs()) {
        throw new IOException("Cannot create directory: " + dir);
    }//from  w w w . j  ava  2s  .  c  o  m
    FileUtils.writeStringToFile(f, content, "UTF8");
}

From source file:Fondation.Utf8File.java

public static void saveStringIntoFile(String filePath, String content) {
    File f = new File(filePath);
    try {//from  w w w  .  jav a  2  s. c  om
        FileUtils.writeStringToFile(f, content, "UTF-8");
    } catch (Exception e) {
        System.out.println("Erreur lors de l'criture du fichier.");
    }
}

From source file:com.mirth.connect.server.userutil.FileUtil.java

/**
 * Writes a string to a specified file, creating the file if it does not exist.
 * /*from www .  ja  v  a2 s  . c  om*/
 * @see org.apache.commons.io.FileUtils#writeStringToFile(File, String)
 * @param fileName
 *            The pathname string of the file to write to.
 * @param append
 *            If true, the data will be added to the end of the file rather than overwriting the
 *            file.
 * @param data
 *            The content to write to the file.
 * @throws IOException
 */
public static void write(String fileName, boolean append, String data) throws IOException {
    FileUtils.writeStringToFile(new File(fileName), data, append);
}

From source file:com.intuit.tank.dao.util.ProjectDaoUtil.java

/**
 * //from w  w w  .j  a va 2  s  .c om
 * @param jobId
 * @param scriptString
 */
public static void storeScriptFile(String jobId, String scriptString) {
    try {
        File f = createScriptFile(jobId);
        FileUtils.writeStringToFile(f, scriptString, "UTF-8");
    } catch (IOException e) {
        LOG.error("Erorr writing file: " + e, e);
        throw new RuntimeException(e);
    }
}