Example usage for java.nio.file Files lines

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

Introduction

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

Prototype

public static Stream<String> lines(Path path, Charset cs) throws IOException 

Source Link

Document

Read all lines from a file as a Stream .

Usage

From source file:com.saptarshidebnath.lib.processrunner.process.RunnerFactoryTest.java

@Test
public void testSaveSysOutAndSaveSysError() throws IOException, ProcessException, InterruptedException,
        ProcessConfigurationException, ExecutionException {
    final File tempFile = File.createTempFile(ProcessRunnerConstants.FILE_PREFIX_NAME_LOG_DUMP,
            ProcessRunnerConstants.FILE_SUFFIX_JSON);
    Runner runner = null;/*from   ww w  .  ja  v a 2s .c  o m*/
    Configuration configuration = null;
    if (SystemUtils.IS_OS_WINDOWS) {
        configuration = new ConfigBuilder("cmd /c", "test.bat").enableLogStreaming(Boolean.TRUE)
                .setWorkigDir(new File(ProcessRunnerConstants.DEFAULT_CURRENT_DIR.getAbsolutePath()
                        + File.separator + "src" + File.separator + "test" + File.separator + "scripts"
                        + File.separator + "batch").toPath())
                .setMasterLogFile(tempFile, Boolean.TRUE).build();
        runner = RunnerFactory.getRunner(configuration);
    } else if (SystemUtils.IS_OS_LINUX) {
        System.out.println("LINUX");
        configuration = new ConfigBuilder("bash", "test.sh").enableLogStreaming(Boolean.TRUE)
                .setWorkigDir(new File(ProcessRunnerConstants.DEFAULT_CURRENT_DIR.getAbsolutePath()
                        + File.separator + "src" + File.separator + "test" + File.separator + "scripts"
                        + File.separator + "shell").toPath())
                .setMasterLogFile(tempFile, Boolean.TRUE).build();
        runner = RunnerFactory.getRunner(configuration);
    }
    assertThat("Validating if processrunner got created or not", runner, not(nullValue()));
    assert runner != null;
    final Output response = runner.run();
    final File masterLog = response.getMasterLogAsJson();
    // masterLog.deleteOnExit();
    final File sysout = response
            .saveSysOut(File.createTempFile(ProcessRunnerConstants.FILE_PREFIX_NAME_LOG_DUMP + "-sysout-",
                    ProcessRunnerConstants.FILE_SUFFIX_JSON));
    sysout.deleteOnExit();
    final File syserr = response
            .saveSysError(File.createTempFile(ProcessRunnerConstants.FILE_PREFIX_NAME_LOG_DUMP + "-syserr-",
                    ProcessRunnerConstants.FILE_SUFFIX_JSON));
    syserr.deleteOnExit();
    assertThat("Validating process return code : ", response.getReturnCode(), is(0));
    assertThat("Validating if JSON log dump is created : ", masterLog.exists(), is(true));

    List<OutputRecord> jsonLog = Files
            .lines(Paths.get(masterLog.getCanonicalPath()), configuration.getCharset())
            .map(line -> GSON.fromJson(line, OutputRecord.class)).collect(Collectors.toList());

    assertThat("Validating json log record number : ", jsonLog.size(), is(greaterThan(0)));
    assertThat("Validating number of input on SYSERR : ", getFileLineNumber(syserr), is(greaterThan(0)));
    assertThat("Validating number of input on SYSOUT : ", getFileLineNumber(sysout), is(greaterThan(0)));
}

From source file:com.saptarshidebnath.lib.processrunner.process.RunnerFactoryTest.java

