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

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

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

FileUtils.sizeOf介绍

[英]Returns the size of the specified file or directory. If the provided File is a regular file, then the file's length is returned. If the argument is a directory, then the size of the directory is calculated recursively. If a directory or subdirectory is security restricted, its size will not be included.
[中]返回指定文件或目录的大小。如果提供的文件是常规文件,则返回文件的长度。如果参数是目录,则递归计算目录的大小。如果目录或子目录受到安全限制,则不包括其大小。

代码示例

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

@Override
public long length(URI fileUri)
  throws IOException {
 File file = new File(decodeURI(fileUri.getRawPath()));
 if (file.isDirectory()) {
  throw new IllegalArgumentException("File is directory");
 }
 return FileUtils.sizeOf(file);
}

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

@Test
public void testCompareSizeOf() {
  final File start = new File("src/test/java");
  final long sizeLong1 = FileUtils.sizeOf(start);
  final BigInteger sizeBig = FileUtils.sizeOfAsBigInteger(start);
  final long sizeLong2 = FileUtils.sizeOf(start);
  assertEquals("Size should not change", sizeLong1, sizeLong2);
  assertEquals("longSize should equal BigSize", sizeLong1, sizeBig.longValue());
}

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

private File generateReportFile() {
 try {
  long startTime = System.currentTimeMillis();
  for (ReportPublisherStep publisher : publishers) {
   publisher.publish(writer);
  }
  long stopTime = System.currentTimeMillis();
  LOG.info("Analysis report generated in {}ms, dir size={}", stopTime - startTime, FileUtils.byteCountToDisplaySize(FileUtils.sizeOfDirectory(reportDir.toFile())));
  startTime = System.currentTimeMillis();
  File reportZip = temp.newFile("scanner-report", ".zip");
  ZipUtils.zipDir(reportDir.toFile(), reportZip);
  stopTime = System.currentTimeMillis();
  LOG.info("Analysis report compressed in {}ms, zip size={}", stopTime - startTime, FileUtils.byteCountToDisplaySize(FileUtils.sizeOf(reportZip)));
  return reportZip;
 } catch (IOException e) {
  throw new IllegalStateException("Unable to prepare analysis report", e);
 }
}

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

long sizeInBytes = FileUtils.sizeOf(dir);
LOG.info("Cleaning up: Removing {}, {} KB", path, sizeInBytes * 1e-3);

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

/**
 * Constuctor.
 *
 * @param stormConf configuration map for Storm cluster
 * @param workerLogs {@link WorkerLogs} instance
 * @param directoryCleaner {@link DirectoryCleaner} instance
 * @param logRootDir root log directory
 * @param metricsRegistry The logviewer metrics registry
 */
public LogCleaner(Map<String, Object> stormConf, WorkerLogs workerLogs, DirectoryCleaner directoryCleaner,
         File logRootDir, StormMetricsRegistry metricsRegistry) {
  this.stormConf = stormConf;
  this.intervalSecs = ObjectReader.getInt(stormConf.get(LOGVIEWER_CLEANUP_INTERVAL_SECS), null);
  this.logRootDir = logRootDir;
  this.workerLogs = workerLogs;
  this.directoryCleaner = directoryCleaner;
  maxSumWorkerLogsSizeMb = ObjectReader.getInt(stormConf.get(LOGVIEWER_MAX_SUM_WORKER_LOGS_SIZE_MB));
  maxPerWorkerLogsSizeMb = ObjectReader.getInt(stormConf.get(LOGVIEWER_MAX_PER_WORKER_LOGS_SIZE_MB));
  maxPerWorkerLogsSizeMb = Math.min(maxPerWorkerLogsSizeMb, (long) (maxSumWorkerLogsSizeMb * 0.5));
  LOG.info("configured max total size of worker logs: {} MB, max total size of worker logs per directory: {} MB",
    maxSumWorkerLogsSizeMb, maxPerWorkerLogsSizeMb);
  //Switch to CachedGauge if this starts to hurt performance
  metricsRegistry.registerGauge("logviewer:worker-log-dir-size", () -> FileUtils.sizeOf(logRootDir));
  this.cleanupRoutineDuration = metricsRegistry.registerTimer("logviewer:cleanup-routine-duration-ms");
  this.numFilesCleanedUp = metricsRegistry.registerHistogram("logviewer:num-files-cleaned-up");
  this.diskSpaceFreed = metricsRegistry.registerHistogram("logviewer:disk-space-freed-in-bytes");
  this.numFileRemovalExceptions = metricsRegistry.registerMeter(ExceptionMeterNames.NUM_FILE_REMOVAL_EXCEPTIONS);
  this.numCleanupExceptions = metricsRegistry.registerMeter(ExceptionMeterNames.NUM_CLEANUP_EXCEPTIONS);
}

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

