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

x33g5p2x  于2022-01-20 转载在 其他  
字(10.8k)|赞(0)|评价(0)|浏览(889)

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

IOUtils.readLines介绍

[英]Get the contents of an InputStream as a list of Strings, one entry per line, using the default character encoding of the platform.

This method buffers the input internally, so there is no need to use a BufferedInputStream.
[中]使用平台的默认字符编码,以字符串列表的形式获取InputStream的内容,每行一个条目。
此方法在内部缓冲输入,因此无需使用BufferedInputStream

代码示例

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

/**
 * @deprecated Use instead {@link org.apache.commons.io.IOUtils#readLines(java.io.Reader)}
 */
@Deprecated
public static List readLines(Reader input) throws IOException {
  return org.apache.commons.io.IOUtils.readLines(input);
}

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

/**
 * @deprecated Use instead {@link org.apache.commons.io.IOUtils#readLines(java.io.InputStream)}
 */
@Deprecated
public static List readLines(InputStream input) throws IOException {
  return org.apache.commons.io.IOUtils.readLines(input);
}

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

/**
 * @deprecated Use instead {@link org.apache.commons.io.IOUtils#readLines(java.io.InputStream, String)}
 */
@Deprecated
public static List readLines(InputStream input, String encoding) throws IOException {
  return org.apache.commons.io.IOUtils.readLines(input, encoding);
}

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

private static Stream<String> configLines(InputStream is) throws IOException {
  return org.apache.commons.io.IOUtils.readLines(is, StandardCharsets.UTF_8).stream().filter(line -> !line.matches("#.*|\\s*"));
}

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

/**
 * Gets the contents of an <code>InputStream</code> as a list of Strings,
 * one entry per line, using the default character encoding of the platform.
 * <p>
 * This method buffers the input internally, so there is no need to use a
 * <code>BufferedInputStream</code>.
 *
 * @param input the <code>InputStream</code> to read from, not null
 * @return the list of Strings, never null
 * @throws NullPointerException if the input is null
 * @throws IOException          if an I/O error occurs
 * @since 1.1
 * @deprecated 2.5 use {@link #readLines(InputStream, Charset)} instead
 */
@Deprecated
public static List<String> readLines(final InputStream input) throws IOException {
  return readLines(input, Charset.defaultCharset());
}

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

private synchronized void reloadFromDefault() {
  try (InputStream is = StaticRoutingDecisionProvider.class.getResourceAsStream("default-whitelist.txt")) {
    whitelistSignaturesFromFixedList = new HashSet<>();
    blacklistSignaturesFromFixedList = new HashSet<>();

    parseFileIntoList(
        IOUtils.readLines(is, StandardCharsets.UTF_8),
        whitelistSignaturesFromFixedList,
        blacklistSignaturesFromFixedList
    );
  } catch (IOException e) {
    throw new ExceptionInInitializerError(e);
  }
  
  LOGGER.log(Level.FINE, "Found {0} getter in the standard whitelist", whitelistSignaturesFromFixedList.size());
}

代码示例来源: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
 * @since 2.3
 */
public static List<String> readLines(final File file, final Charset encoding) throws IOException {
  try (InputStream in = openInputStream(file)) {
    return IOUtils.readLines(in, Charsets.toCharset(encoding));
  }
}

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

/**
 * Gets the contents of an <code>InputStream</code> as a list of Strings,
 * one entry per line, using the specified character encoding.
 * <p>
 * This method buffers the input internally, so there is no need to use a
 * <code>BufferedInputStream</code>.
 *
 * @param input the <code>InputStream</code> to read from, not null
 * @param encoding the encoding to use, null means platform default
 * @return the list of Strings, never null
 * @throws NullPointerException if the input is null
 * @throws IOException          if an I/O error occurs
 * @since 2.3
 */
public static List<String> readLines(final InputStream input, final Charset encoding) throws IOException {
  final InputStreamReader reader = new InputStreamReader(input, Charsets.toCharset(encoding));
  return readLines(reader);
}

代码示例来源:origin: alibaba/jstorm

public Object getResource() {
    try {
      List<String> lines = IOUtils.readLines(new FileInputStream(file));
      return callback.execute(lines);
    } catch (Exception e) {
      LOG.warn("Error reading the stream ", e);
    }
    return null;
  }
}

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

