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, String encoding, 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:com.ctt.persistance.StorageFile.java

public static void saveTransactions(byte[] file) throws IOException {
    try {/*from w ww.j a  va2  s.  co m*/

        List<String> list = new ArrayList<String>();

        CSVParser csv;
        csv = CSVParser.parse(new String(file), CSVFormat.DEFAULT);

        List<CSVRecord> it = csv.getRecords();
        Transaction obj;

        int i = 0;
        for (i = 0; i < it.size(); i++) {
            if (i != 0) {
                obj = new Transaction((CSVRecord) it.get(i));

                if (obj.validateCanonical() && obj.isRepayment()) {
                    list.add(obj.repaymentString());

                    StorageFile.saveTransaction(obj);
                }
            }
        }
        FileUtils.writeLines(new File(Main.prop.getProperty("path_repayments")), list, "\n", true);
    } catch (Exception e) {
        Main.appendLog("Error: SAVE TRANSACTIONS: " + e.getMessage());
    }
}

From source file:com.oracle.javawc.entities.shell.FileLoaderTest.java

/**
 * Test of loadFile method, of class FileLoader.
 *//*from  www  .ja v a 2  s.co m*/
@Test
public void testLoadFile_Path() throws Exception {
    File file1 = testFolder.newFile("file1.txt");

    Collection<String> input = new ArrayList<>();
    Collections.addAll(input, "word1 word2 word3", "word1 word2 word3");
    FileUtils.writeLines(file1, Charset.forName("UTF-8").name(), input, true);

    Path path = file1.toPath();
    FileLoader instance = new FileLoader();
    List<String> lines = instance.loadFile(path);
    WordCount wc = new WordCount();

    assertThat(wc.countLines(lines)).isEqualTo(2);
    assertThat(wc.countWords(lines)).isEqualTo(6);
    assertThat(wc.countAvgLettersPerWords(lines)).isEqualTo(5);
    assertThat(wc.mostCommonLetter(lines)).isEqualTo("d");

}

From source file:com.lynn.controller.FileAction.java

@RequestMapping("/write.do")
@ResponseBody/*w w w .j  a  v  a 2s.co m*/
public String wirte() throws IOException {
    File file = initFile();
    FileUtils.writeStringToFile(file, "hello world\r\n", encoding, true);
    List list = new ArrayList();
    list.add("11");
    list.add("22");
    list.add("33");
    FileUtils.writeLines(file, encoding, list, true);
    return "1";
}

From source file:com.oracle.javawc.entities.shell.FileLoaderTest.java

/**
 * Test of loadFile method, of class FileLoader.
 *///from ww w  . ja v a  2 s  .  c  om
@Test
public void testLoadFile_String() throws Exception {
    File file1 = testFolder.newFile("file1.txt");

    Collection<String> input = new ArrayList<>();
    Collections.addAll(input, "word1 word2 word3", "word1 word2 word3");
    FileUtils.writeLines(file1, Charset.forName("UTF-8").name(), input, true);

    FileLoader instance = new FileLoader();
    List<String> lines = instance.loadFile(file1.getAbsolutePath());
    WordCount wc = new WordCount();

    assertThat(wc.countLines(lines)).isEqualTo(2);
    assertThat(wc.countWords(lines)).isEqualTo(6);
    assertThat(wc.countAvgLettersPerWords(lines)).isEqualTo(5);
    assertThat(wc.mostCommonLetter(lines)).isEqualTo("d");

}

From source file:com.oracle.javawc.entities.shell.FileLoaderTest.java

/**
 * Test of loadFile method, of class FileLoader.
 *//*from   w  w w. ja va 2 s.c  o m*/
@Test
public void testLoadFile_Path_Charset() throws Exception {
    File file1 = testFolder.newFile("file1.txt");

    Collection<String> input = new ArrayList<>();
    Collections.addAll(input, "word1 word2 word3", "word1 word2 word3");
    FileUtils.writeLines(file1, Charset.forName("UTF-8").name(), input, true);

    Path path = file1.toPath();
    FileLoader instance = new FileLoader();
    WordCount result = new WordCount();
    List<String> lines = instance.loadFile(path, Charset.forName("UTF-8"));

    assertThat(result).isNotNull();
    assertThat(result.countLines(lines)).isEqualTo(2);
    assertThat(result.countWords(lines)).isEqualTo(6);
    assertThat(result.countAvgLettersPerWords(lines)).isEqualTo(5);
    assertThat(result.mostCommonLetter(lines)).isEqualTo("d");
}