@Test
public void testScriptWithLargeOutput() throws IOException, ProcessException, InterruptedException,
        ProcessConfigurationException, ExecutionException {
    final File tempFile = File.createTempFile(ProcessRunnerConstants.FILE_PREFIX_NAME_LOG_DUMP,
            ProcessRunnerConstants.FILE_SUFFIX_JSON);
    tempFile.deleteOnExit();//w ww . j  ava2 s. co m
    Configuration configuration = null;
    Runner runner = null;
    if (SystemUtils.IS_OS_WINDOWS) {
        configuration = new ConfigBuilder("cmd /c", "largefile.bat").setWorkigDir(
                new File(ProcessRunnerConstants.DEFAULT_CURRENT_DIR.getAbsolutePath() + File.separator + "src"
                        + File.separator + "test" + File.separator + "scripts" + File.separator + "batch")
                                .toPath())
                .setMasterLogFile(tempFile, true).build();
        runner = RunnerFactory.getRunner(configuration);
    } else if (SystemUtils.IS_OS_LINUX) {
        configuration = new ConfigBuilder("bash", "largefile.sh").setWorkigDir(
                new File(ProcessRunnerConstants.DEFAULT_CURRENT_DIR.getAbsolutePath() + File.separator + "src"
                        + File.separator + "test" + File.separator + "scripts" + File.separator + "shell")
                                .toPath())
                .setMasterLogFile(tempFile, true).build();
        runner = RunnerFactory.getRunner(configuration);
    }

    assertThat("Validating if processrunner is not null", runner, not(nullValue()));
    assert runner != null;
    final Output response = runner.run();
    final File sysout = response.saveSysOut(File.createTempFile("temp-file-sysout", ".json"));
    sysout.deleteOnExit();
    final File syserr = response.saveSysError(File.createTempFile("temp-file-syserr", ".json"));
    final File masterLog = response.getMasterLogAsJson();
    masterLog.deleteOnExit();
    syserr.deleteOnExit();
    assertThat("Validating process return code : ", response.getReturnCode(), is(0));
    assertThat("Validating if JSON log dump is created : ", masterLog.exists(), is(true));
    List<OutputRecord> jsonLog = Files
            .lines(Paths.get(masterLog.getCanonicalPath()), configuration.getCharset())
            .map(line -> GSON.fromJson(line, OutputRecord.class)).collect(Collectors.toList());
    assertThat("Validating json log record number : ", jsonLog.size(), is(greaterThan(0)));
    assertThat("Validating number of input on SYSERR : ", getFileLineNumber(syserr), is(greaterThan(0)));
    assertThat("Validating number of input on SYSOUT : ", getFileLineNumber(sysout), is(greaterThan(0)));
}

From source file:io.siddhi.extension.io.file.FileSinkTestCase.java

@Test
public void fileSinkTest11() throws InterruptedException, CannotRestoreSiddhiAppStateException, IOException {
    log.info("test SiddhiIoFile Sink 11");

    String streams = "" + "@App:name('TestSiddhiApp')"
            + "define stream FooStream (symbol string, price float, volume long); "
            + "@sink(type='file', append='true', add.line.separator='false', @map(type='xml'), append='false', "
            + "file.uri='" + sinkUri + "/test1.xml') "
            + "define stream BarStream (symbol string, price float, volume long); ";

    String query = "" + "from FooStream " + "select * " + "insert into BarStream; ";

    SiddhiManager siddhiManager = new SiddhiManager();
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    InputHandler stockStream = siddhiAppRuntime.getInputHandler("FooStream");

    siddhiAppRuntime.start();/* w w  w .  java  2 s.  c  o m*/

    stockStream.send(new Object[] { "WSO2", 55.6f, 100L });
    stockStream.send(new Object[] { "IBM", 57.678f, 100L });
    stockStream.send(new Object[] { "GOOGLE", 50f, 100L });
    stockStream.send(new Object[] { "REDHAT", 50f, 100L });
    Thread.sleep(100);

    File sink = new File(sinkUri);
    if (sink.isDirectory()) {
        for (File file : sink.listFiles()) {
            Stream<String> lines = Files.lines(file.toPath(), StandardCharsets.UTF_8);
            AssertJUnit.assertEquals(1, lines.count());
        }
    } else {
        AssertJUnit.fail(sinkUri + " is not a directory.");
    }

    Thread.sleep(1000);
    siddhiAppRuntime.shutdown();
}

From source file:io.siddhi.extension.io.file.FileSinkTestCase.java

@Test
public void fileSinkTest12() throws InterruptedException, CannotRestoreSiddhiAppStateException, IOException {
    log.info("test SiddhiIoFile Sink 12");

    String streams = "" + "@App:name('TestSiddhiApp')"
            + "define stream FooStream (symbol string, price float, volume long); "
            + "@sink(type='file', @map(type='xml'), append='true', " + "file.uri='" + sinkUri + "/test2.xml') "
            + "define stream BarStream (symbol string, price float, volume long); ";

    String query = "" + "from FooStream " + "select * " + "insert into BarStream; ";

    SiddhiManager siddhiManager = new SiddhiManager();
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    InputHandler stockStream = siddhiAppRuntime.getInputHandler("FooStream");

    siddhiAppRuntime.start();//from   ww  w  .  j  a v  a 2  s.  c o  m

    stockStream.send(new Object[] { "WSO2", 55.6f, 100L });
    stockStream.send(new Object[] { "IBM", 57.678f, 100L });
    stockStream.send(new Object[] { "GOOGLE", 50f, 100L });
    stockStream.send(new Object[] { "REDHAT", 50f, 100L });
    Thread.sleep(100);

    File sink = new File(sinkUri);
    if (sink.isDirectory()) {
        for (File file : sink.listFiles()) {
            Stream<String> lines = Files.lines(file.toPath(), StandardCharsets.UTF_8);
            AssertJUnit.assertEquals(4, lines.count());
        }
    } else {
        AssertJUnit.fail(sinkUri + " is not a directory.");
    }

    Thread.sleep(1000);
    siddhiAppRuntime.shutdown();
}

