Example usage for com.google.common.io Files write

List of usage examples for com.google.common.io Files write

Introduction

In this page you can find the example usage for com.google.common.io Files write.

Prototype

public static void write(CharSequence from, File to, Charset charset) throws IOException 

Source Link

Usage

From source file:org.apache.s4.tools.CreateApp.java

public static void main(String[] args) {

    final CreateAppArgs appArgs = new CreateAppArgs();
    Tools.parseArgs(appArgs, args);/* www.j  a va2 s .  com*/

    if (new File(appArgs.getAppDir() + "/" + appArgs.appName.get(0)).exists()) {
        System.err.println("There is already a directory called " + appArgs.appName.get(0) + " in the "
                + appArgs.getAppDir()
                + " directory. Please specify another name for your project or specify another parent directory");
        System.exit(1);
    }
    // create project structure
    try {
        createDir(appArgs, "/src/main/java");
        createDir(appArgs, "/src/main/resources");
        createDir(appArgs, "/src/main/java/hello");

        // copy gradlew script (redirecting to s4 gradlew)
        File gradlewTempFile = File.createTempFile("gradlew", "tmp");
        gradlewTempFile.deleteOnExit();
        Files.copy(Resources.newInputStreamSupplier(Resources.getResource("templates/gradlew")),
                gradlewTempFile);
        String gradlewScriptContent = Files.readLines(gradlewTempFile, Charsets.UTF_8,
                new PathsReplacer(appArgs));
        Files.write(gradlewScriptContent, gradlewTempFile, Charsets.UTF_8);
        Files.copy(gradlewTempFile, new File(appArgs.getAppDir() + "/gradlew"));
        new File(appArgs.getAppDir() + "/gradlew").setExecutable(true);

        // copy build file contents
        String buildFileContents = Resources.toString(Resources.getResource("templates/build.gradle"),
                Charsets.UTF_8);
        buildFileContents = buildFileContents.replace("<s4_install_dir>",
                "'" + new File(appArgs.s4ScriptPath).getParent() + "'");
        Files.write(buildFileContents, new File(appArgs.getAppDir() + "/build.gradle"), Charsets.UTF_8);

        // copy lib
        FileUtils.copyDirectory(new File(new File(appArgs.s4ScriptPath).getParentFile(), "lib"),
                new File(appArgs.getAppDir() + "/lib"));

        // update app settings
        String settingsFileContents = Resources.toString(Resources.getResource("templates/settings.gradle"),
                Charsets.UTF_8);
        settingsFileContents = settingsFileContents.replaceFirst("rootProject.name=<project-name>",
                "rootProject.name=\"" + appArgs.appName.get(0) + "\"");
        Files.write(settingsFileContents, new File(appArgs.getAppDir() + "/settings.gradle"), Charsets.UTF_8);
        // copy hello app files
        Files.copy(Resources.newInputStreamSupplier(Resources.getResource("templates/HelloPE.java.txt")),
                new File(appArgs.getAppDir() + "/src/main/java/hello/HelloPE.java"));
        Files.copy(Resources.newInputStreamSupplier(Resources.getResource("templates/HelloApp.java.txt")),
                new File(appArgs.getAppDir() + "/src/main/java/hello/HelloApp.java"));
        // copy hello app adapter
        Files.copy(
                Resources.newInputStreamSupplier(Resources.getResource("templates/HelloInputAdapter.java.txt")),
                new File(appArgs.getAppDir() + "/src/main/java/hello/HelloInputAdapter.java"));

        File s4TmpFile = File.createTempFile("s4Script", "template");
        s4TmpFile.deleteOnExit();
        Files.copy(Resources.newInputStreamSupplier(Resources.getResource("templates/s4")), s4TmpFile);

        // create s4
        String preparedS4Script = Files.readLines(s4TmpFile, Charsets.UTF_8, new PathsReplacer(appArgs));

        File s4Script = new File(appArgs.getAppDir() + "/s4");
        Files.write(preparedS4Script, s4Script, Charsets.UTF_8);
        s4Script.setExecutable(true);

        File readmeTmpFile = File.createTempFile("newApp", "README");
        readmeTmpFile.deleteOnExit();
        Files.copy(Resources.newInputStreamSupplier(Resources.getResource("templates/newApp.README")),
                readmeTmpFile);
        // display contents from readme
        Files.readLines(readmeTmpFile, Charsets.UTF_8, new LineProcessor<Boolean>() {

            @Override
            public boolean processLine(String line) throws IOException {
                if (!line.startsWith("#")) {
                    System.out.println(line.replace("<appDir>", appArgs.getAppDir()));
                }
                return true;
            }

            @Override
            public Boolean getResult() {
                return true;
            }

        });
    } catch (Exception e) {
        logger.error("Could not create project due to [{}]. Please check your configuration.", e.getMessage());
    }
}

