java.io.File.getTotalSpace()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(8.2k)|赞(0)|评价(0)|浏览(154)

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

File.getTotalSpace介绍

[英]Returns the total size in bytes of the partition containing this path. Returns 0 if this path does not exist.
[中]返回包含此路径的分区的总大小(字节)。如果此路径不存在,则返回0。

代码示例

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

@Override
  public Long invoke(File f, VirtualChannel channel) throws IOException {
    return f.getTotalSpace();
  }
}

代码示例来源:origin: h2oai/h2o-2

@Override public long getTotalSpace() {
  return _root.getTotalSpace();
 }
}

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

/**
 * Returns the total amount of space.
 *
 * @return the total amount of space
 */
public long getTotalSpace() {
 return file.getTotalSpace();
}

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

/**
 * Returns the capacity for a given path
 * @param path path
 * @return total space
 */
public static long getContainerCapacity(final Path path) {
  return path.toFile().getTotalSpace();
}

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

StorageLocation(File path, long maxSize, @Nullable Double freeSpacePercent)
{
 this.path = path;
 this.maxSize = maxSize;
 if (freeSpacePercent != null) {
  long totalSpaceInPartition = path.getTotalSpace();
  this.freeSpaceToKeep = (long) ((freeSpacePercent * totalSpaceInPartition) / 100);
  log.info(
    "SegmentLocation[%s] will try and maintain [%d:%d] free space while loading segments.",
    path,
    freeSpaceToKeep,
    totalSpaceInPartition
  );
 } else {
  this.freeSpaceToKeep = 0;
 }
 this.segments = new HashSet<>();
}

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

public static double getDiskPartitionSpaceUsedPercent(final String path) {
  if (null == path || path.isEmpty())
    return -1;
  try {
    File file = new File(path);
    if (!file.exists())
      return -1;
    long totalSpace = file.getTotalSpace();
    if (totalSpace > 0) {
      long freeSpace = file.getFreeSpace();
      long usedSpace = totalSpace - freeSpace;
      return usedSpace / (double) totalSpace;
    }
  } catch (Exception e) {
    return -1;
  }
  return -1;
}

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

@Override
  public long percentAvailable(File file)
  {
    return Math.round(((double) file.getFreeSpace() / file.getTotalSpace()) * 100);
  }
}

代码示例来源:origin: org.apache.hadoop/hadoop-common

public String check(String folder) {
 if (folder == null)
  throw new IllegalArgumentException("folder");
 File f = new File(folder);
 folderSize = getFileSize(f);
 usage = 1.0*(f.getTotalSpace() - f.getFreeSpace())/ f.getTotalSpace();
 return String.format("used %d files %d disk in use %f", folderSize, fileCount, usage);
}

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

private static String getDiskSpace( DatabaseLayout databaseLayout )
{
  File directory = databaseLayout.databaseDirectory();
  long free = directory.getFreeSpace();
  long total = directory.getTotalSpace();
  long percentage = total != 0 ? (free * 100 / total) : 0;
  return String.format( "Disk space on partition (Total / Free / Free %%): %s / %s / %s", total, free, percentage );
}

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

@Override
public long getSpace(String path, SpaceType type) throws IOException {
 path = stripPath(path);
 File file = new File(path);
 switch (type) {
  case SPACE_TOTAL:
   return file.getTotalSpace();
  case SPACE_FREE:
   return file.getFreeSpace();
  case SPACE_USED:
   return file.getTotalSpace() - file.getFreeSpace();
  default:
   throw new IOException("Unknown space type: " + type);
 }
}

代码示例来源:origin: stackoverflow.com

System.out.println("Total space (bytes): " + root.getTotalSpace());
System.out.println("Free space (bytes): " + root.getFreeSpace());
System.out.println("Usable space (bytes): " + root.getUsableSpace());

代码示例来源:origin: org.apache.hadoop/hadoop-common

@Override
public FsStatus getStatus(Path p) throws IOException {
 File partition = pathToFile(p == null ? new Path("/") : p);
 //File provides getUsableSpace() and getFreeSpace()
 //File provides no API to obtain used space, assume used = total - free
 return new FsStatus(partition.getTotalSpace(), 
  partition.getTotalSpace() - partition.getFreeSpace(),
  partition.getFreeSpace());
}

代码示例来源:origin: Graylog2/graylog2-server

@Override
  public FsStats fsStats() {
    final Map<String, FsStats.Filesystem> filesystems = new HashMap<>(locations.size());

    for (File location : locations) {
      final String path = location.getAbsolutePath();
      final long total = location.getTotalSpace();
      final long free = location.getFreeSpace();
      final long available = location.getUsableSpace();
      final long used = total - free;
      final short usedPercent = (short) ((double) used / total * 100);

      final FsStats.Filesystem filesystem = FsStats.Filesystem.create(
          path, total, free, available, used, usedPercent);

      filesystems.put(path, filesystem);
    }

    return FsStats.create(filesystems);
  }
}

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