From source file:io.siddhi.extension.io.file.FileSinkTestCase.java

@Test
public void fileSinkTest13() throws InterruptedException, CannotRestoreSiddhiAppStateException, IOException {
    log.info("test SiddhiIoFile Sink 13");

    String streams = "" + "@App:name('TestSiddhiApp')"
            + "define stream FooStream (symbol string, price float, volume long); "
            + "@sink(type='file', add.line.separator='false', @map(type='csv'), append='true', " + "file.uri='"
            + sinkUri + "/test3.xml') " + "define stream BarStream (symbol string, price float, volume long); ";

    String query = "" + "from FooStream " + "select * " + "insert into BarStream; ";

    SiddhiManager siddhiManager = new SiddhiManager();
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    InputHandler stockStream = siddhiAppRuntime.getInputHandler("FooStream");

    siddhiAppRuntime.start();//w  ww . j a va2 s  .  c  o  m

    stockStream.send(new Object[] { "WSO2", 55.6f, 100L });
    stockStream.send(new Object[] { "IBM", 57.678f, 100L });
    stockStream.send(new Object[] { "GOOGLE", 50f, 100L });
    stockStream.send(new Object[] { "REDHAT", 50f, 100L });
    Thread.sleep(100);

    File sink = new File(sinkUri);
    if (sink.isDirectory()) {
        for (File file : sink.listFiles()) {
            Stream<String> lines = Files.lines(file.toPath(), StandardCharsets.UTF_8);
            AssertJUnit.assertEquals(4, lines.count());
        }
    } else {
        AssertJUnit.fail(sinkUri + " is not a directory.");
    }

    siddhiAppRuntime.shutdown();
}

From source file:io.siddhi.extension.io.file.FileSinkTestCase.java

@Test
public void fileSinkTest14() throws InterruptedException, CannotRestoreSiddhiAppStateException, IOException {
    log.info("test SiddhiIoFile Sink 14");

    String streams = "" + "@App:name('TestSiddhiApp')"
            + "define stream FooStream (symbol string, price float, volume long); "
            + "@sink(type='file', @map(type='csv'), append='true', " + "file.uri='" + sinkUri + "/test4.xml') "
            + "define stream BarStream (symbol string, price float, volume long); ";

    String query = "" + "from FooStream " + "select * " + "insert into BarStream; ";

    SiddhiManager siddhiManager = new SiddhiManager();
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    InputHandler stockStream = siddhiAppRuntime.getInputHandler("FooStream");

    siddhiAppRuntime.start();//from w ww.  j ava 2 s .  c  om

    stockStream.send(new Object[] { "WSO2", 55.6f, 100L });
    stockStream.send(new Object[] { "IBM", 57.678f, 100L });
    stockStream.send(new Object[] { "GOOGLE", 50f, 100L });
    stockStream.send(new Object[] { "REDHAT", 50f, 100L });
    Thread.sleep(100);

    File sink = new File(sinkUri);
    if (sink.isDirectory()) {
        for (File file : sink.listFiles()) {
            Stream<String> lines = Files.lines(file.toPath(), StandardCharsets.UTF_8);
            AssertJUnit.assertEquals(4, lines.count());
        }
    } else {
        AssertJUnit.fail(sinkUri + " is not a directory.");
    }

    Thread.sleep(1000);
    siddhiAppRuntime.shutdown();
}

From source file:org.dataconservancy.packaging.tool.integration.PackageGenerationTest.java

/**
 * Reads in any BagIt file that uses a ':' to delimit a keyword and value pair.
 *
 * @param bagItFile the file to read/*from  w w  w .  ja va 2  s .com*/
 * @return a Map keyed by the keywords, with the List of values as they appear in the file
 * @throws IOException
 */
private Map<String, List<String>> parseBagItKeyValuesFile(File bagItFile) throws IOException {
    Map<String, List<String>> result = new HashMap<>();

    // Used to track state; a streams no-no.  Probably should do this the old-fashioned way.
    BitSet bitSet = new BitSet(1);
    bitSet.set(0);
    StringBuilder key = new StringBuilder();

    Files.lines(bagItFile.toPath(), Charset.forName("UTF-8")).flatMap(line -> Stream
            .of(line.substring(0, line.indexOf(":")), line.substring(line.indexOf(":") + 1).trim()))
            .forEach(token -> {
                if (bitSet.get(0)) {
                    // key
                    key.delete(0, key.length());
                    result.putIfAbsent(token, new ArrayList<>());
                    key.append(token);
                    bitSet.clear(0);
                } else {
                    // value
                    result.get(key.toString()).add(token);
                    bitSet.set(0);
                }
            });

    return result;
}