From source file:com.oracle.javawc.entities.shell.FileLoaderTest.java

/**
 * Test of loadFile method, of class FileLoader.
 *///from  w  w w.j av  a  2  s  .co m
@Test
public void testLoadFile_String_Charset() throws Exception {
    File file1 = testFolder.newFile("file1.txt");

    Collection<String> input = new ArrayList<>();
    Collections.addAll(input, "word1 word2 word3", "word1 word2 word3");
    FileUtils.writeLines(file1, Charset.forName("UTF-8").name(), input, true);

    FileLoader instance = new FileLoader();
    WordCount result = new WordCount();
    List<String> lines = instance.loadFile(file1.getAbsolutePath(), Charset.forName("UTF-8"));

    assertThat(result).isNotNull();
    assertThat(result.countLines(lines)).isEqualTo(2);
    assertThat(result.countWords(lines)).isEqualTo(6);
    assertThat(result.countAvgLettersPerWords(lines)).isEqualTo(5);
    assertThat(result.mostCommonLetter(lines)).isEqualTo("d");
}

From source file:com.screenslicer.common.CommonFile.java

public static void writeLines(File file, Collection<String> lines, boolean append) {
    synchronized (lock(file.getAbsolutePath())) {
        try {/*from   ww  w. j  a  va2 s  . c om*/
            FileUtils.writeLines(file, "utf-8", lines, append);
        } catch (Throwable t) {
            Log.exception(t);
        }
    }
    unlock(file.getAbsolutePath());
}

From source file:org.alex73.skarynka.scan.Book2.java

public synchronized void save() throws Exception {
    LOG.info("Save book " + bookDir);
    List<String> lines = new ArrayList<>();
    get(this, "book.", lines);
    for (Map.Entry<String, PageInfo> en : pages.entrySet()) {
        String p = en.getKey();/*from   w  w w  .  j a  va2s. c  o m*/
        PageInfo pi = en.getValue();
        get(pi, "page." + p + ".", lines);
    }
    Collections.sort(lines);

    File bookFileNew = new File(bookFile.getPath() + ".new");
    File bookFileBak = new File(bookFile.getPath() + ".bak");
    FileUtils.writeLines(bookFileNew, "UTF-8", lines, "\n");
    if (bookFileBak.exists()) {
        if (!bookFileBak.delete()) {
            throw new Exception("Can't delete .bak file");
        }
    }
    if (bookFile.exists()) {
        if (!bookFile.renameTo(bookFileBak)) {
            throw new Exception("Can't rename old file to .bak");
        }
    }
    if (!bookFileNew.renameTo(bookFile)) {
        throw new Exception("Can't rename new file to " + bookFile.getAbsolutePath());
    }
}

From source file:org.codice.ddf.configuration.admin.ConfigurationAdminMigratableTest.java

private Path setupConfigFile(String tag) throws IOException {
    Path configFile = ddfHome.resolve("etc").toRealPath()
            .resolve(ConfigurationAdminMigratableTest.DDF_CUSTOM_MIME_TYPE_RESOLVER_FILENAME);
    Files.createFile(configFile);
    List<String> lines = new ArrayList<>(2);
    lines.add(String.format("#%s:%s", configFile.toRealPath().toString(), tag));
    lines.add(String.format("%s=%s", DirectoryWatcher.FILENAME, configFile.toUri().toString()));
    FileUtils.writeLines(configFile.toFile(), StandardCharsets.UTF_8.toString(), lines, System.lineSeparator());
    return configFile;
}

From source file:org.commonjava.aprox.subsys.datafile.DataFile.java

public void appendLines(final List<String> lines, final String encoding, final ChangeSummary summary)
        throws IOException {
    FileUtils.writeLines(file, lines, encoding, true);
    events.modified(file, summary);// w w  w .ja v a2  s.  c  o m
}