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

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

Introduction

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

Prototype

public static void writeLines(Collection lines, String lineEnding, Writer writer) throws IOException 

Source Link

Document

Writes the toString() value of each item in a collection to a Writer line by line, using the specified line ending.

Usage

From source file:com.doculibre.constellio.utils.izpack.UsersXmlFileUtils.java

public static void createEmptyUsersFile(String fileName) {
    File xmlFile = new File(fileName);

    BufferedWriter writer;//from  www . j ava2s  .  co  m
    try {
        writer = new BufferedWriter(new FileWriter(xmlFile));
    } catch (IOException e) {
        e.printStackTrace();
        return;
    }

    try {
        IOUtils.writeLines(Arrays.asList(emptyFileLines), System.getProperty("line.separator"), writer);
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(writer);
    }

}

From source file:com.github.mojos.distribute.PackageMojo.java

public void execute() throws MojoExecutionException, MojoFailureException {

    if (version != null) {
        packageVersion = version;/*from  w w  w .ja v a2  s  . c  om*/
    }

    //Copy sourceDirectory
    final File sourceDirectoryFile = new File(sourceDirectory);
    final File buildDirectory = Paths.get(project.getBuild().getDirectory(), "py").toFile();

    try {
        FileUtils.copyDirectory(sourceDirectoryFile, buildDirectory);
    } catch (IOException e) {
        throw new MojoExecutionException("Failed to copy source", e);
    }

    final File setup = Paths.get(buildDirectory.getPath(), "setup.py").toFile();
    final boolean setupProvided = setup.exists();

    final File setupTemplate = setupProvided ? setup
            : Paths.get(buildDirectory.getPath(), "setup-template.py").toFile();

    try {
        if (!setupProvided) {
            //update VERSION to latest version
            List<String> lines = new ArrayList<String>();
            final InputStream inputStream = new BufferedInputStream(new FileInputStream(setupTemplate));
            try {
                lines.addAll(IOUtils.readLines(inputStream));
            } finally {
                inputStream.close();
            }

            int index = 0;
            for (String line : lines) {
                line = line.replace(VERSION, packageVersion);
                line = line.replace(PROJECT_NAME, packageName);
                lines.set(index, line);
                index++;
            }

            final OutputStream outputStream = new FileOutputStream(setup);
            try {
                IOUtils.writeLines(lines, "\n", outputStream);
            } finally {
                outputStream.flush();
                outputStream.close();
            }
        }

        //execute setup script
        ProcessBuilder processBuilder = new ProcessBuilder(pythonExecutable, setup.getCanonicalPath(),
                "bdist_egg");
        processBuilder.directory(buildDirectory);
        processBuilder.redirectErrorStream(true);

        Process pr = processBuilder.start();
        int exitCode = pr.waitFor();
        BufferedReader buf = new BufferedReader(new InputStreamReader(pr.getInputStream()));
        String line = "";
        while ((line = buf.readLine()) != null) {
            getLog().info(line);
        }

        if (exitCode != 0) {
            throw new MojoExecutionException("python setup.py returned error code " + exitCode);
        }

    } catch (FileNotFoundException e) {
        throw new MojoExecutionException("Unable to find " + setup.getPath(), e);
    } catch (IOException e) {
        throw new MojoExecutionException("Unable to read " + setup.getPath(), e);
    } catch (InterruptedException e) {
        throw new MojoExecutionException("Unable to execute python " + setup.getPath(), e);
    }

}

From source file:com.smartitengineering.cms.ws.common.providers.TextURIListProvider.java

