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:net.osten.watermap.batch.FetchPCTJob.java

/**
 * Fetch the PCT files and save in output directory.
 *
 * @see javax.batch.api.Batchlet#process()
 * @return FAILED if the files cannot be downloaded or can't be written to disk; COMPLETED otherwise
 *///from ww  w  .  j av  a  2s  .  com
@Override
public String process() throws Exception {
    googleKey = config.getString("GOOGLE_API_KEY");
    outputDir = new File(config.getString("output_dir"));

    if (!outputDir.isDirectory()) {
        log.log(Level.WARNING, "Output directory [{0}] is not a directory.", outputDir);
        return BatchStatus.FAILED.toString();
    } else if (!outputDir.canWrite()) {
        log.log(Level.WARNING, "Output directory [{0}] is not writable.", outputDir);
        return BatchStatus.FAILED.toString();
    }

    for (String url : URLS) {
        log.log(Level.FINE, "Fetching PCT to {0}", new Object[] { url });
        String response = Request.Get(context.getProperties().getProperty(url) + "?key=" + googleKey).execute()
                .returnContent().asString();
        File outputFile = new File(outputDir.getAbsolutePath() + File.separator + url + ".json");
        FileUtils.writeStringToFile(outputFile, response, Charsets.UTF_8);
    }

    /*
     * curl -o $od/pct-CA-A.json
     * https://sheets.googleapis.com/v4/spreadsheets/1gEyz3bw__aPvNXpqqHcs7KRwmwYrTH2L0DEMW3RbHes/values/Campo%20-%
     * 20Idyllwild!A11:G1000?key=$apik
     * curl -o $od/pct-CA-C.json
     * https://sheets.googleapis.com/v4/spreadsheets/1gEyz3bw__aPvNXpqqHcs7KRwmwYrTH2L0DEMW3RbHes/values/Idyllwild%20-
     * %20Agua%20Dulce!A11:G1000?key=$apik
     * curl -o $od/pct-CA-E.json
     * https://sheets.googleapis.com/v4/spreadsheets/1gEyz3bw__aPvNXpqqHcs7KRwmwYrTH2L0DEMW3RbHes/values/Agua%20Dulce%
     * 20-%20Cottonwood%20Pass!A11:G100?key=$apik
     * curl -o $od/pct-CA-M.json
     * https://sheets.googleapis.com/v4/spreadsheets/1gEyz3bw__aPvNXpqqHcs7KRwmwYrTH2L0DEMW3RbHes/values/Northern%20CA
     * !A9:G1000?key=$apik
     * curl -o $od/pct-OR-B.json
     * https://sheets.googleapis.com/v4/spreadsheets/1gEyz3bw__aPvNXpqqHcs7KRwmwYrTH2L0DEMW3RbHes/values/Oregon!A9:
     * G1000?key=$apik
     * curl -o $od/pct-WA-H.json
     * https://sheets.googleapis.com/v4/spreadsheets/1gEyz3bw__aPvNXpqqHcs7KRwmwYrTH2L0DEMW3RbHes/values/Washington!A9
     * :G1000?key=$apik
     */
    return BatchStatus.COMPLETED.toString();
}

From source file:de.unidue.ltl.flextag.core.reports.CvAvgAccuracyReport.java

public void execute() throws Exception {
    StorageService storageService = getContext().getStorageService();
    for (TaskContextMetadata subcontext : getSubtasks()) {
        if (TcTaskTypeUtil.isCrossValidationTask(storageService, subcontext.getId())) {
            File attributes = storageService.locateKey(subcontext.getId(), "ATTRIBUTES.txt");
            List<String> foldersOfSingleRuns = getFoldersOfSingleRuns(attributes);

            Double sumAcc = 0.0;/*from w  ww. j  av  a2 s. c  o  m*/
            for (String folder : foldersOfSingleRuns) {

                File id2outcomeFile = storageService.locateKey(folder, Constants.ID_OUTCOME_KEY);
                String learningMode = getLearningMode(folder, storageService);
                Id2Outcome o = new Id2Outcome(id2outcomeFile, learningMode);
                EvaluatorBase createEvaluator = EvaluatorFactory.createEvaluator(o, true, false);

                Double accuracy = createEvaluator.calculateEvaluationMeasures()
                        .get(Accuracy.class.getSimpleName());
                sumAcc += accuracy;
            }

            Double average = sumAcc / foldersOfSingleRuns.size();

            System.out.println(
                    "\nAverage accuracy over all folds: " + String.format("%.1f percent\n", average * 100));

            File locateKey = storageService.locateKey(subcontext.getId(), OUTPUT_FILE);
            FileUtils.writeStringToFile(locateKey, String.format("Avg-Accuracy: %.1f", average * 100), "utf-8");
        }
    }

}

