org.apache.commons.io.FileUtils.readLines()方法的使用及代码示例

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

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

FileUtils.readLines介绍

[英]Reads the contents of a file line by line to a List of Strings using the default encoding for the VM. The file is always closed.
[中]使用VM的默认编码,将文件的内容逐行读取到字符串列表中。文件始终处于关闭状态。

代码示例

代码示例来源:origin: org.apache.commons/commons-io

/**
 * Reads the contents of a file line by line to a List of Strings using the default encoding for the VM.
 * The file is always closed.
 *
 * @param file  the file to read, must not be <code>null</code>
 * @return the list of Strings representing each line in the file, never <code>null</code>
 * @throws IOException in case of an I/O error
 * @since Commons IO 1.3
 */
public static List readLines(File file) throws IOException {
  return readLines(file, null);
}

代码示例来源:origin: commons-io/commons-io

/**
 * Reads the contents of a file line by line to a List of Strings using the default encoding for the VM.
 * The file is always closed.
 *
 * @param file the file to read, must not be {@code null}
 * @return the list of Strings representing each line in the file, never {@code null}
 * @throws IOException in case of an I/O error
 * @since 1.3
 * @deprecated 2.5 use {@link #readLines(File, Charset)} instead (and specify the appropriate encoding)
 */
@Deprecated
public static List<String> readLines(final File file) throws IOException {
  return readLines(file, Charset.defaultCharset());
}

代码示例来源:origin: commons-io/commons-io

/**
 * Reads the contents of a file line by line to a List of Strings. The file is always closed.
 *
 * @param file     the file to read, must not be {@code null}
 * @param encoding the encoding to use, {@code null} means platform default
 * @return the list of Strings representing each line in the file, never {@code null}
 * @throws IOException                 in case of an I/O error
 * @throws java.nio.charset.UnsupportedCharsetException thrown instead of {@link java.io
 * .UnsupportedEncodingException} in version 2.2 if the encoding is not supported.
 * @since 1.1
 */
public static List<String> readLines(final File file, final String encoding) throws IOException {
  return readLines(file, Charsets.toCharset(encoding));
}

代码示例来源:origin: jenkinsci/jenkins

/**
 * @return whether there was a file to load
 */
private boolean load(File dir) {
  File f = new File(dir, MAP_FILE);
  if (!f.isFile()) {
    return false;
  }
  if (f.length() == 0) {
    return true;
  }
  idToNumber = new TreeMap<String,Integer>();
  try {
    for (String line : FileUtils.readLines(f)) {
      int i = line.indexOf(' ');
      idToNumber.put(line.substring(0, i), Integer.parseInt(line.substring(i + 1)));
    }
  } catch (Exception x) { // IOException, IndexOutOfBoundsException, NumberFormatException
    LOGGER.log(WARNING, "could not read from " + f, x);
  }
  return true;
}

代码示例来源:origin: skylot/jadx

