com.google.common.io.Files.readFirstLine()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(9.2k)|赞(0)|评价(0)|浏览(146)

本文整理了Java中com.google.common.io.Files.readFirstLine()方法的一些代码示例,展示了Files.readFirstLine()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Files.readFirstLine()方法的具体详情如下:
包路径:com.google.common.io.Files
类名称:Files
方法名:readFirstLine

Files.readFirstLine介绍

[英]Reads the first line from a file. The line does not include line-termination characters, but does include other leading and trailing whitespace.
[中]从文件中读取第一行。该行不包含行终止字符,但包含其他前导和尾随空格。

代码示例

代码示例来源:origin: azkaban/azkaban

private String getPid() {
 final File libDir = PACKAGE_JAR.getParentFile();
 final File installDir = libDir.getParentFile();
 final File pidFile = new File(installDir, this.pidFilename);
 try {
  return Files.readFirstLine(pidFile, StandardCharsets.UTF_8).trim();
 } catch (final IOException e) {
  log.error("Unable to obtain PID", e);
  return "unknown";
 }
}

代码示例来源:origin: spotify/dockerfile-maven

@Nullable
protected String readMetadata(@Nonnull Metadata metadata) throws MojoExecutionException {
 final File metadataFile = ensureMetadataFile(metadata);
 if (!metadataFile.exists()) {
  return null;
 }
 try {
  return Files.readFirstLine(metadataFile, Charsets.UTF_8);
 } catch (IOException e) {
  final String message =
    MessageFormat.format("Could not read {0} file at {1}", metadata.getFileName(),
               metadataFile);
  throw new MojoExecutionException(message, e);
 }
}

代码示例来源:origin: google/guava

public void testLineReading() throws IOException {
 File temp = createTempFile();
 assertNull(Files.readFirstLine(temp, Charsets.UTF_8));
 assertTrue(Files.readLines(temp, Charsets.UTF_8).isEmpty());
 PrintWriter w = new PrintWriter(Files.newWriter(temp, Charsets.UTF_8));
 w.println("hello");
 w.println("");
 w.println(" world  ");
 w.println("");
 w.close();
 assertEquals("hello", Files.readFirstLine(temp, Charsets.UTF_8));
 assertEquals(
   ImmutableList.of("hello", "", " world  ", ""), Files.readLines(temp, Charsets.UTF_8));
 assertTrue(temp.delete());
}

代码示例来源:origin: springside/springside4

@Test
public void testCopy() throws Exception {
  Path dir = FileUtil.createTempDir();
  assertThat(dir).exists();
  String srcFileName = "src";
  File srcFile = dir.resolve(srcFileName).toFile();
  FileUtil.touch(srcFile);
  assertThat(srcFile).exists();
  FileUtil.write("test", srcFile);
  String destFileName = "dest";
  File destFile = new File(dir.toFile(), "parent1/parent2/" + destFileName);
  FileUtil.makesureParentDirExists(destFile);
  FileUtil.copy(srcFile, destFile);
  assertThat(Files.readFirstLine(destFile, Charsets.UTF_8)).isEqualTo("test");
}

代码示例来源:origin: google/google-api-java-client-samples

