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

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

Introduction

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

Prototype

public static void writeLines(File file, Collection lines, String lineEnding) throws IOException 

Source Link

Document

Writes the toString() value of each item in a collection to the specified File line by line.

Usage

From source file:synapticloop.copyrightr.Parser.java

private void parseFile(File file) throws CopyrightrException {
    statistics.incrementNumFiles();//  w  w  w .ja v  a2 s .c  o  m

    boolean fileMatch = false;
    logger.info(String.format("Searching for copyright notice in file '%s'", file.getPath()));
    try {
        List<String> readLines = FileUtils.readLines(file);
        int i = 0;
        for (String line : readLines) {
            for (Pattern pattern : compiledPatterns) {
                // if we have found a line in the file and we already only want to 
                // replace the first one, then break here
                if (onlyReplaceFirst && fileMatch) {
                    break;
                }
                Matcher matcher = pattern.matcher(line);

                if (matcher.matches()) {
                    logger.debug(String.format("Found a match with pattern '%s' for line '%s'",
                            pattern.pattern(), line));

                    fileMatch = true;
                    int groupCount = matcher.groupCount();

                    String group = matcher.group(groupCount);
                    int regionStart = matcher.start(groupCount);
                    int regionEnd = matcher.end(groupCount);

                    boolean overwrite = false;
                    switch (groupCount) {
                    case 2:
                        // we have a date with a '-' in it, we will replace the last group
                        overwrite = true;
                        break;
                    case 1:
                        // we have a date with no '-' in it - we are going to add one in...
                    default:
                        break;
                    }

                    readLines.set(i, getReplacementLine(line, group, regionStart, regionEnd, overwrite));
                    statistics.incrementNumFound();
                    break;
                }
            }
            i++;
        }

        if (fileMatch) {
            if (!dryRun) {
                FileUtils.writeLines(file, readLines, false);
            }
        } else {
            statistics.incrementNumMissing();
            logger.warn(String.format("Could not find copyright in file '%s'.", file.getName()));
        }

    } catch (IOException ex) {
        throw new CopyrightrException(
                String.format("Could not update copyright for file '%s', message was '%s'", file.getPath(),
                        ex.getMessage()),
                ex);
    }
}

From source file:utils.CloudifyUtils.java

/**
 * Creates a cloud folder containing all necessary credentials 
 * for bootstrapping to the HP cloud.//w ww. j a  va  2s.  c om
 * 
 * @return
 *          A path to the newly created cloud folder.
 * @throws IOException
  *
 */
public static File createCloudFolder(String project, String key, String secretKey,
        ComputeServiceContext context) throws IOException {

    CloudBootstrapConfiguration cloudConf = ApplicationContext.get().conf().server.cloudBootstrap;
    String cloudifyBuildFolder = ApplicationContext.get().conf().server.environment.cloudifyHome;
    File cloudifyEscFolder = new File(cloudifyBuildFolder, cloudConf.cloudifyEscDirRelativePath);

    //copy the content of hp configuration files to a new folder
    File origCloudFolder = new File(cloudifyEscFolder, cloudConf.cloudName);
    File destFolder = new File(cloudifyEscFolder, cloudConf.cloudName + getTempSuffix());
    FileUtils.copyDirectory(origCloudFolder, destFolder);

    // create new pem file using new credentials.
    File pemFolder = new File(destFolder, cloudConf.cloudifyHpUploadDirName);
    File newPemFile = createPemFile(context);
    FileUtils.copyFile(newPemFile, new File(pemFolder, newPemFile.getName() + ".pem"), true);

    try {
        File propertiesFile = new File(destFolder, cloudConf.cloudPropertiesFileName);

        // GUY - Important - Note - Even though this is the "properties" files, it is not used for "properties" per say
        // we are actually writing a groovy file that defines variables.
        Collection<String> newLines = new LinkedList<String>();
        newLines.add("tenant=" + StringUtils.wrapWithQuotes(project));
        newLines.add("user=" + StringUtils.wrapWithQuotes(key));
        newLines.add("apiKey=" + StringUtils.wrapWithQuotes(secretKey));
        newLines.add("keyFile=" + StringUtils.wrapWithQuotes(newPemFile.getName() + ".pem"));
        newLines.add("keyPair=" + StringUtils.wrapWithQuotes(newPemFile.getName()));
        newLines.add("securityGroup=" + StringUtils.wrapWithQuotes(cloudConf.securityGroup));
        FileUtils.writeLines(propertiesFile, newLines, true);

        return destFolder;
    } catch (Exception e) {
        throw new RuntimeException(String.format("error while writing cloud properties"), e);
    }
}