List<String> lines = FileUtils.readLines(deobfMapFile, MAP_FILE_CHARSET);
for (String l : lines) {
  l = l.trim();

代码示例来源:origin: jenkinsci/jenkins

FileUtils.readLines(file, StandardCharsets.UTF_8),
whitelistSignaturesFromUserControlledList,
blacklistSignaturesFromUserControlledList

代码示例来源:origin: SonarSource/sonarqube

private String[] readLines(File filename) throws IOException {
 return FileUtils
  .readLines(filename, StandardCharsets.UTF_8)
  .toArray(new String[0]);
}

代码示例来源:origin: docker-java/docker-java

public Iterable<DockerfileStatement> getStatements() throws IOException {
  Collection<String> dockerFileContent = FileUtils.readLines(dockerFile);
  if (dockerFileContent.size() <= 0) {
    throw new DockerClientException(String.format("Dockerfile %s is empty", dockerFile));
  }
  Collection<Optional<? extends DockerfileStatement>> optionals = Collections2.transform(dockerFileContent,
      new LineTransformer());
  return Optional.presentInstances(optionals);
}

代码示例来源:origin: citerus/dddsample-core

private void parse(final File file) throws IOException {
 final List<String> lines = FileUtils.readLines(file);
 final List<String> rejectedLines = new ArrayList<String>();
 for (String line : lines) {
  try {
   parseLine(line);
  } catch (Exception e) {
   logger.error("Rejected line \n" + line + "\nReason is: " + e);
   rejectedLines.add(line);
  }
 }
 if (!rejectedLines.isEmpty()) {
  writeRejectedLinesToFile(toRejectedFilename(file), rejectedLines);
 }
}

代码示例来源:origin: SonarSource/sonarqube

private void processFileMeasures(InputComponent component, File measureFile, SensorContext context) {
 if (measureFile.exists()) {
  LOG.debug("Processing " + measureFile.getAbsolutePath());
  try {
   List<String> lines = FileUtils.readLines(measureFile, context.fileSystem().encoding().name());
   int lineNumber = 0;
   for (String line : lines) {
    lineNumber++;
    if (StringUtils.isBlank(line) || line.startsWith("#")) {
     continue;
    }
    processMeasure(component, context, measureFile, lineNumber, line);
   }
  } catch (IOException e) {
   throw new IllegalStateException(e);
  }
 }
}

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

private void assertMessageInLog(File pluginLogFile, String expectedLoggingLevel, String loggerName, String expectedLogMessage) throws Exception {
  List linesInLog = FileUtils.readLines(pluginLogFile, Charset.defaultCharset());
  for (Object line : linesInLog) {
    if (((String) line).matches(String.format("^.*%s\\s+\\[%s\\] %s:.* - %s$", expectedLoggingLevel, Thread.currentThread().getName(), loggerName, expectedLogMessage))) {
      return;
    }
  }
  fail(String.format("None of the lines matched level:%s message:'%s'. Lines were: %s", expectedLoggingLevel, expectedLogMessage, linesInLog));
}

代码示例来源:origin: SonarSource/sonarqube

private void processFileMeasures(InputFile inputFile, SensorContext context) {
 File ioFile = inputFile.file();
 File measureFile = new File(ioFile.getParentFile(), ioFile.getName() + MEASURES_EXTENSION);
 if (measureFile.exists()) {
  LOG.debug("Processing " + measureFile.getAbsolutePath());
  try {
   FileLinesContext linesContext = contextFactory.createFor(inputFile);
   List<String> lines = FileUtils.readLines(measureFile, context.fileSystem().encoding().name());
   int lineNumber = 0;
   for (String line : lines) {
    lineNumber++;
    if (StringUtils.isBlank(line) || line.startsWith("#")) {
     continue;
    }
    processMeasure(linesContext, measureFile, lineNumber, line);
   }
   linesContext.save();
  } catch (IOException e) {
   throw new IllegalStateException(e);
  }
 }
}

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

private void assertNumberOfMessagesInLog(File pluginLogFile, int size) throws Exception {
  assertThat(FileUtils.readLines(pluginLogFile, Charset.defaultCharset()).size(), is(size));
}

代码示例来源:origin: SonarSource/sonarqube

@Override
public void execute(SensorContext context) {
 File f = new File(context.settings().getString(SONAR_XOO_RANDOM_ACCESS_ISSUE_PATHS));
 FileSystem fs = context.fileSystem();
 FilePredicates p = fs.predicates();
 try {
  for (String path : FileUtils.readLines(f)) {
   createIssues(fs.inputFile(p.and(p.hasPath(path), p.hasType(Type.MAIN), p.hasLanguage(Xoo.KEY))), context);
  }
 } catch (IOException e) {
  throw new IllegalStateException(e);
 }
}

代码示例来源:origin: commons-io/commons-io

@Test
public void testReadLines() throws Exception {
  final File file = TestUtils.newFile(getTestDirectory(), "lines.txt");
  try {
    final String[] data = new String[]{"hello", "/u1234", "", "this is", "some text"};
    TestUtils.createLineBasedFile(file, data);
    final List<String> lines = FileUtils.readLines(file, "UTF-8");
    assertEquals(Arrays.asList(data), lines);
  } finally {
    TestUtils.deleteFile(file);
  }
}

代码示例来源:origin: apache/geode

@Test
 public void createFileFromResource() throws Exception {
  File file =
    ResourceUtils.createFileFromResource(resource, temporaryFolder.getRoot(), resourceName);
  assertThat(file).isNotNull().exists();

  List<String> content = FileUtils.readLines(file, Charset.defaultCharset());
  assertThat(content).contains(RESOURCE_CONTENT);
 }
}

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