@Test
public void testSizeOf() throws Exception {
  final File file = new File(getTestDirectory(), getName());
  // Null argument
  try {
    FileUtils.sizeOf(null);
    fail("Exception expected.");
  } catch (final NullPointerException ignore) {
  }
  // Non-existent file
  try {
    FileUtils.sizeOf(file);
    fail("Exception expected.");
  } catch (final IllegalArgumentException ignore) {
  }
  // Creates file
  file.createNewFile();
  file.deleteOnExit();
  // New file
  assertEquals(0, FileUtils.sizeOf(file));
  file.delete();
  // Existing file
  assertEquals("Unexpected files size",
      testFile1Size,
      FileUtils.sizeOf(testFile1));
  // Existing directory
  assertEquals("Unexpected directory size",
      TEST_DIRECTORY_SIZE,
      FileUtils.sizeOf(getTestDirectory()));
}

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

long size = FileUtils.sizeOf( file );

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

private boolean commonCheckThatLogFileAssertionSucceeds(AssertionInfo info, File actual,
  Object[] sequence) {
 checkIsNotNull(sequence);
 assertNotNull(info, actual);
 files.assertIsFile(info, actual);
 files.assertExists(info, actual);
 // if both actual and values are empty, then assertion passes.
 if (FileUtils.sizeOf(actual) == 0 && sequence.length == 0) {
  return true;
 }
 failIfEmptySinceActualIsNotEmpty(sequence);
 return false;
}

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

@Test
public void testJavadocParseError() throws Exception {
  try {
    JavadocPropertiesGenerator.main(
      getPath("InputJavadocPropertiesGeneratorJavadocParseError.java"),
      "--destfile", DESTFILE.getAbsolutePath());
    fail("Exception was expected");
  }
  catch (IllegalArgumentException ex) {
    assertTrue("Invalid error message", ex.getMessage().contains(
      "mismatched input '<EOF>' expecting JAVADOC_INLINE_TAG_END"));
  }
  assertEquals("File '" + DESTFILE + "' must be empty", 0, FileUtils.sizeOf(DESTFILE));
}

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

@Test
public void testNotImplementedTag() throws Exception {
  try {
    JavadocPropertiesGenerator.main(
      getPath("InputJavadocPropertiesGeneratorNotImplementedTag.java"),
      "--destfile", DESTFILE.getAbsolutePath());
    fail("Exception was expected");
  }
  catch (CheckstyleException ex) {
    assertEquals("Invalid error message", "Unsupported inline tag LINK_LITERAL",
      ex.getMessage());
  }
  assertEquals("File '" + DESTFILE + "' must be empty", 0, FileUtils.sizeOf(DESTFILE));
}

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

@Test
public void testNotConstants() throws Exception {
  JavadocPropertiesGenerator.main(getPath("InputJavadocPropertiesGeneratorNotConstants.java"),
    "--destfile", DESTFILE.getAbsolutePath());
  assertEquals("Unexpected error log", "", systemErr.getLog());
  assertEquals("Unexpected output log", "", systemOut.getLog());
  assertEquals("File '" + DESTFILE + "' must be empty", 0, FileUtils.sizeOf(DESTFILE));
}

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

