java.nio.file.Files.size()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(7.2k)|赞(0)|评价(0)|浏览(318)

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

Files.size介绍

暂无

代码示例

代码示例来源:origin: spring-projects/spring-framework

/**
 * This implementation returns the underlying file's length.
 */
@Override
public long contentLength() throws IOException {
  return Files.size(this.path);
}

代码示例来源:origin: org.springframework/spring-core

/**
 * This implementation returns the underlying file's length.
 */
@Override
public long contentLength() throws IOException {
  return Files.size(this.path);
}

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

/**
 * Provide the standard Groovy <code>size()</code> method for <code>Path</code>.
 *
 * @param self a {@code Path} object
 * @return the file's size (length)
 * @since 2.3.0
 */
public static long size(Path self) throws IOException {
  return Files.size(self);
}

代码示例来源:origin: ata4/disunity

@Override
public long size() {
  try {
    return Files.size(file);
  } catch (IOException ex) {
    return 0;
  }
}

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

@Override
public Long getContentLength() {
  try {
    return Files.size(file);
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}

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

@Override
public int getNumBytes() {
  try {
    return (int) Files.size(path);
  } catch (IOException e) {
    e.printStackTrace();
    return -1;
  }
}

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

@Override
public long getFileSize( File fileName )
{
  try
  {
    return Files.size( path( fileName ) );
  }
  catch ( IOException e )
  {
    return 0;
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * This implementation returns the underlying File/Path length.
 */
@Override
public long contentLength() throws IOException {
  if (this.file != null) {
    long length = this.file.length();
    if (length == 0L && !this.file.exists()) {
      throw new FileNotFoundException(getDescription() +
          " cannot be resolved in the file system for checking its content length");
    }
    return length;
  }
  else {
    try {
      return Files.size(this.filePath);
    }
    catch (NoSuchFileException ex) {
      throw new FileNotFoundException(ex.getMessage());
    }
  }
}

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

public long getFileSize() throws IOException {
  if (isInMemory()) {
    return content.length;
  } else {
    return Files.size(file);
  }
}

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

private byte[] readFlowFromDisk() throws IOException {
  final Path flowPath = nifiProperties.getFlowConfigurationFile().toPath();
  if (!Files.exists(flowPath) || Files.size(flowPath) == 0) {
    return new byte[0];
  }
  final ByteArrayOutputStream baos = new ByteArrayOutputStream();
  try (final InputStream in = Files.newInputStream(flowPath, StandardOpenOption.READ);
      final InputStream gzipIn = new GZIPInputStream(in)) {
    FileUtils.copy(gzipIn, baos);
  }
  return baos.toByteArray();
}

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

@Override
public void addToArchive( Path archiveDestination, DiagnosticsReporterProgress progress )
    throws IOException
{
  // Heap dump has to target an actual file, we cannot stream directly to the archive
  progress.info( "dumping..." );
  Path tempFile = Files.createTempFile("neo4j-heapdump", ".hprof");
  Files.deleteIfExists( tempFile );
  heapDump( tempFile.toAbsolutePath().toString() );
  // Track progress of archiving process
  progress.info( "archiving..." );
  long size = Files.size( tempFile );
  InputStream in = Files.newInputStream( tempFile );
  try ( ProgressAwareInputStream inStream = new ProgressAwareInputStream( in, size, progress::percentChanged ) )
  {
    Files.copy( inStream, archiveDestination );
  }
  Files.delete( tempFile );
}

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

static void putTarEntry(TarArchiveOutputStream tarOutputStream, TarArchiveEntry tarEntry, Path file)
    throws IOException {
  tarEntry.setSize(Files.size(file));
  tarOutputStream.putArchiveEntry(tarEntry);
  try (InputStream input = new BufferedInputStream(Files.newInputStream(file))) {
    ByteStreams.copy(input, tarOutputStream);
    tarOutputStream.closeArchiveEntry();
  }
}

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

public static void main(String[] args) throws Exception {
  final ParameterTool params = ParameterTool.fromArgs(args);
  final Path inputFile = Paths.get(params.getRequired("inputFile"));
  final Path inputDir = Paths.get(params.getRequired("inputDir"));
  final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
  env.setParallelism(1);
  env.registerCachedFile(inputFile.toString(), "test_data", false);
  env.registerCachedFile(inputDir.toString(), "test_dir", false);
  final Path containedFile;
  try (Stream<Path> files = Files.list(inputDir)) {
    containedFile = files.findAny().orElseThrow(() -> new RuntimeException("Input directory must not be empty."));
  }
  env.fromElements(1)
    .map(new TestMapFunction(
      inputFile.toAbsolutePath().toString(),
      Files.size(inputFile),
      inputDir.toAbsolutePath().toString(),
      containedFile.getFileName().toString()))
    .writeAsText(params.getRequired("output"), FileSystem.WriteMode.OVERWRITE);
  env.execute("Distributed Cache Via Blob Test Program");
}

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

@Implementation
protected final AssetFileDescriptor openFd(String fileName) throws IOException {
 Path path = findAssetFile(fileName);
 if (path.getFileSystem().provider().getScheme().equals("jar")) {
  path = getFileFromZip(path);
 }
 ParcelFileDescriptor parcelFileDescriptor =
   ParcelFileDescriptor.open(path.toFile(), ParcelFileDescriptor.MODE_READ_ONLY);
 return new AssetFileDescriptor(parcelFileDescriptor, 0, Files.size(path));
}

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

public static Asset newFileAsset(FileTypedResource fileTypedResource) throws IOException {
 _FileAsset fileAsset = new _FileAsset();
 Path path = fileTypedResource.getPath();
 fileAsset.mFileName = Fs.externalize(path);
 fileAsset.mLength = Files.size(path);
 fileAsset.mBuf = Fs.getBytes(path);
 return fileAsset;
}

代码示例来源:origin: googleapis/google-cloud-java

@Test
public void testSize() throws Exception {
 Path path = Paths.get(URI.create("gs://bucket/wat"));
 Files.write(path, SINGULARITY.getBytes(UTF_8));
 assertThat(Files.size(path)).isEqualTo(SINGULARITY.getBytes(UTF_8).length);
}

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

@Override
public long size(final ContentClaim claim) throws IOException {
  if (claim == null) {
    return 0L;
  }
  // see javadocs for claim.getLength() as to why we do this.
  if (claim.getLength() < 0) {
    return Files.size(getPath(claim, true)) - claim.getOffset();
  }
  return claim.getLength();
}

代码示例来源:origin: OryxProject/oryx

public static void assertNonEmpty(Path p) throws IOException {
 assertTrue("File should exist: " + p, Files.exists(p));
 assertTrue("File should not be empty: " + p, Files.size(p) > 0);
}

代码示例来源:origin: square/okio

@Test public void testReadWriteFile() throws Exception {
 Path path = temporaryFolder.newFile().toPath();
 Sink sink = new FileChannelSink(FileChannel.open(path, w), Timeout.NONE);
 sink.write(new Buffer().writeUtf8(quote), 317);
 sink.close();
 assertTrue(Files.exists(path));
 assertEquals(quote.length(), Files.size(path));
 Buffer buffer = new Buffer();
 Source source = new FileChannelSource(FileChannel.open(path, r), Timeout.NONE);
 source.read(buffer, 44);
 assertThat(buffer.readUtf8())
   .isEqualTo("John, the kind of control you're attempting ");
 source.read(buffer, 31);
 assertThat(buffer.readUtf8())
   .isEqualTo("simply is... it's not possible.");
}

代码示例来源:origin: square/okio

@Test public void readWritePath() throws Exception {
 Path path = temporaryFolder.newFile().toPath();
 BufferedSink sink = Okio.buffer(Okio.sink(path));
 sink.writeUtf8("Hello, java.nio file!");
 sink.close();
 assertTrue(Files.exists(path));
 assertEquals(21, Files.size(path));
 BufferedSource source = Okio.buffer(Okio.source(path));
 assertEquals("Hello, java.nio file!", source.readUtf8());
 source.close();
}

相关文章

微信公众号

最新文章

更多