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) 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:org.sonar.scanner.protocol.output.ScannerReportReaderTest.java

@Test
public void read_source_lines() throws Exception {
    ScannerReportWriter writer = new ScannerReportWriter(dir);
    File file = writer.getFileStructure().fileFor(FileStructure.Domain.SOURCE, 1);
    FileUtils.writeLines(file, Lists.newArrayList("line1", "line2"));

    File sourceFile = new ScannerReportReader(dir).readFileSource(1);
    assertThat(sourceFile).isEqualTo(file);
}

From source file:org.sonar.server.benchmark.PersistFileSourcesStepTest.java

private Component generateFileReport(BatchReportWriter writer, int fileRef) throws IOException {
    LineData lineData = new LineData();
    for (int line = 1; line <= NUMBER_OF_LINES; line++) {
        lineData.generateLineData(line);
        duplicationRepository.add(fileRef, new Duplication(new TextBlock(line, line),
                Arrays.<Duplicate>asList(new InnerDuplicate(new TextBlock(line + 1, line + 1)))));
    }//  w w w . j  ava 2 s .  c  om
    writer.writeComponent(BatchReport.Component.newBuilder().setRef(fileRef)
            .setType(Constants.ComponentType.FILE).setLines(NUMBER_OF_LINES).build());

    FileUtils.writeLines(writer.getSourceFile(fileRef), lineData.lines);
    writer.writeComponentCoverage(fileRef, lineData.coverages);
    writer.writeComponentChangesets(lineData.changesetsBuilder.setComponentRef(fileRef).build());
    writer.writeComponentSyntaxHighlighting(fileRef, lineData.highlightings);
    writer.writeComponentSymbols(fileRef, lineData.symbols);

    return ReportComponent.builder(Component.Type.FILE, fileRef).setUuid(Uuids.create())
            .setKey("PROJECT:" + fileRef).build();
}

From source file:org.sonar.server.computation.batch.BatchReportReaderImplTest.java

@Test
public void verify_readFileSource() throws IOException {
    File file = writer.getSourceFile(COMPONENT_REF);
    FileUtils.writeLines(file, of("1", "2", "3"));

    CloseableIterator<String> res = underTest.readFileSource(COMPONENT_REF).get();
    assertThat(res).containsExactly("1", "2", "3");
    res.close();//from   w w w  . j  av  a  2  s .c o m
}

From source file:org.sonar.server.computation.step.PersistFileSourcesStepTest.java

@Test
public void persist_last_line() throws Exception {
    dbComponentsRefCache.addComponent(1, new DbComponentsRefCache.DbComponent(1L, PROJECT_KEY, PROJECT_UUID));
    dbComponentsRefCache.addComponent(FILE_REF,
            new DbComponentsRefCache.DbComponent(2L, "PROJECT_KEY:file", FILE_UUID));

    BatchReportWriter writer = new BatchReportWriter(reportDir);
    FileUtils.writeLines(writer.getFileStructure().fileFor(FileStructure.Domain.SOURCE, FILE_REF),
            Lists.newArrayList("line1", "line2"));
    writer.writeMetadata(/* w  ww .j a  v  a2 s .  co m*/
            BatchReport.Metadata.newBuilder().setRootComponentRef(1).setProjectKey(PROJECT_KEY).build());
    writer.writeComponent(BatchReport.Component.newBuilder().setRef(1).setType(Constants.ComponentType.PROJECT)
            .addChildRef(FILE_REF).build());
    writer.writeComponent(
            BatchReport.Component.newBuilder().setRef(FILE_REF).setType(Constants.ComponentType.FILE)
                    // Lines is set to 3 but only 2 lines are read from the file -> the last lines should be added
                    .setLines(3).build());

    sut.execute(new ComputationContext(new BatchReportReader(reportDir), PROJECT_KEY, projectSettings, dbClient,
            ComponentTreeBuilders.from(DumbComponent.DUMB_PROJECT), languageRepository));

    assertThat(dbTester.countRowsOfTable("file_sources")).isEqualTo(1);
    FileSourceDto fileSourceDto = dbClient.fileSourceDao().selectSource(FILE_UUID);
    FileSourceDb.Data data = FileSourceDto.decodeSourceData(fileSourceDto.getBinaryData());
    assertThat(data.getLinesCount()).isEqualTo(3);
    assertThat(data.getLines(2).getLine()).isEqualTo(3);
    assertThat(data.getLines(2).getSource()).isEmpty();
}