@Test
public final void testXmlOutput() throws IOException {
  final CheckstyleAntTask antTask = getCheckstyleAntTask();
  antTask.setFile(new File(getPath(VIOLATED_INPUT)));
  antTask.setFailOnViolation(false);
  final CheckstyleAntTask.Formatter formatter = new CheckstyleAntTask.Formatter();
  final File outputFile = new File("target/log.xml");
  formatter.setTofile(outputFile);
  final CheckstyleAntTask.FormatterType formatterType = new CheckstyleAntTask.FormatterType();
  formatterType.setValue("xml");
  formatter.setType(formatterType);
  antTask.addFormatter(formatter);
  antTask.execute();
  final List<String> expected = FileUtils.readLines(
      new File(getPath("ExpectedCheckstyleAntTaskXmlOutput.xml")),
          StandardCharsets.UTF_8);
  final List<String> actual = FileUtils.readLines(outputFile, StandardCharsets.UTF_8);
  for (int i = 0; i < expected.size(); i++) {
    final String line = expected.get(i);
    if (!line.startsWith("<checkstyle version") && !line.startsWith("<file")) {
      assertEquals("Content of file with violations differs from expected",
          line, actual.get(i));
    }
  }
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void shouldWriteIndex() throws IOException {
 InstalledPlugin javaPlugin = newInstalledPlugin("java", true);
 InstalledPlugin gitPlugin = newInstalledPlugin("scmgit", false);
 when(pluginFileSystem.getInstalledFiles()).thenReturn(asList(javaPlugin, gitPlugin));
 GeneratePluginIndex underTest = new GeneratePluginIndex(serverFileSystem, pluginFileSystem);
 underTest.start();
 List<String> lines = FileUtils.readLines(index);
 assertThat(lines).containsExactly(
  "java,true," + javaPlugin.getLoadedJar().getFile().getName() + "|" + javaPlugin.getLoadedJar().getMd5(),
  "scmgit,false," + gitPlugin.getLoadedJar().getFile().getName() + "|" + gitPlugin.getLoadedJar().getMd5());
 underTest.stop();
}

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

@Test
public final void testConfigurationByUrl() throws IOException {
  final CheckstyleAntTask antTask = new CheckstyleAntTask();
  antTask.setProject(new Project());
  final URL url = new File(getPath(CONFIG_FILE)).toURI().toURL();
  antTask.setConfig(url.toString());
  antTask.setFile(new File(getPath(FLAWLESS_INPUT)));
  final CheckstyleAntTask.Formatter formatter = new CheckstyleAntTask.Formatter();
  final File outputFile = new File("target/ant_task_config_by_url.txt");
  formatter.setTofile(outputFile);
  final CheckstyleAntTask.FormatterType formatterType = new CheckstyleAntTask.FormatterType();
  formatterType.setValue("plain");
  formatter.setType(formatterType);
  formatter.createListener(null);
  antTask.addFormatter(formatter);
  antTask.execute();
  final List<String> output = FileUtils.readLines(outputFile, StandardCharsets.UTF_8);
  final int sizeOfOutputWithNoViolations = 2;
  assertEquals("No violations expected", sizeOfOutputWithNoViolations, output.size());
}

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

@Test
public final void testConfigurationByResource() throws IOException {
  final CheckstyleAntTask antTask = new CheckstyleAntTask();
  antTask.setProject(new Project());
  antTask.setConfig(getPath(CONFIG_FILE));
  antTask.setFile(new File(getPath(FLAWLESS_INPUT)));
  final CheckstyleAntTask.Formatter formatter = new CheckstyleAntTask.Formatter();
  final File outputFile = new File("target/ant_task_config_by_url.txt");
  formatter.setTofile(outputFile);
  final CheckstyleAntTask.FormatterType formatterType = new CheckstyleAntTask.FormatterType();
  formatterType.setValue("plain");
  formatter.setType(formatterType);
  formatter.createListener(null);
  antTask.addFormatter(formatter);
  antTask.execute();
  final List<String> output = FileUtils.readLines(outputFile, StandardCharsets.UTF_8);
  final int sizeOfOutputWithNoViolations = 2;
  assertEquals("No violations expected", sizeOfOutputWithNoViolations, output.size());
}

相关文章

FileUtils类方法