System.exit(1);
String p12Content = Files.readFirstLine(new File("key.p12"), Charset.defaultCharset());
if (p12Content.startsWith("Please")) {
 System.err.println(p12Content);

代码示例来源:origin: KylinOLAP/Kylin

protected void verifyResultRowCount(String queryFolder) throws Exception {
  printInfo("---------- verify result count in folder: " + queryFolder);
  List<File> sqlFiles = getFilesFromFolder(new File(queryFolder), ".sql");
  for (File sqlFile : sqlFiles) {
    String queryName = StringUtils.split(sqlFile.getName(), '.')[0];
    String sql = getTextFromFile(sqlFile);
    File expectResultFile = new File(sqlFile.getParent(), sqlFile.getName() + ".expected");
    int expectRowCount = Integer.parseInt(Files.readFirstLine(expectResultFile, Charset.defaultCharset()));
    // execute Kylin
    printInfo("Query Result from Kylin - " + queryName + "  (" + queryFolder + ")");
    IDatabaseConnection kylinConn = new DatabaseConnection(cubeConnection);
    ITable kylinTable = executeQuery(kylinConn, queryName, sql, false);
    // compare the result
    Assert.assertEquals(expectRowCount, kylinTable.getRowCount());
    // Assertion.assertEquals(expectRowCount, kylinTable.getRowCount());
  }
}

代码示例来源:origin: org.geogit/geogit-core

private String readRef(File refFile) {
  try {
    synchronized (refFile.getCanonicalPath().intern()) {
      return Files.readFirstLine(refFile, CHARSET);
    }
  } catch (IOException e) {
    throw Throwables.propagate(e);
  }
}

代码示例来源:origin: com.google.cloud/google-cloud-core

private static String getActiveGoogleCloudConfig(File configDir) {
 String activeGoogleCloudConfig = null;
 try {
  activeGoogleCloudConfig =
    Files.readFirstLine(new File(configDir, "active_config"), Charset.defaultCharset());
 } catch (IOException ex) {
  // ignore
 }
 // if reading active_config failed or the file is empty we try default
 return firstNonNull(activeGoogleCloudConfig, "default");
}

代码示例来源:origin: com.google.cloud/gcloud-java-core

private static String activeGoogleCloudConfig(File configDir) {
 String activeGoogleCloudConfig = null;
 try {
  activeGoogleCloudConfig =
    Files.readFirstLine(new File(configDir, "active_config"), Charset.defaultCharset());
 } catch (IOException ex) {
  // ignore
 }
 // if reading active_config failed or the file is empty we try default
 return firstNonNull(activeGoogleCloudConfig, "default");
}

代码示例来源:origin: org.locationtech.geogig/geogig-core

private String readRef(final File refFile) {
  try {
    // make sure no other thread changes the ref as we read it
    synchronized (refFile.getCanonicalPath().intern()) {
      return Files.readFirstLine(refFile, CHARSET);
    }
  } catch (IOException e) {
    throw Throwables.propagate(e);
  }
}

代码示例来源:origin: Noahs-ARK/semafor

/** Reads the number of features in the model from the first line of alphabetFile */
private static int getNumFeatures(String alphabetFile) throws IOException {
  final String firstLine = Files.readFirstLine(new File(alphabetFile), Charsets.UTF_8);
  final int numFeatures = Integer.parseInt(firstLine.trim()) + 1;
  logger.info("Number of features: " + numFeatures);
  return numFeatures;
}

代码示例来源:origin: org.deephacks/confit-tck

private Class<?> readSuiteContext() {
  try {
    String className = Files.readFirstLine(lastRunFeatureFile, Charsets.UTF_8);
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    if (className != null && !"".equals(className.trim())) {
      return cl.loadClass(className);
    }
  } catch (Exception e) {
    return null;
  }
  return null;
}

代码示例来源:origin: org.deephacks.tools4j/tools4j-config-tck

private Class<?> readSuiteContext() {
  try {
    String className = Files.readFirstLine(lastRunFeatureFile, Charsets.UTF_8);
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    if (className != null && !"".equals(className.trim())) {
      return cl.loadClass(className);
    }
  } catch (Exception e) {
    return null;
  }
  return null;
}

代码示例来源:origin: org.geogit/geogit-core

private void skipCurrentCommit() {
  File rebaseFolder = getRebaseFolder();
  File nextFile = new File(rebaseFolder, "next");
  try {
    String idx = Files.readFirstLine(nextFile, Charsets.UTF_8);
    File commitFile = new File(rebaseFolder, idx);
    commitFile.delete();
    int newIdx = Integer.parseInt(idx) + 1;
    Files.write(Integer.toString(newIdx), nextFile, Charsets.UTF_8);
  } catch (IOException e) {
    throw new IllegalStateException("Cannot read/write rebase commits index file");
  }
}

代码示例来源:origin: com.android.tools.lint/lint-checks

private static boolean isPrivateKeyFile(File file) {
  if (!file.isFile() ||
    (!LintUtils.endsWith(file.getPath(), "pem") &&
     !LintUtils.endsWith(file.getPath(), "key"))) {
    return false;
  }
  try {
    String firstLine = Files.readFirstLine(file, Charsets.US_ASCII);
    return firstLine != null &&
      firstLine.startsWith("---") &&
      firstLine.contains("PRIVATE KEY");
  } catch (IOException ex) {
    // Don't care
  }
  return false;
}

代码示例来源:origin: Netflix/iceberg

int readVersionHint() throws IOException {
 return Integer.parseInt(Files.readFirstLine(versionHintFile, Charsets.UTF_8));
}

代码示例来源:origin: org.apache.brooklyn/brooklyn-core

@Test(groups = "Integration")
public void testCopyFileTo() throws Exception {
  File dest = Os.newTempFile(getClass(), ".dest.tmp");
  File src = Os.newTempFile(getClass(), ".src.tmp");
  try {
    Files.write("abc", src, Charsets.UTF_8);
    host.copyTo(src, dest);
    assertEquals("abc", Files.readFirstLine(dest, Charsets.UTF_8));
  } finally {
    src.delete();
    dest.delete();
  }
}

代码示例来源:origin: org.apache.brooklyn/brooklyn-core

@Test(groups = "Integration")
public void testCopyStreamTo() throws Exception {
  String contents = "abc";
  File dest = new File(Os.tmp(), "sshMachineLocationTest_dest.tmp");
  try {
    host.copyTo(Streams.newInputStreamWithContents(contents), dest.getAbsolutePath());
    assertEquals("abc", Files.readFirstLine(dest, Charsets.UTF_8));
  } finally {
    dest.delete();
  }
}

代码示例来源:origin: org.apache.brooklyn/brooklyn-software-monitoring

@Override
  public void run() {
    String targetStatus = monitNode.getAttribute(MonitNode.MONIT_TARGET_PROCESS_STATUS);
    LOG.debug("MonitNode target status: {}", targetStatus);
    assertEquals(targetStatus, "Running");
    try {
      initialPid[0] = Files.readFirstLine(new File(mySqlNode.getAttribute(SoftwareProcess.PID_FILE)), Charset.defaultCharset());
      LOG.debug("Initial PID: {}", initialPid[0]);
    } catch (IOException e) {
      Asserts.fail("Could not read PID file: " + e);
    }
  }
});

代码示例来源:origin: com.google.guava/guava-tests

public void testLineReading() throws IOException {
 File temp = createTempFile();
 assertNull(Files.readFirstLine(temp, Charsets.UTF_8));
 assertTrue(Files.readLines(temp, Charsets.UTF_8).isEmpty());
 PrintWriter w = new PrintWriter(Files.newWriter(temp, Charsets.UTF_8));
 w.println("hello");
 w.println("");
 w.println(" world  ");
 w.println("");
 w.close();
 assertEquals("hello", Files.readFirstLine(temp, Charsets.UTF_8));
 assertEquals(ImmutableList.of("hello", "", " world  ", ""),
   Files.readLines(temp, Charsets.UTF_8));
 assertTrue(temp.delete());
}

相关文章