@Override
public void writeTo(Collection<URI> t, Class<?> type, Type genericType, Annotation[] annotations,
        MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
        throws IOException, WebApplicationException {
    if (isWriteable(type, genericType, annotations, mediaType)) {
        List<String> lines = new ArrayList<String>(t.size());
        for (URI uri : t) {
            if (uri == null) {
                continue;
            }//  w  w  w.j  av  a2s  .c o m
            lines.add(uri.toASCIIString());
        }
        IOUtils.writeLines(lines, null, entityStream);
    }
}

From source file:com.example.listsync.WebDavRepository.java

private void doUpload(String file, List<String> content) throws IOException {
    LOGGER.info("uploading {}", file);
    LOGGER.info("uploading {} -> {}", file, content);
    PutMethod putMethod = new PutMethod(config.getBaseUrl() + config.getWatchpath() + file);
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    IOUtils.writeLines(content, IOUtils.LINE_SEPARATOR_UNIX, output);
    putMethod.setRequestEntity(new ByteArrayRequestEntity(output.toByteArray()));
    int code = client.executeMethod(putMethod);
    LOGGER.info("upload resultcode: {}", code);
}

From source file:com.doculibre.constellio.services.SynonymServicesImpl.java

@Override
public void writeSynonymsFile(RecordCollection collection) {
    List<List<String>> collectionSynonyms = getSynonyms(collection.getName());
    List<String> lines = new ArrayList<String>();
    for (List<String> syn : collectionSynonyms) {
        StringBuilder synDefLine = new StringBuilder();
        for (String term : syn) {
            synDefLine.append(term + ", ");
        }/*from ww w  .ja  v a 2s .c o m*/
        String synDefLineString = synDefLine.toString();
        if (!synDefLineString.isEmpty()) {
            synDefLineString = org.apache.commons.lang.StringUtils.substringBeforeLast(synDefLineString, ",");
            lines.add(synDefLineString);
        }
    }
    //TODO : lors que les filtres auront les parametres necessaires 
    //Pour chacun des analyseurs qui a un filtre de synonymes : "solr.SynonymFilterFactory"
    //1. lire le fichier des synonymes (dans conf ou data) (le creer le cas echeant)
    //2. remplacer son contenu avec lines
    File synonymsFile = ClasspathUtils.getSynonymsFile(collection);

    FileOutputStream ecraserSynonymsFile = null;

    try {
        ecraserSynonymsFile = new FileOutputStream(synonymsFile.getAbsoluteFile());
        IOUtils.writeLines(lines, System.getProperty("line.separator"), ecraserSynonymsFile);
        ecraserSynonymsFile.close();
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        IOUtils.closeQuietly(ecraserSynonymsFile);
    }
}

From source file:com.webguys.djinn.marid.util.BuiltinAnnotationProcessor.java

private void saveDatabase() throws Exception {
    if (!db.exists() && !db.createNewFile()) {
        throw new Exception(String.format("Unable to create the db file: %s", DB_PATHNAME));
    }//w w w .j  a  va 2 s  .c  o  m

    PrintWriter writer = new PrintWriter(db, "UTF-8");
    IOUtils.writeLines(this.builtins, IOUtils.LINE_SEPARATOR_UNIX, writer);
    writer.flush();
    IOUtils.closeQuietly(writer);
}

From source file:io.cloudslang.content.httpclient.build.auth.AuthSchemeProviderLookupBuilder.java

private static File createKrb5Configuration(String domain) throws IOException {
    File tempFile = File.createTempFile("krb", "kdc");
    tempFile.deleteOnExit();//  w ww  . j  ava2 s . c  o m
    ArrayList<String> lines = new ArrayList<>();
    lines.add("[libdefaults]");
    lines.add("\tdefault_realm = " + domain.toUpperCase());
    lines.add("[realms]");
    lines.add("\t" + domain.toUpperCase() + " = {");
    lines.add("\t\tkdc = " + domain);
    lines.add("\t\tadmin_server = " + domain);
    lines.add("\t}");
    FileWriter writer = null;
    try {
        writer = new FileWriter(tempFile);
        IOUtils.writeLines(lines, System.lineSeparator(), writer);
    } finally {
        if (writer != null) {
            //                IOUtils.closeQuietly(writer);
            safeClose(writer);
        }
    }

    return tempFile;
}

From source file:eu.delving.sip.frames.AllFrames.java

private void createDefaultFrameArrangements(File file) throws IOException {
    List<String> lines = IOUtils.readLines(getClass().getResource("/frame-arrangements.xml").openStream());
    IOUtils.writeLines(lines, "\n", new FileOutputStream(file));
}

From source file:io.cloudslang.content.httpclient.build.auth.AuthSchemeProviderLookupBuilder.java

private static File createLoginConfig() throws IOException {
    File tempFile = File.createTempFile("krb", "loginConf");
    tempFile.deleteOnExit();//from   w  w w  . j  a  v  a  2  s.c o m
    ArrayList<String> lines = new ArrayList<>();
    lines.add("com.sun.security.jgss.initiate {\n" + "  " + KrbHttpLoginModule.class.getCanonicalName()
            + " required\n" + "  doNotPrompt=true\n" + "  useFirstPass=true\n" + "  debug=true ;\n" + "};");
    FileWriter writer = null;
    try {
        writer = new FileWriter(tempFile);
        IOUtils.writeLines(lines, System.lineSeparator(), writer);
    } finally {
        if (writer != null) {
            //                IOUtils.closeQuietly(writer);
            safeClose(writer);
        }
    }
    return tempFile;
}

From source file:io.milton.common.FileUtils.java

public static void writeLines(File f, List<String> lines) {
    FileOutputStream fout = null;
    try {/*from w  w w .j a v a 2 s .  co  m*/
        fout = new FileOutputStream(f);
        IOUtils.writeLines(lines, null, fout);
    } catch (Exception e) {
        throw new RuntimeException(f.getAbsolutePath(), e);
    } finally {
        IOUtils.closeQuietly(fout);
    }
}