protected void doRun() {
    long free = Jenkins.getInstance().getRootDir().getUsableSpace();
    long total = Jenkins.getInstance().getRootDir().getTotalSpace();
    if(free<=0 || total<=0) {
      // information unavailable. pointless to try.
      LOGGER.info("JENKINS_HOME disk usage information isn't available. aborting to monitor");
      cancel();
      return;
    }
    LOGGER.fine("Monitoring disk usage of JENKINS_HOME. total="+total+" free="+free);
    // if it's more than 90% full and less than the minimum, activate
    // it's AND and not OR so that small Hudson home won't get a warning,
    // and similarly, if you have a 1TB disk, you don't get a warning when you still have 100GB to go.
    HudsonHomeDiskUsageMonitor.get().activated = (total/free>10 && free< FREE_SPACE_THRESHOLD);
}

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

protected void percentLimitFromFile(File directory) {
  if (percentLimit > 0) {
    if (total > 0) {
      this.setLimit(total * percentLimit / 100);
    } else if (directory != null) {
      File dir = StoreUtil.findParentDirectory(directory);
      if (dir != null) {
        this.setLimit(dir.getTotalSpace() * percentLimit / 100);
      }
    }
  }
}

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

private StorageLocation fakeLocation(long total, long free, long max, Double percent)
{
 File file = EasyMock.mock(File.class);
 EasyMock.expect(file.getTotalSpace()).andReturn(total).anyTimes();
 EasyMock.expect(file.getFreeSpace()).andReturn(free).anyTimes();
 EasyMock.replay(file);
 return new StorageLocation(file, max, percent);
}

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

@Test
public void updateSendsTotalToRecordsStats() {
 File dir = mock(File.class);
 when(dir.exists()).thenReturn(true);
 final long expectedTotal = 123;
 when(dir.getTotalSpace()).thenReturn(expectedTotal);
 TestableDiskUsage diskUsage = new TestableDiskUsage(dir);
 diskUsage.update(0.0f, 1.0f);
 assertThat(diskUsage.getTotal()).isEqualTo(expectedTotal);
}

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

@Test
public void criticalMessageStatesUsageExceedsCritical() {
 File dir = mock(File.class);
 when(dir.exists()).thenReturn(true);
 when(dir.getTotalSpace()).thenReturn(1024L * 1024L * 3L);
 when(dir.getUsableSpace()).thenReturn(1024L * 1024L * 2L);
 TestableDiskUsage diskUsage = new TestableDiskUsage(dir, 1/* one megabyte */);
 assertThat(diskUsage.update(0.0f, 33.2f)).isEqualTo(DiskState.CRITICAL);
 assertThat(diskUsage.getNext()).isEqualTo(DiskState.CRITICAL);
 assertThat(diskUsage.getPct()).isEqualTo("33.3%");
 assertThat(diskUsage.getCriticalMessage())
   .isEqualTo("the file system is 33.3% full, which reached the critical threshold of 33.2%.");
}

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

@Test
public void criticalMessageStatesUsageExceedsCriticalWithManyDigits() {
 File dir = mock(File.class);
 when(dir.exists()).thenReturn(true);
 when(dir.getTotalSpace()).thenReturn(1024L * 1024L * 3L);
 when(dir.getUsableSpace()).thenReturn(1024L * 1024L * 2L);
 TestableDiskUsage diskUsage = new TestableDiskUsage(dir, 1/* one megabyte */);
 assertThat(diskUsage.update(0.0f, 33.25783495783648593746336f)).isEqualTo(DiskState.CRITICAL);
 assertThat(diskUsage.getNext()).isEqualTo(DiskState.CRITICAL);
 assertThat(diskUsage.getPct()).isEqualTo("33.3%");
 assertThat(diskUsage.getCriticalMessage())
   .isEqualTo("the file system is 33.3% full, which reached the critical threshold of 33.3%.");
}

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

@Test
public void shouldPrintDiskUsage()
{
  // Not sure how to get around this w/o spying. The method that we're unit testing will construct
  // other File instances with this guy as parent and internally the File constructor uses the field 'path'
  // which, if purely mocked, won't be assigned. At the same time we want to control the total/free space methods
  // and what they return... a tough one.
  File storeDir = Mockito.spy( new File( "storeDir" ) );
  DatabaseLayout layout = mock( DatabaseLayout.class );
  when( layout.databaseDirectory() ).thenReturn( storeDir );
  when( storeDir.getTotalSpace() ).thenReturn( 100L );
  when( storeDir.getFreeSpace() ).thenReturn( 40L );
  AssertableLogProvider logProvider = new AssertableLogProvider();
  KernelDiagnostics.StoreFiles storeFiles = new KernelDiagnostics.StoreFiles( layout );
  storeFiles.dump( logProvider.getLog( getClass() ).debugLogger() );
  logProvider.assertContainsMessageContaining( "100 / 40 / 40" );
}

相关文章