Java Code Examples for java.nio.file.Files#readAllLines()

The following examples show how to use java.nio.file.Files#readAllLines() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OpenJ9SwitchParser.java    From VMOptionsExplorer with BSD 2-Clause "Simplified" License 7 votes vote down vote up
private void parseJVMInitHeader(File file) throws IOException
{
	List<String> lines = Files.readAllLines(file.toPath());

	for (String line : lines)
	{
		String trimmed = line.trim();

		if (isJVMInitSwitch(trimmed))
		{
			trimmed = trimmed.replace("<", "&lt;").replace(">", "&gt;");
			
			String name = cleanName(ParseUtil.getBetween(trimmed, "\"", "\""));

			SwitchInfo info = new SwitchInfo(PREFIX_XX, name.trim());

			switchMap.put(info.getKey(), info);
		}
	}
}
 
Example 2
Source File: Node.java    From bt with Apache License 2.0 6 votes vote down vote up
void initKey(DHTConfiguration config)
{
	if(config != null && config.isPersistingID()) {
		Path keyPath = config.getStoragePath().resolve("baseID.config");
		File keyFile = keyPath.toFile();
		
		if (keyFile.exists() && keyFile.isFile()) {
			try {
				List<String> raw = Files.readAllLines(keyPath);
				baseKey = raw.stream().map(String::trim).filter(Key.STRING_PATTERN.asPredicate()).findAny().map(Key::new).orElseThrow(() -> new IllegalArgumentException(keyPath.toString()+" did not contain valid node ID"));
				return;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	baseKey = Key.createRandomKey();
	
	persistKey();
}
 
Example 3
Source File: CollectdParser.java    From datacollector with Apache License 2.0 6 votes vote down vote up
private void loadAuthFile(String authFileLocation) {
  authKeys = new HashMap<>();
  if (authFileLocation == null || authFileLocation.isEmpty()) {
    securityLevel = SecurityLevel.NONE;
    return;
  }

  try {
    Path authFilePath = FileSystems.getDefault().getPath(authFileLocation); // NOSONAR
    List<String> lines = Files.readAllLines(authFilePath, charset);

    for (String line : lines) {
      String[] auth = line.split(":");
      authKeys.put(auth[0].trim(), auth[1].trim());
    }
  } catch (IOException e) {
    LOG.error("Failed to read auth file.", e);
  }
  securityLevel = SecurityLevel.SIGN;
}
 
Example 4
Source File: EqualsFactoryTest.java    From super-csv-annotation with Apache License 2.0 6 votes vote down vote up
@Override
public Collection<Integer> getEqualedValues(final FieldAccessor field) {
    
    final List<String> lines;
    try {
        lines = Files.readAllLines(
                new File("src/test/data/data_equaled_value.txt").toPath(), Charset.forName("UTF-8"));
        
    } catch (IOException e) {
        throw new RuntimeException("fail reading the equaled value file.", e);
    }
    
    return lines.stream()
            .map(l -> Integer.valueOf(l))
            .collect(Collectors.toList());
    
}
 
Example 5
Source File: ServerTofaTest.java    From incubator-tuweni with Apache License 2.0 5 votes vote down vote up
@Test
void shouldValidateOnFirstUse() throws Exception {
  HttpClientRequest req = fooClient.get(httpServer.actualPort(), "localhost", "/upcheck");
  CompletableFuture<HttpClientResponse> respFuture = new CompletableFuture<>();
  req.handler(respFuture::complete).exceptionHandler(respFuture::completeExceptionally).end();
  HttpClientResponse resp = respFuture.join();
  assertEquals(200, resp.statusCode());

  List<String> knownClients = Files.readAllLines(knownClientsFile);
  assertEquals(3, knownClients.size());
  assertEquals("#First line", knownClients.get(0));
  assertEquals("foobar.com " + DUMMY_FINGERPRINT, knownClients.get(1));
  assertEquals("foo.com " + fooFingerprint, knownClients.get(2));
}
 
Example 6
Source File: PasswordsDbPlaintext.java    From token-dispenser with GNU General Public License v2.0 5 votes vote down vote up
private void readStorage() throws IOException {
    Server.LOG.info("Reading " + path);
    int lineNum = 0;
    for (String line: Files.readAllLines(Paths.get(path))) {
        lineNum++;
        String[] pair = line.split(FIELD_SEPARATOR);
        if (pair.length != 2) {
            Server.LOG.warn("Line " + lineNum + " is invalid");
            continue;
        }
        passwords.put(pair[0], pair[1]);
    }
}
 
Example 7
Source File: ReadFileUsingFiles.java    From journaldev with MIT License 5 votes vote down vote up
public static void main(String[] args) {
	try {
		List<String> allLines = Files.readAllLines(Paths.get("/Users/pankaj/Downloads/myfile.txt"));
		for (String line : allLines) {
			System.out.println(line);
		}
	} catch (IOException e) {
		e.printStackTrace();
	}
}
 
Example 8
Source File: OSGiLibBundleDeployerTest.java    From carbon-kernel with Apache License 2.0 5 votes vote down vote up
private static List<BundleInfo> getActualBundleInfo(Path bundleInfoFile) throws IOException {
    if ((bundleInfoFile != null) && (Files.exists(bundleInfoFile))) {
        List<String> bundleInfoLines = Files.readAllLines(bundleInfoFile);
        List<BundleInfo> bundleInfo = new ArrayList<>();
        bundleInfoLines
                .stream()
                .forEach(line -> bundleInfo.add(BundleInfo.getInstance(line)));

        return bundleInfo;
    } else {
        throw new IOException("Invalid bundles.info file specified");
    }
}
 
Example 9
Source File: ClientWhitelistTest.java    From incubator-tuweni with Apache License 2.0 5 votes vote down vote up
@AfterEach
void cleanupClient() throws Exception {
  client.close();

  List<String> knownServers = Files.readAllLines(knownServersFile);
  assertEquals(2, knownServers.size(), "Host was verified via TOFU and not CA");
  assertEquals("#First line", knownServers.get(0));
  assertEquals("localhost:" + fooServer.actualPort() + " " + fooFingerprint, knownServers.get(1));
}
 
Example 10
Source File: BytesAndLines.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Exercise Files.readAllLines(Path)
 */
public void testReadAllLinesUTF8() throws IOException {
    Files.write(tmpfile, encodeAsUTF8(EN_STRING + "\n" + JA_STRING));

    List<String> lines = Files.readAllLines(tmpfile);
    assertTrue(lines.size() == 2, "Read " + lines.size() + " lines instead of 2");
    assertTrue(lines.get(0).equals(EN_STRING));
    assertTrue(lines.get(1).equals(JA_STRING));

    // a sample of malformed sequences
    testReadAllLinesMalformedUTF8((byte)0xFF); // one-byte sequence
    testReadAllLinesMalformedUTF8((byte)0xC0, (byte)0x80);  // invalid first byte
    testReadAllLinesMalformedUTF8((byte)0xC2, (byte)0x00); // invalid second byte
}
 
Example 11
Source File: FileConsoleTest.java    From copybara with Apache License 2.0 5 votes vote down vote up
private void runTest(Path file) throws IOException {
  TestingConsole delegate = new TestingConsole();
  try (FileConsole fileConsole = new FileConsole(delegate, file, Duration.ZERO)) {
    fileConsole.startupMessage("v1");
    fileConsole.info("This is info");
    fileConsole.warn("This is warning");
    fileConsole.error("This is error");
    fileConsole.verbose("This is verbose");
    fileConsole.progress("This is progress");
  }

  List<String> lines = Files.readAllLines(file);
  assertThat(lines).hasSize(6);
  assertThat(lines.get(0)).contains("INFO: Copybara source mover (Version: v1)");
  assertThat(lines.get(1)).contains("INFO: This is info");
  assertThat(lines.get(2)).contains("WARNING: This is warning");
  assertThat(lines.get(3)).contains("ERROR: This is error");
  assertThat(lines.get(4)).contains("VERBOSE: This is verbose");
  assertThat(lines.get(5)).contains("PROGRESS: This is progress");

  delegate
      .assertThat()
      .matchesNext(MessageType.INFO, "Copybara source mover [(]Version: v1[)]")
      .matchesNext(MessageType.INFO, "This is info")
      .matchesNext(MessageType.WARNING, "This is warning")
      .matchesNext(MessageType.ERROR, "This is error")
      .matchesNext(MessageType.VERBOSE, "This is verbose")
      .matchesNext(MessageType.PROGRESS, "This is progress");
}
 
Example 12
Source File: ITStylesheet.java    From japicmp with Apache License 2.0 5 votes vote down vote up
@Test
public void testStylesheetIsUsed() throws IOException {
	Path htmlPath = Paths.get(System.getProperty("user.dir"), "target", "japicmp", "single-version.html");
	assertThat(Files.exists(htmlPath), is(true));
	List<String> htmlLines = Files.readAllLines(htmlPath, Charset.forName("UTF-8"));
	List<String> cssLines = Files.readAllLines(Paths.get(System.getProperty("user.dir"), "src", "main", "resources", "css", "stylesheet.css"), Charset.forName("UTF-8"));
	assertThat(htmlLines.size(), not(is(0)));
	assertThat(cssLines.size(), not(is(0)));
	for (String cssLine : cssLines) {
		assertThat(htmlLines, hasItem(cssLine));
	}
}
 
Example 13
Source File: BytesAndLines.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Exercise Files.write(Path, Iterable<? extends CharSequence>, OpenOption...)
 */
public void testWriteLinesUTF8() throws IOException {
    List<String> lines = Arrays.asList(EN_STRING, JA_STRING);
    Files.write(tmpfile, lines);
    List<String> actual = Files.readAllLines(tmpfile, UTF_8);
    assertTrue(actual.equals(lines), "Unexpected lines");
}
 
Example 14
Source File: TestDroppedSpans.java    From incubator-retired-htrace with Apache License 2.0 5 votes vote down vote up
/**
 * Test that we can write to the dropped spans log.
 */
@Test(timeout = 60000)
public void testWriteToDroppedSpansLog() throws Exception {
  final String logPath = new File(
      tempDir.toFile(), "testWriteToDroppedSpansLog").getAbsolutePath();
  HTraceConfiguration conf = HTraceConfiguration.fromMap(
      new HashMap<String, String>() {{
        put(Conf.ADDRESS_KEY, "127.0.0.1:8080");
        put(TracerId.TRACER_ID_KEY, "testWriteToDroppedSpansLog");
        put(Conf.DROPPED_SPANS_LOG_PATH_KEY, logPath);
        put(Conf.DROPPED_SPANS_LOG_MAX_SIZE_KEY, "128");
      }});
  HTracedSpanReceiver rcvr = new HTracedSpanReceiver(conf);
  try {
    final String LINE1 = "This is a test of the dropped spans log.";
    rcvr.appendToDroppedSpansLog(LINE1 + "\n");
    final String LINE2 = "These lines should appear in the log.";
    rcvr.appendToDroppedSpansLog(LINE2 + "\n");
    try {
      rcvr.appendToDroppedSpansLog("This line won't be written because we're " +
          "out of space.");
      Assert.fail("expected append to fail because of lack of space");
    } catch (IOException e) {
      // ignore
    }
    List<String> lines =
        Files.readAllLines(Paths.get(logPath), StandardCharsets.UTF_8);
    Assert.assertEquals(2, lines.size());
    Assert.assertEquals(LINE1, lines.get(0).substring(25));
    Assert.assertEquals(LINE2, lines.get(1).substring(25));
  } finally {
    rcvr.close();
  }
}
 
Example 15
Source File: ClientRecordTest.java    From cava with Apache License 2.0 5 votes vote down vote up
@Test
void shouldRecordMultipleHosts() throws Exception {
  CompletableFuture<Integer> statusCode = new CompletableFuture<>();
  client
      .post(fooServer.actualPort(), "localhost", "/sample", response -> statusCode.complete(response.statusCode()))
      .exceptionHandler(statusCode::completeExceptionally)
      .end();
  assertEquals((Integer) 200, statusCode.join());

  List<String> knownServers = Files.readAllLines(knownServersFile);
  assertEquals(3, knownServers.size(), String.join("\n", knownServers));
  assertEquals("#First line", knownServers.get(0));
  assertEquals("localhost:" + foobarServer.actualPort() + " " + DUMMY_FINGERPRINT, knownServers.get(1));
  assertEquals("localhost:" + fooServer.actualPort() + " " + fooFingerprint, knownServers.get(2));

  CompletableFuture<Integer> secondStatusCode = new CompletableFuture<>();
  client
      .post(
          barServer.actualPort(),
          "localhost",
          "/sample",
          response -> secondStatusCode.complete(response.statusCode()))
      .exceptionHandler(secondStatusCode::completeExceptionally)
      .end();
  assertEquals((Integer) 200, secondStatusCode.join());

  knownServers = Files.readAllLines(knownServersFile);
  assertEquals(4, knownServers.size(), String.join("\n", knownServers));
  assertEquals("#First line", knownServers.get(0));
  assertEquals("localhost:" + foobarServer.actualPort() + " " + DUMMY_FINGERPRINT, knownServers.get(1));
  assertEquals("localhost:" + fooServer.actualPort() + " " + fooFingerprint, knownServers.get(2));
  assertEquals("localhost:" + barServer.actualPort() + " " + barFingerprint, knownServers.get(3));
}
 
Example 16
Source File: ToolBox.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
public static List<String> grep(String regExpr, File f)
        throws IOException {
    List<String> lines = Files.readAllLines(f.toPath(), defaultCharset);
    return grep(regExpr, lines);
}
 
Example 17
Source File: TestDockerContainerRuntime.java    From hadoop with Apache License 2.0 4 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testContainerLaunchWithCustomNetworks()
    throws ContainerExecutionException, IOException,
    PrivilegedOperationException {
  DockerLinuxContainerRuntime runtime =
      new DockerLinuxContainerRuntime(mockExecutor, mockCGroupsHandler);

  String customNetwork1 = "sdn1";
  String customNetwork2 = "sdn2";
  String customNetwork3 = "sdn3";

  String[] networks = {"host", "none", "bridge", customNetwork1,
      customNetwork2};

  //customized set of allowed networks
  conf.setStrings(YarnConfiguration.NM_DOCKER_ALLOWED_CONTAINER_NETWORKS,
      networks);
  //default network is "sdn1"
  conf.set(YarnConfiguration.NM_DOCKER_DEFAULT_CONTAINER_NETWORK,
      customNetwork1);

  //this should cause no failures.
  runtime.initialize(conf);
  runtime.launchContainer(builder.build());
  PrivilegedOperation op = capturePrivilegedOperationAndVerifyArgs();
  List<String> args = op.getArguments();
  String dockerCommandFile = args.get(11);

  //This is the expected docker invocation for this case. customNetwork1
  // ("sdn1") is the expected network to be used in this case
  StringBuffer expectedCommandTemplate =
      new StringBuffer("run --name=%1$s ").append("--user=%2$s -d ")
          .append("--workdir=%3$s ")
          .append("--net=" + customNetwork1 + " ")
          .append(getExpectedTestCapabilitiesArgumentString())
          .append("-v %4$s:%4$s ").append("-v %5$s:%5$s ")
          .append("-v %6$s:%6$s ").append("-v %7$s:%7$s ")
          .append("-v %8$s:%8$s ").append("%9$s ")
          .append("bash %10$s/launch_container.sh");

  String expectedCommand = String
      .format(expectedCommandTemplate.toString(), containerId, runAsUser,
          containerWorkDir, containerLocalDirs.get(0), filecacheDirs.get(0),
          containerWorkDir, containerLogDirs.get(0), userLocalDirs.get(0),
          image, containerWorkDir);

  List<String> dockerCommands = Files
      .readAllLines(Paths.get(dockerCommandFile), Charset.forName("UTF-8"));

  Assert.assertEquals(1, dockerCommands.size());
  Assert.assertEquals(expectedCommand, dockerCommands.get(0));


  //now set an explicit (non-default) allowedNetwork and ensure that it is
  // used.

  env.put("YARN_CONTAINER_RUNTIME_DOCKER_CONTAINER_NETWORK",
      customNetwork2);
  runtime.launchContainer(builder.build());

  op = capturePrivilegedOperationAndVerifyArgs();
  args = op.getArguments();
  dockerCommandFile = args.get(11);

  //This is the expected docker invocation for this case. customNetwork2
  // ("sdn2") is the expected network to be used in this case
  expectedCommandTemplate =
      new StringBuffer("run --name=%1$s ").append("--user=%2$s -d ")
          .append("--workdir=%3$s ")
          .append("--net=" + customNetwork2 + " ")
          .append(getExpectedTestCapabilitiesArgumentString())
          .append("-v %4$s:%4$s ").append("-v %5$s:%5$s ")
          .append("-v %6$s:%6$s ").append("-v %7$s:%7$s ")
          .append("-v %8$s:%8$s ").append("%9$s ")
          .append("bash %10$s/launch_container.sh");

  expectedCommand = String
      .format(expectedCommandTemplate.toString(), containerId, runAsUser,
          containerWorkDir, containerLocalDirs.get(0), filecacheDirs.get(0),
          containerWorkDir, containerLogDirs.get(0), userLocalDirs.get(0),
          image, containerWorkDir);
  dockerCommands = Files
      .readAllLines(Paths.get(dockerCommandFile), Charset.forName("UTF-8"));

  Assert.assertEquals(1, dockerCommands.size());
  Assert.assertEquals(expectedCommand, dockerCommands.get(0));

  //disallowed network should trigger a launch failure

  env.put("YARN_CONTAINER_RUNTIME_DOCKER_CONTAINER_NETWORK",
      customNetwork3);
  try {
    runtime.launchContainer(builder.build());
    Assert.fail("Disallowed network : " + customNetwork3
        + "did not trigger launch failure.");
  } catch (ContainerExecutionException e) {
    LOG.info("Caught expected exception : " + e);
  }
}
 
Example 18
Source File: ConfigHandler.java    From OmniOcular with Apache License 2.0 4 votes vote down vote up
public static void mergeConfig() {
        mergedConfig = "";
        File configDir = new File(minecraftConfigDirectory, Reference.MOD_ID);
        File[] configFiles = configDir.listFiles();
        if (configFiles != null) {
            for (File configFile : configFiles) {
                if (configFile.isFile()) {
                    try {
                        List<String> lines = Files.readAllLines(configFile.toPath(), Charset.forName("UTF-8"));
                        for (String line : lines) {
                            mergedConfig += line;
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        mergedConfig = "<root>" + mergedConfig + "</root>";

        final String[][] quoteChars = {
                {"&", "&amp;"},
                {"<", "&lt;"},
                {">", "&gt;"},
//                {"\"", "&quot;"},
//                {"'", "&apos;"}
        };

        StringBuffer quotedBuffer = new StringBuffer();
        Pattern tagSelectorRegex = Pattern.compile("(?<=<(init|line)[^>]*>).*?(?=</\\1>)");
        Matcher tagSelectorMatcher = tagSelectorRegex.matcher(mergedConfig);
        while (tagSelectorMatcher.find()) {
            String quotedString = tagSelectorMatcher.group();
            for (String[] quoteCharPair : quoteChars) {
                quotedString = quotedString.replace(quoteCharPair[0], quoteCharPair[1]);
            }
            tagSelectorMatcher.appendReplacement(quotedBuffer, quotedString);
        }
        tagSelectorMatcher.appendTail(quotedBuffer);
        mergedConfig = quotedBuffer.toString();
    }
 
Example 19
Source File: WalScannerTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void shouldDumpToFileFoundRecord() throws Exception {
    // given: File for dumping records.
    File targetFile = Paths.get(U.defaultWorkDirectory(), TEST_DUMP_FILE).toFile();

    long expectedPageId = 984;
    int grpId = 123;

    WALIterator mockedIter = mockWalIterator(
        new IgniteBiTuple<>(ZERO_POINTER, new PageSnapshot(new FullPageId(expectedPageId, grpId), dummyPage(1024, expectedPageId), 1024)),
        new IgniteBiTuple<>(ZERO_POINTER, new CheckpointRecord(new FileWALPointer(5738, 0, 0))),
        new IgniteBiTuple<>(ZERO_POINTER, new FixCountRecord(grpId, expectedPageId, 4))
    );

    IgniteWalIteratorFactory factory = mock(IgniteWalIteratorFactory.class);
    when(factory.iterator(any(IteratorParametersBuilder.class))).thenReturn(mockedIter);

    Set<T2<Integer, Long>> groupAndPageIds = new HashSet<>();

    groupAndPageIds.add(new T2<>(grpId, expectedPageId));

    List<String> actualRecords;

    try {
        // when: Scanning WAL for searching expected page.
        buildWalScanner(withIteratorParameters(), factory)
            .findAllRecordsFor(groupAndPageIds)
            .forEach(printToFile(targetFile));

        actualRecords = Files.readAllLines(targetFile.toPath());
    }
    finally {
        targetFile.delete();
    }

    // then: Should be find only expected value from file. PageSnapshot string representation is 11 lines long.
    assertEquals(13, actualRecords.size());

    assertTrue(actualRecords.get(0), actualRecords.get(0).contains("PageSnapshot ["));
    assertTrue(actualRecords.get(11), actualRecords.get(11).contains("CheckpointRecord ["));
    assertTrue(actualRecords.get(12), actualRecords.get(12).contains("FixCountRecord ["));
}
 
Example 20
Source File: TerminalClient.java    From philadelphia with Apache License 2.0 4 votes vote down vote up
private static List<String> readAllLines(String filename) throws IOException {
    return Files.readAllLines(new File(filename).toPath(), UTF_8);
}