From source file:org.sonar.server.computation.step.PersistFileSourcesStepTest.java

private BatchReportWriter initBasicReport(int numberOfLines) throws IOException {
    dbComponentsRefCache.addComponent(1, new DbComponentsRefCache.DbComponent(1L, PROJECT_KEY, PROJECT_UUID));
    dbComponentsRefCache.addComponent(2, new DbComponentsRefCache.DbComponent(2L, "MODULE_KEY", "MODULE"));
    dbComponentsRefCache.addComponent(FILE_REF,
            new DbComponentsRefCache.DbComponent(3L, "MODULE_KEY:src/Foo.java", FILE_UUID));

    BatchReportWriter writer = new BatchReportWriter(reportDir);
    writer.writeMetadata(//from w w  w .  j  a v a 2 s.  c o  m
            BatchReport.Metadata.newBuilder().setRootComponentRef(1).setProjectKey(PROJECT_KEY).build());

    writer.writeComponent(BatchReport.Component.newBuilder().setRef(1).setType(Constants.ComponentType.PROJECT)
            .addChildRef(2).build());
    writer.writeComponent(BatchReport.Component.newBuilder().setRef(2).setType(Constants.ComponentType.MODULE)
            .addChildRef(FILE_REF).build());
    writer.writeComponent(BatchReport.Component.newBuilder().setRef(FILE_REF)
            .setType(Constants.ComponentType.FILE).setPath("src/Foo.java").setLines(numberOfLines).build());

    List<String> lines = newArrayList();
    for (int i = 1; i <= numberOfLines; i++) {
        lines.add("line" + i);
    }
    FileUtils.writeLines(writer.getSourceFile(FILE_REF), lines);

    return writer;
}

From source file:org.splevo.vpm.analyzer.semantic.SemanticVPMAnalyzer.java

private void logIndexedTerms() {

    Map<String, Integer> indexedTerms = getTermsFromIndex();

    List<String> lines = Lists.newLinkedList();
    lines.add("Term,VPCount");
    for (String term : indexedTerms.keySet()) {
        lines.add(term + "," + indexedTerms.get(term));
    }/*  w w w.  ja  va  2  s  . c o  m*/

    String logDirectory = getCurrentLogSubDirectory();
    File logFile = new File(logDirectory + "indexed-terms.csv");

    try {
        FileUtils.writeLines(logFile, lines);
    } catch (IOException e) {
        logger.error("Failed to write term log", e);
    }
}

From source file:org.terasoluna.gfw.functionaltest.app.DBLog.java

private void writeLog(String sql, int sequenceNo, String subTitle, String tableName) {
    String evidenceFile = String.format("dblog_%03d%s-%s.log", sequenceNo, subTitle, tableName);
    List<Map<String, Object>> results = jdbcTemplate.queryForList(sql);
    try {//w  w  w  .  j  a va  2 s  .  co  m
        FileUtils.writeLines(new File(evidenceSavingDirectory, evidenceFile), results);
    } catch (IOException e) {
        logger.error(e.toString());
    } finally {
        results.clear();
        results = null;
    }
}

From source file:org.tolven.tools.TolvenInstaller.java