From source file:net.sourceforge.docfetcher.enums.MsgMigrator.java

public static void main(String[] args) throws Exception {
    File oldTransDir = new File("dev/old-translations-from-1.0.3");
    final String enPropName = "Resource.properties";

    Properties oldEnProp = CharsetDetectorHelper.load(new File(oldTransDir, enPropName));
    List<File> oldPropFiles = Arrays.asList(Util.listFiles(oldTransDir, new FilenameFilter() {
        public boolean accept(File dir, String name) {
            return !name.equals(enPropName);
        }/*w w  w . ja va 2s  .co  m*/
    }));

    final Map<Properties, File> propToFileMap = Maps.newHashMap();

    List<Properties> oldProps = Lists.transform(oldPropFiles, new Function<File, Properties>() {
        public Properties apply(File file) {
            try {
                Properties prop = CharsetDetectorHelper.load(file);
                propToFileMap.put(prop, file);
                return prop;
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        };
    });

    StringBuilder sb0 = new StringBuilder();
    for (Msg msg : Msg.values()) {
        String key = ConfLoader.convert(msg.name(), true);
        String value = ConfLoader.convert(msg.get(), false);
        String comments = msg.getComment();

        if (!comments.isEmpty())
            sb0.append("# " + comments + Util.LS);
        sb0.append(key + "=" + value);
        sb0.append(Util.LS);
    }
    File enOutFile = new File(Util.TEMP_DIR, enPropName);
    Files.write(sb0.toString(), enOutFile, Charsets.UTF_8);
    Util.println("File written: " + enOutFile.getPath());

    for (Properties oldProp : oldProps) {
        StringBuilder sb = new StringBuilder();
        for (Msg msg : Msg.values()) {
            String key = msg.name();

            String enOldValue = oldEnProp.getProperty(key);
            if (enOldValue == null) // New key?
                continue;
            else if (!enOldValue.equals(msg.get())) // Changed value?
                continue;

            String value = oldProp.getProperty(key);
            if (value == null)
                value = enOldValue;
            else if (value.equals("$TODO$"))
                continue;

            key = ConfLoader.convert(key, true);
            value = ConfLoader.convert(value, false);
            sb.append(key + "=" + value);
            sb.append(Util.LS);
        }

        String filename = propToFileMap.get(oldProp).getName();
        File outFile = new File(Util.TEMP_DIR, filename);
        Files.write(sb.toString(), outFile, Charsets.UTF_8);
        Util.println("File written: " + outFile.getPath());
    }
}

From source file:org.smartdeveloperhub.harvesters.it.testing.generator.ProjectDataGenerator.java

public static void main(final String... args) {
    final Path path = getOutputPath(args);
    try {/*from  w  w  w  .  ja  v  a2s  .c  o  m*/
        System.out.printf("Project Test Data Generator %s%n", serviceVersion());
        final LocalData localData = generateData();
        Files.write(Entities.marshallEntity(localData), path.toFile(), StandardCharsets.UTF_8);
    } catch (final IOException e) {
        LOGGER.error("Project generation failed", e);
        System.exit(-1);
    }
}

From source file:org.joda.railmodel.Crossrail2BalhamSWLondonModel.java

public static void main(String[] args) throws Exception {
    Crossrail2BalhamSWLondonModel model = new Crossrail2BalhamSWLondonModel();
    ImmutableList<Station> starts = ImmutableList.of(CSS, LHD, EPS, SNL, WCP, MOT, SHP, FLW, KNG, HMC, SUR, NEM,
            RAY, WIM, EAD, UMD, USW, UTB, BAL, SRH, STE);
    ImmutableList<Station> ends = ImmutableList.of(VIC, TCR, EUS, AGL, WAT, UGP, UOX, BDS, CHX, ULS, UGS, UWS,
            UBS, UWM, UTM, ZFD, UBH, LBG, UBK, MOG, UOS, UHL, UCL, USP, CWF);

    List<String> output = new ArrayList<>();
    output.add("Modelling for SW London with Crossrail 2 via Balham" + NEWLINE);
    output.add("===================================================" + NEWLINE);
    output.add("This uses CR2 via Balham, with best efforts guesses of interchange times." + NEWLINE);
    output.add(NEWLINE);/*from ww  w . j  a v  a2 s  .c  om*/
    appendDocs(output);
    appendTotals(output, starts, ends, model);
    appendSeparator(output);
    for (Iterator<Station> it = starts.iterator(); it.hasNext();) {
        Station start = it.next();
        for (Station end : ends) {
            String explain = model.explain(start, end);
            output.add(explain);
            output.add(NEWLINE);
        }
        if (it.hasNext()) {
            appendSeparator(output);
        }
    }
    appendStations(output);
    output.add(NEWLINE);
    output.add("Feel free to send a pull request for errors and enhancments!" + NEWLINE);

    File file = new File("CR2-SWLondon.md");
    String result = Joiner.on("").join(output);
    Files.write(result, file, StandardCharsets.UTF_8);
    System.out.println(result);
}

From source file:org.joda.railmodel.Crossrail2TootingSWLondonModel.java

public static void main(String[] args) throws Exception {
    Crossrail2TootingSWLondonModel model = new Crossrail2TootingSWLondonModel();
    ImmutableList<Station> starts = ImmutableList.of(CSS, LHD, EPS, SNL, WCP, MOT, SHP, FLW, KNG, HMC, SUR, NEM,
            RAY, WIM, EAD, UMD, USW, UTB, BAL, SRH, STE);
    ImmutableList<Station> ends = ImmutableList.of(VIC, TCR, EUS, AGL, WAT, UGP, UOX, BDS, CHX, ULS, UGS, UWS,
            UBS, UWM, UTM, ZFD, UBH, LBG, UBK, MOG, UOS, UHL, UCL, USP, CWF);

    List<String> output = new ArrayList<>();
    output.add("Modelling for SW London with Crossrail 2 via Tooting Broadway" + NEWLINE);
    output.add("=============================================================" + NEWLINE);
    output.add("This uses CR2 via Tooting Broadway, with best efforts guesses of interchange times." + NEWLINE);
    output.add(NEWLINE);//  ww  w .  jav a  2  s  .  c  om
    appendDocs(output);
    appendTotals(output, starts, ends, model);
    appendSeparator(output);
    for (Iterator<Station> it = starts.iterator(); it.hasNext();) {
        Station start = it.next();
        for (Station end : ends) {
            String explain = model.explain(start, end);
            output.add(explain);
            output.add(NEWLINE);
        }
        if (it.hasNext()) {
            appendSeparator(output);
        }
    }
    appendStations(output);
    output.add(NEWLINE);
    output.add("Feel free to send a pull request for errors and enhancments!" + NEWLINE);

    File file = new File("CR2-Tooting-SWLondon.md");
    String result = Joiner.on("").join(output);
    Files.write(result, file, StandardCharsets.UTF_8);
    System.out.println(result);
}

From source file:org.joda.railmodel.Crossrail2EarlsfieldSWLondonModel.java

public static void main(String[] args) throws Exception {
    Crossrail2EarlsfieldSWLondonModel model = new Crossrail2EarlsfieldSWLondonModel();
    ImmutableList<Station> starts = ImmutableList.of(CSS, LHD, EPS, SNL, WCP, MOT, SHP, FLW, KNG, HMC, SUR, NEM,
            RAY, WIM, EAD, UMD, USW, UTB, BAL, SRH, STE);
    ImmutableList<Station> ends = ImmutableList.of(VIC, TCR, EUS, AGL, WAT, UGP, UOX, BDS, CHX, ULS, UGS, UWS,
            UBS, UWM, UTM, ZFD, UBH, LBG, UBK, MOG, UOS, UHL, UCL, USP, CWF);

    List<String> output = new ArrayList<>();
    output.add("Modelling for SW London with Crossrail 2 Swirl" + NEWLINE);
    output.add("==============================================" + NEWLINE);
    output.add(//from w  ww  .ja v a  2  s  . com
            "This uses CR2 via Earlsfield based on the [Swirl plan](http://ukrail.blogspot.co.uk/2015/11/crossrail-2-swirl.html),"
                    + " with best efforts guesses of interchange times." + NEWLINE);
    output.add(NEWLINE);
    appendDocs(output);
    appendTotals(output, starts, ends, model);
    appendSeparator(output);
    for (Iterator<Station> it = starts.iterator(); it.hasNext();) {
        Station start = it.next();
        for (Station end : ends) {
            String explain = model.explain(start, end);
            output.add(explain);
            output.add(NEWLINE);
        }
        if (it.hasNext()) {
            appendSeparator(output);
        }
    }
    appendStations(output);
    output.add(NEWLINE);
    output.add("Feel free to send a pull request for errors and enhancments!" + NEWLINE);

    File file = new File("CR2-Swirl-SWLondon.md");
    String result = Joiner.on("").join(output);
    Files.write(result, file, StandardCharsets.UTF_8);
    System.out.println(result);
}

From source file:net.sourceforge.docfetcher.website.Website.java

public static void main(String[] args) throws Exception {
    for (File srcDir : Util.listFiles(websiteDir)) {
        if (!srcDir.isDirectory())
            continue;
        if (srcDir.getName().equals("all"))
            continue;

        File dstDir = new File("dist/website/" + srcDir.getName());
        dstDir.mkdirs();/*from  w  w w . j  a  v a 2s .c  o  m*/
        Util.deleteContents(dstDir);

        PegDownProcessor processor = new PegDownProcessor(Extensions.TABLES);
        File propsFile = new File(srcDir, "/page.properties");
        PageProperties props = new PageProperties(propsFile);
        convertDir(processor, props, srcDir, dstDir);
    }

    // Deploy files in the website/all directory
    for (File file : Util.listFiles(new File(websiteDir + "/all"))) {
        File dstFile = new File("dist/website/all", file.getName());
        Files.createParentDirs(dstFile);
        Files.copy(file, dstFile);
    }

    // Deploy PAD file
    File padFileSrc = new File(websiteDir, "docfetcher-pad-template.xml");
    File padFileDst = new File("dist/website", "docfetcher-pad.xml");
    String padContents = CharsetDetectorHelper.toString(padFileSrc);
    padContents = UtilGlobal.replace(padFileSrc.getPath(), padContents, "${version}", version);
    Files.write(padContents, padFileDst, Charsets.UTF_8);
    Util.println("File written: " + padFileDst.getPath());

    // Deploy English Resource.properties; this is currently used for
    // GUI translation via transifex.com
    Properties prop = new Properties();
    for (Msg msg : Msg.values())
        prop.put(msg.name(), msg.get());
    File propFile = new File("dist/website", "Resource.properties");
    FileWriter w = new FileWriter(propFile);
    prop.store(w, "");
    Closeables.closeQuietly(w);
    Util.println("File written: " + propFile.getPath());

    // Deploy index.php file
    File indexPhpFile = new File("dist/website/index.php");
    Files.copy(new File(websiteDir, "index.php"), indexPhpFile);
    Util.println("File written: " + indexPhpFile.getPath());

    // Deploy help file for GUI translators
    File propHelpSrc = new File(websiteDir, "prop-help-template.html");
    File propHelpDst = new File("dist/website", "properties-help.html");
    StringBuilder sb = new StringBuilder();
    for (Msg msg : Msg.values()) {
        sb.append("<tr align=\"left\">");
        sb.append("<td>");
        sb.append(escapeHtml(propHelpSrc.getPath(), msg.get()));
        sb.append("</td>");
        sb.append(Util.LS);
        sb.append("<td>");
        sb.append(msg.getComment());
        sb.append("</td>");
        sb.append("</tr>");
        sb.append(Util.LS);
    }
    String propHelpContents = CharsetDetectorHelper.toString(propHelpSrc);
    propHelpContents = UtilGlobal.replace(propHelpSrc.getPath(), propHelpContents, "${contents}",
            sb.toString());
    Files.write(propHelpContents, propHelpDst, Charsets.UTF_8);
    Util.println("File written: " + propHelpDst.getPath());
}

From source file:org.joda.railmodel.Crossrail2SwirlMaxSWLondonModel.java

public static void main(String[] args) throws Exception {
    Crossrail2SwirlMaxSWLondonModel model = new Crossrail2SwirlMaxSWLondonModel();
    ImmutableList<Station> starts = ImmutableList.of(CSS, LHD, EPS, SNL, WCP, MOT, SHP, FLW, KNG, HMC, SUR, NEM,
            RAY, WIM, EAD, UMD, USW, UTB, BAL, SRH, STE);
    ImmutableList<Station> ends = ImmutableList.of(VIC, TCR, EUS, AGL, WAT, UGP, UOX, BDS, CHX, ULS, UGS, UWS,
            UBS, UWM, UTM, ZFD, UBH, LBG, UBK, MOG, UOS, UHL, UCL, USP, CWF);

    List<String> output = new ArrayList<>();
    output.add("Modelling for SW London with Crossrail 2 Swirl-Max" + NEWLINE);
    output.add("==================================================" + NEWLINE);
    output.add(/*w w w.  j  a  va2 s  .  c  o  m*/
            "This uses CR2 via Earlsfield based on the [Swirl plan](http://ukrail.blogspot.co.uk/2015/11/crossrail-2-swirl.html), "
                    + NEWLINE);
    output.add("with a separate branch to Balham, Streatham and on to Wimbledon via Haydons Road." + NEWLINE);
    output.add("It uses best efforts guesses for interchange times." + NEWLINE);
    output.add(NEWLINE);
    appendDocs(output);
    appendTotals(output, starts, ends, model);
    appendSeparator(output);
    for (Iterator<Station> it = starts.iterator(); it.hasNext();) {
        Station start = it.next();
        for (Station end : ends) {
            String explain = model.explain(start, end);
            output.add(explain);
            output.add(NEWLINE);
        }
        if (it.hasNext()) {
            appendSeparator(output);
        }
    }
    appendStations(output);
    output.add(NEWLINE);
    output.add("Feel free to send a pull request for errors and enhancments!" + NEWLINE);

    File file = new File("CR2-SwirlMax-SWLondon.md");
    String result = Joiner.on("").join(output);
    Files.write(result, file, StandardCharsets.UTF_8);
    System.out.println(result);
}

From source file:org.joda.railmodel.Crossrail2StreathamSWLondonModel.java

public static void main(String[] args) throws Exception {
    Crossrail2StreathamSWLondonModel model = new Crossrail2StreathamSWLondonModel();
    ImmutableList<Station> starts = ImmutableList.of(CSS, LHD, EPS, SNL, WCP, MOT, SHP, FLW, KNG, HMC, SUR, NEM,
            RAY, WIM, EAD, UMD, USW, UTB, BAL, SRH, STE);
    ImmutableList<Station> ends = ImmutableList.of(VIC, TCR, EUS, AGL, WAT, UGP, UOX, BDS, CHX, ULS, UGS, UWS,
            UBS, UWM, UTM, ZFD, UBH, LBG, UBK, MOG, UOS, UHL, UCL, USP, CWF);

    List<String> output = new ArrayList<>();
    output.add("Modelling for SW London with Crossrail 2 via Streatham" + NEWLINE);
    output.add("======================================================" + NEWLINE);
    output.add(/*from  ww  w .j  av a  2  s  . com*/
            "This uses CR2 via Tooting (mainline) and Streatham, with best efforts guesses of interchange times."
                    + NEWLINE);
    output.add("This route is promoted by various groups in Streatham." + NEWLINE);
    output.add(
            "It adds at least 6 minutes to all journeys on Crossrail 2 between Wimbledon and Clapham Junction."
                    + NEWLINE);
    output.add("As such, no matter what benefits it gives Streatham, it simply will not happen." + NEWLINE);
    output.add(NEWLINE);
    appendDocs(output);
    appendTotals(output, starts, ends, model);
    appendSeparator(output);
    for (Iterator<Station> it = starts.iterator(); it.hasNext();) {
        Station start = it.next();
        for (Station end : ends) {
            String explain = model.explain(start, end);
            output.add(explain);
            output.add(NEWLINE);
        }
        if (it.hasNext()) {
            appendSeparator(output);
        }
    }
    appendStations(output);
    output.add(NEWLINE);
    output.add("Feel free to send a pull request for errors and enhancments!" + NEWLINE);

    File file = new File("CR2-Streatham-SWLondon.md");
    String result = Joiner.on("").join(output);
    Files.write(result, file, StandardCharsets.UTF_8);
    System.out.println(result);
}

From source file:org.dllearner.algorithms.qtl.experiments.Queries.java

public static void main(String[] args) throws Exception {
    File queries = new File(args[0]);
    File targetFile = new File(args[1]);

    List<String> lines = Files.readLines(queries, Charsets.UTF_8);

    String css = "<style>table.reference {\n" + "   width:100%;\n" + "   max-width:100%;\n"
            + "   border-left:1px solid #dddddd;\n" + "   border-right:1px solid #dddddd;   \n"
            + "   border-bottom:1px solid #dddddd;      \n" + "}\n"
            + "table.reference>thead>tr>th, table.reference>tbody>tr>th, table.reference>tfoot>tr>th, table.reference>thead>tr>td, table.reference>tbody>tr>td, table.reference>tfoot>tr>td {\n"
            + "    padding: 8px;\n" + "    line-height: 1.42857143;\n" + "    vertical-align: top;\n"
            + "    border-top: 1px solid #ddd;\n" + "}\n"
            + "table.reference tr:nth-child(odd)   {background-color:#ffffff;}\n"
            + "table.reference tr:nth-child(even)   {background-color:#f1f1f1;}</style>\n\n";

    String html = "<table class=\"reference\">\n";

    html += "<thead><tr><th>ID</th><th>Query</th></tr></thead>\n";

    html += "<tbody>\n";
    int row = 1;//  w ww  .j a  v  a2s  .  co m
    for (String line : lines) {
        html += "<tr>\n";

        html += "<td>" + row++ + "</td>\n";

        Query query = QueryFactory.create(line);
        html += "<td><pre>" + HtmlEscapers.htmlEscaper().escape(query.toString()) + "</pre></td>\n";
        html += "</tr>\n";
    }

    html += "</tbody>\n";
    html += "</table>";

    Files.write(css + html, targetFile, Charsets.UTF_8);
}