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

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

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

FileUtils.openInputStream介绍

[英]Opens a FileInputStream for the specified file, providing better error messages than simply calling new FileInputStream(file).

At the end of the method either the stream will be successfully opened, or an exception will have been thrown.

An exception is thrown if the file does not exist. An exception is thrown if the file object exists but is a directory. An exception is thrown if the file exists but cannot be read.
[中]打开指定文件的FileInputStream,提供比简单调用new FileInputStream(file)更好的错误消息。
在方法结束时,要么成功打开流,要么抛出异常。
如果文件不存在,将引发异常。如果文件对象存在但为目录,则引发异常。如果文件存在但无法读取,将引发异常。

代码示例

代码示例来源:origin: apache/incubator-druid

@Override
protected InputStream openObjectStream(File object) throws IOException
{
 return FileUtils.openInputStream(object);
}

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

public byte[] readFileToByteArray(File file) throws IOException {
    InputStream in = org.apache.commons.io.FileUtils.openInputStream(file);
    try {
      return org.apache.commons.io.IOUtils.toByteArray(in);
    } finally {
        in.close();
    }
  }
}

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

public ReportIterator(File file, Parser<E> parser) {
 try {
  this.parser = parser;
  this.stream = FileUtils.openInputStream(file);
 } catch (IOException e) {
  throw Throwables.propagate(e);
 }
}

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

/**
 * Reads the contents of a file into a byte array.
 * The file is always closed.
 *
 * @param file the file to read, must not be {@code null}
 * @return the file contents, never {@code null}
 * @throws IOException in case of an I/O error
 * @since 1.1
 */
public static byte[] readFileToByteArray(final File file) throws IOException {
  try (InputStream in = openInputStream(file)) {
    final long fileLength = file.length();
    // file.length() may return 0 for system-dependent entities, treat 0 as unknown length - see IO-453
    return fileLength > 0 ? IOUtils.toByteArray(in, fileLength) : IOUtils.toByteArray(in);
  }
}

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

public FileAndMd5(File file) {
 try (InputStream fis = FileUtils.openInputStream(file)) {
  this.file = file;
  this.md5 = DigestUtils.md5Hex(fis);
 } catch (IOException e) {
  throw new IllegalStateException("Fail to compute md5 of " + file, e);
 }
}

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

private static String computeMd5(File file) {
 try (InputStream fis = new BufferedInputStream(FileUtils.openInputStream(file))) {
  return DigestUtils.md5Hex(fis);
 } catch (IOException e) {
  throw new IllegalStateException("Fail to compute md5 of " + file, e);
 }
}

代码示例来源: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

/**
 * Reads the contents of a file into a String.
 * 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 file contents, never {@code null}
 * @throws IOException in case of an I/O error
 * @since 2.3
 */
public static String readFileToString(final File file, final Charset encoding) throws IOException {
  try (InputStream in = openInputStream(file)) {
    return IOUtils.toString(in, Charsets.toCharset(encoding));
  }
}

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

private void restore(DbSession dbSession, File backupFile, QProfileDto profile) {
  try (Reader reader = new InputStreamReader(FileUtils.openInputStream(backupFile), UTF_8)) {
   backuper.restore(dbSession, reader, profile);
  } catch (IOException e) {
   throw new IllegalStateException("Fail to create temporary backup file: " + backupFile, e);
  }
 }
}

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

public CloseableIterator<O> traverse() {
 try {
  return new ObjectInputStreamIterator<>(FileUtils.openInputStream(file));
 } catch (IOException e) {
  throw new IllegalStateException("Fail to traverse file: " + file, e);
 }
}

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

public static void configure(File logbackFile, Map<String, String> substitutionVariables) {
 try {
  FileInputStream input = FileUtils.openInputStream(logbackFile);
  configure(input, substitutionVariables);
 } catch (IOException e) {
  throw new IllegalArgumentException("Fail to load the Logback configuration: " + logbackFile, e);
 }
}

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

public List<Rule> parse(File file) {
 try (Reader reader = new InputStreamReader(FileUtils.openInputStream(file), UTF_8)) {
  return parse(reader);
 } catch (IOException e) {
  throw new SonarException("Fail to load the file: " + file, e);
 }
}

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

private File unpack200(String pluginKey, File compressedFile) {
 LOGGER.debug("Unpacking plugin {}", pluginKey);
 File jar = newTempFile();
 try (InputStream input = new GZIPInputStream(new BufferedInputStream(FileUtils.openInputStream(compressedFile)));
    JarOutputStream output = new JarOutputStream(new BufferedOutputStream(FileUtils.openOutputStream(jar)))) {
  Pack200.newUnpacker().unpack(input, output);
 } catch (IOException e) {
  throw new IllegalStateException(format("Fail to download plugin [%s]. Pack200 error.", pluginKey), e);
 }
 return jar;
}

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

@Override
public Optional<CloseableIterator<String>> readFileSource(int fileRef) {
 ensureInitialized();
 File file = delegate.readFileSource(fileRef);
 if (file == null) {
  return Optional.empty();
 }
 try {
  return Optional.of(new CloseableLineIterator(IOUtils.lineIterator(FileUtils.openInputStream(file), UTF_8)));
 } catch (IOException e) {
  throw new IllegalStateException("Fail to traverse file: " + file, e);
 }
}

代码示例来源:origin: apache/incubator-druid

OpenedObject(FetchedFile<T> fetchedFile) throws IOException
{
 this(fetchedFile.getObject(), FileUtils.openInputStream(fetchedFile.getFile()), fetchedFile.getResourceCloser());
}

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

@Test
public void test_openInputStream_existsButIsDirectory() throws Exception {
  final File directory = new File(getTestDirectory(), "subdir");
  directory.mkdirs();
  try (FileInputStream in = FileUtils.openInputStream(directory)) {
    fail();
  } catch (final IOException ioe) {
    // expected
  }
}

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

FileAndMd5() throws IOException {
 this.file = temp.newFile();
 FileUtils.write(this.file, RandomStringUtils.random(3));
 try (InputStream fis = FileUtils.openInputStream(this.file)) {
  this.md5 = DigestUtils.md5Hex(fis);
 } catch (IOException e) {
  throw new IllegalStateException("Fail to compute md5 of " + this.file, e);
 }
}

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

@Test
public void test_openInputStream_notExists() throws Exception {
  final File directory = new File(getTestDirectory(), "test.txt");
  try (FileInputStream in = FileUtils.openInputStream(directory)) {
    fail();
  } catch (final IOException ioe) {
    // expected
  }
}

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

@Test
public void test_openInputStream_exists() throws Exception {
  final File file = new File(getTestDirectory(), "test.txt");
  TestUtils.createLineBasedFile(file, new String[]{"Hello"});
  try (FileInputStream in = FileUtils.openInputStream(file)) {
    assertEquals('H', in.read());
  }
}

代码示例来源: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");
 }
}

相关文章

微信公众号

最新文章

更多

FileUtils类方法