private static void updateScripts(File binDirectory, File templateBinDir, File selectedConfigDir)
        throws IOException {
    String extension = null;//  w  w w  .  j  ava2 s . co m
    if (System.getProperty("os.name").toLowerCase().indexOf("windows") == -1) {
        extension = ".sh";
    } else {
        extension = ".bat";
    }
    Collection<File> scripts = FileUtils.listFiles(templateBinDir, new SuffixFileFilter(extension), null);
    for (File script : scripts) {
        File dest = new File(binDirectory, script.getName());
        if (dest.exists()) {
            dest.delete();
        }
        System.out.println("move: " + script.getPath() + " to: " + binDirectory.getPath());
        FileUtils.moveFileToDirectory(script, binDirectory, false);
        if (script.getName().equals("tpfenv" + extension)) {
            List<String> lines = FileUtils.readLines(dest);
            List<String> sub_lines = new ArrayList<String>();
            boolean tpfenvRequiresUpdate = false;
            for (String line : lines) {
                String replacedLine = line.replace("$TOLVEN_CONFIG", selectedConfigDir.getPath());
                if (!replacedLine.equals(line)) {
                    tpfenvRequiresUpdate = true;
                }
                sub_lines.add(replacedLine);
            }
            if (tpfenvRequiresUpdate) {
                System.out.println("updated: " + dest.getPath());
                FileUtils.writeLines(dest, sub_lines);
            }
        }
    }
}

From source file:org.umlg.eclipselib.Dependency.java

private void correctPomForUpload(File pomTarget) throws IOException {
    List<String> pomAsList = FileUtils.readLines(pomTarget);

    //Find the name
    int nameLine = 0;
    String description = "";
    for (String line : pomAsList) {
        nameLine++;/*from   w  ww . ja v a 2s . co  m*/
        if (line.contains("<name>")) {
            String tmp = line.trim();
            tmp = tmp.substring(6);
            description = tmp.replace("</name>", "");
            break;
        }

    }

    StringBuilder sb = new StringBuilder();
    sb.append("  <description>");
    sb.append(description);
    sb.append("</description>\n");
    sb.append("  <packaging>jar</packaging>\n");
    sb.append("  <url>http://www.eclipse.org</url>\n");
    sb.append("  <scm>\n");
    sb.append("    <url/>\n");
    sb.append("    <connection/>\n");
    sb.append("  </scm>\n");
    sb.append("  <developers>\n");
    sb.append("    <developer/>\n");
    sb.append("  </developers>");
    pomAsList.add(nameLine, sb.toString());

    //Check if there is a licence part, if not give it an eclipse licence
    boolean hasLicence = false;
    int endOfdevelopers = 0;
    nameLine = 0;
    for (String s : pomAsList) {
        nameLine++;
        if (s.contains("</developers>")) {
            endOfdevelopers = nameLine;
        }
        if (s.contains("<licenses>")) {
            hasLicence = true;
            break;
        }
    }
    if (!hasLicence) {
        sb.setLength(0);
        sb.append("  <licenses>\n");
        sb.append("    <license>\n");
        sb.append("      <name>Eclipse Public License - v 1.0</name>\n");
        sb.append("      <url>http://www.eclipse.org/org/documents/epl-v10.html</url>\n");
        sb.append("    </license>\n");
        sb.append("  </licenses>");
        pomAsList.add(endOfdevelopers, sb.toString());
    }

    FileUtils.writeLines(pomTarget, pomAsList);
}

From source file:org.voyanttools.trombone.storage.file.FileStoredDocumentSourceStorage.java

public void setMultipleExpandedStoredDocumentSources(String id,
        List<StoredDocumentSource> multipleExpandedStoredDocumentSources, String prefix) throws IOException {
    List<String> multipleExpandedStoredDocumentSourceIds = new ArrayList<String>();
    for (StoredDocumentSource doc : multipleExpandedStoredDocumentSources) {
        multipleExpandedStoredDocumentSourceIds.add(doc.getId());
    }//from   w w  w .jav  a 2 s.co  m
    File file = getMultipleExpandedStoredDocumentSourcesFile(id, prefix);
    FileUtils.writeLines(file, multipleExpandedStoredDocumentSourceIds);
}