/**
 * Gets the contents of an <code>InputStream</code> as a list of Strings,
 * one entry per line, using the specified character encoding.
 * <p>
 * Character encoding names can be found at
 * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
 * <p>
 * This method buffers the input internally, so there is no need to use a
 * <code>BufferedInputStream</code>.
 *
 * @param input the <code>InputStream</code> to read from, not null
 * @param encoding the encoding to use, null means platform default
 * @return the list of Strings, never null
 * @throws NullPointerException                         if the input is null
 * @throws IOException                                  if an I/O error occurs
 * @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 InputStream input, final String encoding) throws IOException {
  return readLines(input, Charsets.toCharset(encoding));
}

代码示例来源:origin: alibaba/jstorm

public static Long getFreePhysicalMem() {
  if (!OSInfo.isLinux()) {
    return 0L;
  }
  try {
    List<String> lines = IOUtils.readLines(new FileInputStream(PROCFS_MEMINFO));
    String free = lines.get(1).split("\\s+")[1];
    return Long.valueOf(free);
  } catch (Exception ignored) {
    LOG.warn("failed to get total free memory.");
  }
  return 0L;
}

代码示例来源:origin: Alluxio/alluxio

private static boolean processExists(String processName) throws Exception {
 Process ps = Runtime.getRuntime().exec(new String[] {"ps", "ax"});
 InputStream psOutput = ps.getInputStream();
 Process processGrep = Runtime.getRuntime().exec(new String[] {"grep", processName});
 OutputStream processGrepInput = processGrep.getOutputStream();
 IOUtils.copy(psOutput, processGrepInput);
 InputStream processGrepOutput = processGrep.getInputStream();
 processGrepInput.close();
 // Filter out the grep process itself.
 Process filterGrep = Runtime.getRuntime().exec(new String[] {"grep", "-v", "grep"});
 OutputStream filterGrepInput = filterGrep.getOutputStream();
 IOUtils.copy(processGrepOutput, filterGrepInput);
 filterGrepInput.close();
 return IOUtils.readLines(filterGrep.getInputStream()).size() >= 1;
}

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

private void reapFiles() {
  try (FileReader tmpFileReader = new FileReader(LAUNCHER_TMP_FILE_LIST)) {
    List<String> fileList = IOUtils.readLines(tmpFileReader);
    Set<String> fileSet = new HashSet<>(fileList);
    for (String fileName : fileSet) {
      File file = new File(fileName);
      FileUtils.deleteQuietly(file);
      File depsDir = new File(FileUtil.TMP_PARENT_DIR, fileName);
      FileUtils.deleteQuietly(depsDir);
      if (!file.exists() && !depsDir.exists()) {
        fileList.remove(fileName);
      }
    }
    writeToFile(fileList, false);
  } catch (Exception ignore) {
  }
}

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

@Override
public Map<String, Properties> getAllConfigsMap() {
  try {
    String rawConfigContent = Joiner.on("\n")
                    .join(IOUtils.readLines(new FileReader(getConfigFile())))
                    .trim();
    return ClientConfigUtil.readMultipleClientConfigAvro(rawConfigContent);
  } catch(IOException e) {
    logger.error(PROBLEM_READING_CONFIG_FILE, e);
    throw new RuntimeException(PROBLEM_READING_CONFIG_FILE, e);
  }
}

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

@Initializer(after = InitMilestone.PLUGINS_PREPARED, before = InitMilestone.PLUGINS_STARTED, fatal = false)
public static void load() throws IOException {
  Map<String, Boolean> overrides = ExtensionList.lookup(CustomClassFilter.class).get(Contributed.class).overrides;
  overrides.clear();
  Enumeration<URL> resources = Jenkins.getInstance().getPluginManager().uberClassLoader.getResources("META-INF/hudson.remoting.ClassFilter");
  while (resources.hasMoreElements()) {
    try (InputStream is = resources.nextElement().openStream()) {
      for (String entry : IOUtils.readLines(is, StandardCharsets.UTF_8)) {
        if (entry.matches("#.*|\\s*")) {
          // skip
        } else if (entry.startsWith("!")) {
          overrides.put(entry.substring(1), false);
        } else {
          overrides.put(entry, true);
        }
      }
    }
  }
  Logger.getLogger(Contributed.class.getName()).log(Level.FINE, "plugin-defined entries: {0}", overrides);
}

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

private String readFile(Path file, Charset defaultEncoding) throws IOException {
 CharsetDetector detector = new CharsetDetector(file, defaultEncoding);
 assertThat(detector.run()).isTrue();
 List<String> readLines = IOUtils.readLines(new InputStreamReader(detector.inputStream(), detector.charset()));
 return StringUtils.join(readLines, "\n");
}

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

@Test public void testReadLines_Reader() throws Exception {
  final File file = TestUtils.newFile(getTestDirectory(), "lines.txt");
  Reader in = null;
  try {
    final String[] data = new String[] { "hello", "/u1234", "", "this is", "some text" };
    TestUtils.createLineBasedFile(file, data);
    in = new InputStreamReader(new FileInputStream(file));
    final List<String> lines = IOUtils.readLines(in);
    assertEquals(Arrays.asList(data), lines);
    assertEquals(-1, in.read());
  } finally {
    IOUtils.closeQuietly(in);
    TestUtils.deleteFile(file);
  }
}

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

@Test
public void read_file_source() throws Exception {
 ScannerReportWriter writer = new ScannerReportWriter(dir);
 try (FileOutputStream outputStream = new FileOutputStream(writer.getSourceFile(1))) {
  IOUtils.write("line1\nline2", outputStream);
 }
 try (InputStream inputStream = FileUtils.openInputStream(underTest.readFileSource(1))) {
  assertThat(IOUtils.readLines(inputStream)).containsOnly("line1", "line2");
 }
}

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

@Test public void testReadLines_InputStream() throws Exception {
  final File file = TestUtils.newFile(getTestDirectory(), "lines.txt");
  InputStream in = null;
  try {
    final String[] data = new String[] { "hello", "world", "", "this is", "some text" };
    TestUtils.createLineBasedFile(file, data);
    in = new FileInputStream(file);
    final List<String> lines = IOUtils.readLines(in);
    assertEquals(Arrays.asList(data), lines);
    assertEquals(-1, in.read());
  } finally {
    IOUtils.closeQuietly(in);
    TestUtils.deleteFile(file);
  }
}

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

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

相关文章