Example usage for java.nio.file Files newBufferedWriter

List of usage examples for java.nio.file Files newBufferedWriter

Introduction

In this page you can find the example usage for java.nio.file Files newBufferedWriter.

Prototype

public static BufferedWriter newBufferedWriter(Path path, OpenOption... options) throws IOException 

Source Link

Document

Opens or creates a file for writing, returning a BufferedWriter to write text to the file in an efficient manner.

Usage

From source file:net.sourceforge.pmd.util.IOUtil.java

public static Writer createWriter(String reportFile) {
    try {//from ww  w. ja  v  a  2 s  .  c  o  m
        return StringUtils.isBlank(reportFile) ? createWriter()
                : Files.newBufferedWriter(new File(reportFile).toPath(), Charset.defaultCharset());
    } catch (IOException e) {
        throw new IllegalArgumentException(e);
    }
}

From source file:io.druid.query.aggregation.datasketches.tuple.GenerateTestData.java

private static void generateSketches() throws Exception {
    Path path = FileSystems.getDefault().getPath("array_of_doubles_sketch_data.tsv");
    try (BufferedWriter out = Files.newBufferedWriter(path, StandardCharsets.UTF_8)) {
        Random rand = new Random();
        int key = 0;
        for (int i = 0; i < 20; i++) {
            ArrayOfDoublesUpdatableSketch sketch = new ArrayOfDoublesUpdatableSketchBuilder()
                    .setNominalEntries(1024).build();
            sketch.update(key++, new double[] { 1 });
            sketch.update(key++, new double[] { 1 });
            out.write("2015010101");
            out.write('\t');
            out.write("product_" + (rand.nextInt(10) + 1));
            out.write('\t');
            out.write(Base64.encodeBase64String(sketch.compact().toByteArray()));
            out.newLine();//  w  ww  .  j a v  a2  s  . c  o m
        }
    }
}

From source file:org.apache.druid.query.aggregation.datasketches.hll.GenerateTestData.java

private static void generateSketches() throws Exception {
    int lgK = 12;
    String date = "20170101";
    Path rawPath = FileSystems.getDefault().getPath("hll_raw.tsv");
    Path sketchPath = FileSystems.getDefault().getPath("hll_sketches.tsv");
    try (BufferedWriter out1 = Files.newBufferedWriter(rawPath, StandardCharsets.UTF_8)) {
        try (BufferedWriter out2 = Files.newBufferedWriter(sketchPath, StandardCharsets.UTF_8)) {
            Random rand = ThreadLocalRandom.current();
            int key = 0;
            for (int i = 0; i < 100; i++) {
                HllSketch sketch = new HllSketch(lgK);
                String dimension = Integer.toString(rand.nextInt(10) + 1);
                writeRawRecord(out1, date, dimension, key);
                sketch.update(key++);/*w w w  . jav a 2 s.  c om*/
                writeRawRecord(out1, date, dimension, key);
                sketch.update(key++);
                writeSketchRecord(out2, date, dimension, sketch);
            }
        }
    }
}

From source file:org.apache.druid.query.aggregation.datasketches.tuple.GenerateTestData.java

private static void generateSketches() throws Exception {
    Path path = FileSystems.getDefault().getPath("array_of_doubles_sketch_data.tsv");
    try (BufferedWriter out = Files.newBufferedWriter(path, StandardCharsets.UTF_8)) {
        Random rand = ThreadLocalRandom.current();
        int key = 0;
        for (int i = 0; i < 20; i++) {
            ArrayOfDoublesUpdatableSketch sketch = new ArrayOfDoublesUpdatableSketchBuilder()
                    .setNominalEntries(1024).build();
            sketch.update(key++, new double[] { 1 });
            sketch.update(key++, new double[] { 1 });
            out.write("2015010101");
            out.write('\t');
            out.write("product_" + (rand.nextInt(10) + 1));
            out.write('\t');
            out.write(Base64.encodeBase64String(sketch.compact().toByteArray()));
            out.newLine();//from   w  w  w  .  ja  v  a  2 s  .co m
        }
    }
}

From source file:org.apache.tika.tools.TopCommonTokenCounterTest.java