From source file:jclparser.NewClass.java

public static File analyze(File f) {

    String procedure = StringUtils.substringBeforeLast(f.getName(), ".");

    if (f.exists()) {
        f.delete();//  ww w.j  av a  2  s. c om
    }

    System.out.println("gepeto args2 = " + procedure);

    String tmp;

    PROC proc = parse(PROC.class, "SYS1.VIDA.PROCLIB", procedure);

    List<STEP> steps = proc.getListSTEP();

    List<String> nodes = new ArrayList<>();
    List<String> fromsTos = new ArrayList<>();

    String nodeTemplate = "{\"key\": \"%s\", \"category\": \"program\", \"color\": \"lightblue\", \"label\": \"%s\", \"num\": \"%s\", \"card\": \"%s\"},";
    String dsnTemplate = "{\"key\": \"%s\", \"color\": \"magenta\", \"label\": \"%s\"},";
    String fromToTemplate = "{\"from\": \"%s\", \"to\": \"%s\"},";

    Map<String, Integer> programs = new HashMap<>();
    Set<String> files = new HashSet<>();

    for (STEP step : steps) {
        String pgm = step.getProgram().getName();
        String xxx = "";

        CARD card = step.getProgram().getCard();
        if (card != null) {
            if (card instanceof CARD_IKJEFT01) {
                pgm = ((CARD_IKJEFT01) card).getProgram();
            }

            xxx = step.getProgram().getCard().toString();

            System.out.println("VVV " + step.getProgram().getCard().toString());
            //                if (card instanceof CARD_SORT) {
            //                    pgm = ((CARD_SORT) card).getProgram();
            //                }
        }

        Integer num = programs.get(pgm);
        num = num == null ? 1 : num + 1;
        programs.put(pgm, num);

        if (num > 1) {
            pgm = pgm + " (" + num + ")";
        }

        String t = String.format(nodeTemplate, pgm, "XXX", "XXX", xxx);

        nodes.add(t);

        List<DSN> dsns = step.getListDSN();
        for (DSN dsn : dsns) {
            if (dsn.getName().trim().isEmpty()) {
                continue;
            }

            if (files.add(dsn.getName())) {

                String u = String.format(dsnTemplate, dsn.getName(), dsn.getLabel());
                files.add(nodeTemplate);
                nodes.add(u);
            }

            String from = dsn.getMode().equals("INPUT") ? dsn.getName() : pgm;
            String to = !dsn.getMode().equals("INPUT") ? dsn.getName() : pgm;
            tmp = String.format(fromToTemplate, from, to);

            if (!pgm.equalsIgnoreCase("COPIADOR")) {
                fromsTos.add(tmp);
            }

            System.out.println("Label: " + StringUtils.rightPad(dsn.getLabel(), 8) + " Dsn: " + dsn.getName()
                    + " Type: " + dsn.getType() + " Mode: " + dsn.getMode());
        }
    }

    String lastNode = nodes.remove(nodes.size() - 1);
    lastNode = StringUtils.removeEnd(lastNode, ",");
    nodes.add(lastNode);
    String lastFromsTos = fromsTos.remove(fromsTos.size() - 1);
    lastFromsTos = StringUtils.removeEnd(lastFromsTos, ",");
    fromsTos.add(lastFromsTos);

    try {
        FileUtils.writeStringToFile(f, "{\"class\": \"go.GraphLinksModel\",\"nodeDataArray\": [", true);
        FileUtils.writeLines(f, nodes, true);
        FileUtils.writeStringToFile(f, "],", true);
        FileUtils.writeStringToFile(f, "\"linkDataArray\": [", true);
        FileUtils.writeLines(f, fromsTos, true);
        FileUtils.writeStringToFile(f, "]}", true);
    } catch (IOException ex) {
        Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
    }

    System.out.println("directoru " + f.getAbsolutePath());

    //        for (String node : nodes) {
    //            System.out.println(node + ",");
    //        }
    //
    //        for (String fromTo : fromsTos) {
    //            System.out.println(fromTo + ",");
    //        }
    return f;
}