@Test
public void testEmptyJavadoc() throws Exception {
  JavadocPropertiesGenerator.main(getPath("InputJavadocPropertiesGeneratorEmptyJavadoc.java"),
    "--destfile", DESTFILE.getAbsolutePath());
  assertEquals("Unexpected error log", "", systemErr.getLog());
  assertEquals("Unexpected output log", "", systemOut.getLog());
  assertEquals("File '" + DESTFILE + "' must be empty", 0, FileUtils.sizeOf(DESTFILE));
}

代码示例来源:origin: jamesagnew/hapi-fhir

while ((nRead = result.getEntity().getContent().read(data, 0, data.length)) != -1) {
  buffer.write(data, 0, nRead);
  long fileSize = FileUtils.sizeOf(localFile);
  if (fileSize > nextLog) {
    System.err.print("\r" + ansi().eraseLine());

代码示例来源:origin: jamesagnew/hapi-fhir

ourLog.info("Successfully Loaded example pack ({})", FileUtils.byteCountToDisplaySize(FileUtils.sizeOf(inputFile)));
IOUtils.closeQuietly(result.getEntity().getContent());

代码示例来源:origin: jamesagnew/hapi-fhir

if (!theFile.exists()) {
  actuallyWrite = true;
} else if (FileUtils.sizeOf(theFile) != bytesToWrite.length) {
  actuallyWrite = true;
} else {

代码示例来源:origin: org.openestate.is24/OpenEstate-IS24-REST-Core

/**
 * Return the total size of the local pool directory.
 *
 * @return size in bytes
 */
public long getTotalSize() {
  return (this.baseDir.exists()) ?
      FileUtils.sizeOf(this.baseDir) : 0;
}

代码示例来源:origin: PhoenicisOrg/phoenicis

List<File> uncompressTarGzFile(File inputFile, File outputDir, Consumer<ProgressEntity> stateCallback) {
  try (CountingInputStream countingInputStream = new CountingInputStream(new FileInputStream(inputFile));
      InputStream inputStream = new GZIPInputStream(countingInputStream)) {
    final long finalSize = FileUtils.sizeOf(inputFile);
    return uncompress(inputStream, countingInputStream, outputDir, finalSize, stateCallback);
  } catch (IOException e) {
    throw new ArchiveException(TAR_ERROR_MESSAGE, e);
  }
}

代码示例来源:origin: PhoenicisOrg/phoenicis

List<File> uncompressTarFile(File inputFile, File outputDir, Consumer<ProgressEntity> stateCallback) {
  try (CountingInputStream countingInputStream = new CountingInputStream(new FileInputStream(inputFile))) {
    final long finalSize = FileUtils.sizeOf(inputFile);
    return uncompress(countingInputStream, countingInputStream, outputDir, finalSize, stateCallback);
  } catch (IOException e) {
    throw new ArchiveException(TAR_ERROR_MESSAGE, e);
  }
}

代码示例来源:origin: PhoenicisOrg/phoenicis

List<File> uncompressZipFile(File inputFile, File outputDir, Consumer<ProgressEntity> stateCallback) {
  try (CountingInputStream inputStream = new CountingInputStream(new FileInputStream(inputFile))) {
    final long finalSize = FileUtils.sizeOf(inputFile);
    List<File> files = uncompress(inputStream, inputStream, outputDir, finalSize, stateCallback);
    return files;
  } catch (IOException e) {
    throw new ArchiveException(ZIP_ERROR_MESSAGE, e);
  }
}

代码示例来源:origin: shilad/wikibrain

/**
 * Create a dense matrix based on the data in a particular file.
 * @param path Path to the matrix data file.
 * @throws java.io.IOException
 */
public DenseMatrix(File path) throws IOException {
  this.path = path;
  info("initializing sparse matrix with file length " + FileUtils.sizeOf(path));
  this.channel = (new FileInputStream(path)).getChannel();
  readHeaders();
  rowBuffers = new MemoryMappedMatrix(path, channel, rowIds, rowOffsets);
}

相关文章

FileUtils类方法