@BeforeClass
public static void setUp() throws Exception {
    String[] docs = new String[] { "the quick brown fox", "jumped over the brown lazy", "brown lazy fox",
            "\u666e\u6797\u65af\u987f\u5927\u5b66", "\u666e\u6797\u65af\u987f\u5927\u5b66" };

    WORKING_DIR = Files.createTempDirectory("tika-eval-common-tokens");
    try (BufferedWriter writer = Files.newBufferedWriter(WORKING_DIR.resolve(INPUT_FILE),
            StandardCharsets.UTF_8)) {
        //do this 10 times to bump the numbers above the TopCommonTokenCounter's MIN_DOC_FREQ
        for (int i = 0; i < 10; i++) {
            for (String d : docs) {
                writer.write(d);//from  w w  w.  j  a  va  2 s .c om
                writer.newLine();
            }
        }
        writer.flush();
    }
    TopCommonTokenCounter.main(new String[] {
            ProcessUtils.escapeCommandLine(WORKING_DIR.resolve(INPUT_FILE).toAbsolutePath().toString()),
            ProcessUtils
                    .escapeCommandLine(WORKING_DIR.resolve(COMMON_TOKENS_FILE).toAbsolutePath().toString()) });
}

From source file:eu.itesla_project.computation.mpi.ExportTasksStatisticsTool.java

@Override
public void run(CommandLine line) throws Exception {
    Path statisticsDbDir = Paths.get(line.getOptionValue("statistics-db-dir"));
    String statisticsDbName = line.getOptionValue("statistics-db-name");
    Path outputFile = Paths.get(line.getOptionValue("output-file"));
    try (MpiStatistics statistics = new CsvMpiStatistics(statisticsDbDir, statisticsDbName)) {
        try (Writer writer = Files.newBufferedWriter(outputFile, StandardCharsets.UTF_8)) {
            statistics.exportTasksToCsv(writer);
        }/*from w w  w .ja v a2 s.  co m*/
    }
}

From source file:azkaban.project.FlowLoaderUtils.java

/**
 * Sets props in flow yaml file./*ww w  .  jav  a 2  s.  c  o  m*/
 *
 * @param path the flow or job path delimited by ":", e.g. "flow:subflow1:subflow2:job3"
 * @param flowFile the flow yaml file
 * @param prop the props to set
 */
public static void setPropsInYamlFile(final String path, final File flowFile, final Props prop) {
    final DumperOptions options = new DumperOptions();
    options.setDefaultFlowStyle(FlowStyle.BLOCK);
    final NodeBean nodeBean = FlowLoaderUtils.setPropsInNodeBean(path, flowFile, prop);
    try (final BufferedWriter writer = Files.newBufferedWriter(flowFile.toPath(), StandardCharsets.UTF_8)) {
        new Yaml(options).dump(nodeBean, writer);
    } catch (final IOException e) {
        throw new ProjectManagerException("Failed to set properties in flow file " + flowFile.getName());
    }
}

From source file:net.sourceforge.pmd.lang.dfa.report.ReportHTMLPrintVisitor.java

/**
 * Writes the buffer to file.//from w w w . j  a va 2 s . c o  m
 */
private void write(String filename, StringBuilder buf) throws IOException {
    try (BufferedWriter bw = Files.newBufferedWriter(new File(baseDir + FILE_SEPARATOR + filename).toPath(),
            StandardCharsets.UTF_8)) {
        bw.write(buf.toString(), 0, buf.length());
    }
}

From source file:eu.itesla_project.modules.offline.ExportMetricsTool.java

@Override
public void run(CommandLine line) throws Exception {
    String metricsDbName = line.hasOption("metrics-db-name") ? line.getOptionValue("metrics-db-name")
            : OfflineConfig.DEFAULT_METRICS_DB_NAME;
    OfflineConfig config = OfflineConfig.load();
    MetricsDb metricsDb = config.getMetricsDbFactoryClass().newInstance().create(metricsDbName);
    String workflowId = line.getOptionValue("workflow");
    Path outputFile = Paths.get(line.getOptionValue("output-file"));
    char delimiter = ';';
    if (line.hasOption("delimiter")) {
        String value = line.getOptionValue("delimiter");
        if (value.length() != 1) {
            throw new RuntimeException("A character is expected");
        }/*from w  w  w  . ja v a  2 s. c  o  m*/
        delimiter = value.charAt(0);
    }
    try (Writer writer = Files.newBufferedWriter(outputFile, StandardCharsets.UTF_8)) {
        metricsDb.exportCsv(workflowId, writer, delimiter);
    }
}

From source file:io.druid.query.aggregation.datasketches.tuple.GenerateTestData.java

private static void generateBucketTestData() throws Exception {
    double meanTest = 10;
    double meanControl = 10.2;
    Path path = FileSystems.getDefault().getPath("bucket_test_data.tsv");
    try (BufferedWriter out = Files.newBufferedWriter(path, StandardCharsets.UTF_8)) {
        Random rand = new Random();
        for (int i = 0; i < 1000; i++) {
            writeBucketTestRecord(out, "test", i, rand.nextGaussian() + meanTest);
            writeBucketTestRecord(out, "control", i, rand.nextGaussian() + meanControl);
        }/*from   w w w  . j  av  a2s  . c  om*/
    }
}