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

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

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

Files.isSameFile介绍

暂无

代码示例

代码示例来源:origin: siacs/Conversations

@Override
  public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
    // Don't delete the cache directory itself.
    if (!Files.isSameFile(dir, cacheDirectory)) {
      Files.deleteIfExists(dir);
    }
    return FileVisitResult.CONTINUE;
  }
});

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

/**
 * Create a symbolic link pointing at target
 *
 * @param link   the link to create
 * @param target where it should point to
 * @throws IOException on any error.
 */
@Override
public void createSymlink(File link, File target) throws IOException {
  if (_symlinksDisabled) {
    throw new IOException("Symlinks have been disabled, this should not be called");
  }
  Path plink = link.toPath().toAbsolutePath();
  Path ptarget = target.toPath().toAbsolutePath();
  LOG.debug("Creating symlink [{}] to [{}]", plink, ptarget);
  if (Files.exists(plink)) {
    if (Files.isSameFile(plink, ptarget)) {
      //It already points where we want it to
      return;
    }
    FileUtils.forceDelete(link);
  }
  Files.createSymbolicLink(plink, ptarget);
}

代码示例来源:origin: MovingBlocks/Terasology

Path installModPath = installPath.resolve(MOD_DIR);
Files.createDirectories(installModPath);
if (Files.isSameFile(homeModPath, installModPath)) {
  modPaths = ImmutableList.of(homeModPath);
} else {

代码示例来源:origin: google/guava

/**
 * Returns true if the files located by the given paths exist, are not directories, and contain
 * the same bytes.
 *
 * @throws IOException if an I/O error occurs
 * @since 22.0
 */
public static boolean equal(Path path1, Path path2) throws IOException {
 checkNotNull(path1);
 checkNotNull(path2);
 if (Files.isSameFile(path1, path2)) {
  return true;
 }
 /*
  * Some operating systems may return zero as the length for files denoting system-dependent
  * entities such as devices or pipes, in which case we must fall back on comparing the bytes
  * directly.
  */
 ByteSource source1 = asByteSource(path1);
 ByteSource source2 = asByteSource(path2);
 long len1 = source1.sizeIfKnown().or(0L);
 long len2 = source2.sizeIfKnown().or(0L);
 if (len1 != 0 && len2 != 0 && len1 != len2) {
  return false;
 }
 return source1.contentEquals(source2);
}

代码示例来源:origin: looly/hutool

/**
 * 检查两个文件是否是同一个文件<br>
 * 所谓文件相同,是指File对象是否指向同一个文件或文件夹
 * 
 * @param file1 文件1
 * @param file2 文件2
 * @return 是否相同
 * @throws IORuntimeException IO异常
 * @see Files#isSameFile(Path, Path)
 */
public static boolean equals(File file1, File file2) throws IORuntimeException {
  Assert.notNull(file1);
  Assert.notNull(file2);
  if (false == file1.exists() || false == file2.exists()) {
    // 两个文件都不存在判断其路径是否相同
    if (false == file1.exists() && false == file2.exists() && pathEquals(file1, file2)) {
      return true;
    }
    // 对于一个存在一个不存在的情况,一定不相同
    return false;
  }
  try {
    return Files.isSameFile(file1.toPath(), file2.toPath());
  } catch (IOException e) {
    throw new IORuntimeException(e);
  }
}

代码示例来源:origin: looly/hutool

/**
 * 检查两个文件是否是同一个文件<br>
 * 所谓文件相同,是指File对象是否指向同一个文件或文件夹
 * 
 * @param file1 文件1
 * @param file2 文件2
 * @return 是否相同
 * @throws IORuntimeException IO异常
 * @see Files#isSameFile(Path, Path)
 */
public static boolean equals(File file1, File file2) throws IORuntimeException {
  Assert.notNull(file1);
  Assert.notNull(file2);
  if (false == file1.exists() || false == file2.exists()) {
    // 两个文件都不存在判断其路径是否相同
    if (false == file1.exists() && false == file2.exists() && pathEquals(file1, file2)) {
      return true;
    }
    // 对于一个存在一个不存在的情况,一定不相同
    return false;
  }
  try {
    return Files.isSameFile(file1.toPath(), file2.toPath());
  } catch (IOException e) {
    throw new IORuntimeException(e);
  }
}

代码示例来源:origin: google/j2objc

/**
 * Returns true if the files located by the given paths exist, are not directories, and contain
 * the same bytes.
 *
 * @throws IOException if an I/O error occurs
 * @since 22.0
 */
public static boolean equal(Path path1, Path path2) throws IOException {
 checkNotNull(path1);
 checkNotNull(path2);
 if (Files.isSameFile(path1, path2)) {
  return true;
 }
 /*
  * Some operating systems may return zero as the length for files denoting system-dependent
  * entities such as devices or pipes, in which case we must fall back on comparing the bytes
  * directly.
  */
 ByteSource source1 = asByteSource(path1);
 ByteSource source2 = asByteSource(path2);
 long len1 = source1.sizeIfKnown().or(0L);
 long len2 = source2.sizeIfKnown().or(0L);
 if (len1 != 0 && len2 != 0 && len1 != len2) {
  return false;
 }
 return source1.contentEquals(source2);
}

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

if (!Files.isSameFile(path0, path1))
  throw new IgniteException("Failed to set IGNITE_HOME after it has been already resolved " +
    "[igniteHome=" + path0 + ", newIgniteHome=" + path1 + ']');

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

/**
 * Returns true if the files located by the given paths exist, are not directories, and contain
 * the same bytes.
 *
 * @throws IOException if an I/O error occurs
 * @since 22.0
 */
public static boolean equal(Path path1, Path path2) throws IOException {
 checkNotNull(path1);
 checkNotNull(path2);
 if (Files.isSameFile(path1, path2)) {
  return true;
 }
 /*
  * Some operating systems may return zero as the length for files denoting system-dependent
  * entities such as devices or pipes, in which case we must fall back on comparing the bytes
  * directly.
  */
 ByteSource source1 = asByteSource(path1);
 ByteSource source2 = asByteSource(path2);
 long len1 = source1.sizeIfKnown().or(0L);
 long len2 = source2.sizeIfKnown().or(0L);
 if (len1 != 0 && len2 != 0 && len1 != len2) {
  return false;
 }
 return source1.contentEquals(source2);
}

代码示例来源:origin: oracle/opengrok

if (!Files.isSameFile(given, Paths.get(wsRoot))) {
  String linkedTo = Files.readSymbolicLink(given).toRealPath().toString();
  if (linkedTo.regionMatches(0, wsRoot, 0, wsRoot.length())) {

代码示例来源:origin: spotify/docker-client

@Override
public FileVisitResult preVisitDirectory(Path dir,
                     BasicFileAttributes attrs) throws IOException {
 if (Files.isSameFile(dir, root)) {
  return FileVisitResult.CONTINUE;
 }
 final Path relativePath = root.relativize(dir);
 if (exclude(ignoreMatchers, relativePath)) {
  return FileVisitResult.CONTINUE;
 }
 final TarArchiveEntry entry = new TarArchiveEntry(dir.toFile());
 entry.setName(relativePath.toString());
 entry.setMode(getFileMode(dir));
 tarStream.putArchiveEntry(entry);
 tarStream.closeArchiveEntry();
 return FileVisitResult.CONTINUE;
}

代码示例来源:origin: google/guava

public void testEqual() throws IOException {
 try (FileSystem fs = Jimfs.newFileSystem(Configuration.unix())) {
  Path fooPath = fs.getPath("foo");
  Path barPath = fs.getPath("bar");
  MoreFiles.asCharSink(fooPath, UTF_8).write("foo");
  MoreFiles.asCharSink(barPath, UTF_8).write("barbar");
  assertThat(MoreFiles.equal(fooPath, barPath)).isFalse();
  assertThat(MoreFiles.equal(fooPath, fooPath)).isTrue();
  assertThat(MoreFiles.asByteSource(fooPath).contentEquals(MoreFiles.asByteSource(fooPath)))
    .isTrue();
  Path fooCopy = Files.copy(fooPath, fs.getPath("fooCopy"));
  assertThat(Files.isSameFile(fooPath, fooCopy)).isFalse();
  assertThat(MoreFiles.equal(fooPath, fooCopy)).isTrue();
  MoreFiles.asCharSink(fooCopy, UTF_8).write("boo");
  assertThat(MoreFiles.asByteSource(fooPath).size())
    .isEqualTo(MoreFiles.asByteSource(fooCopy).size());
  assertThat(MoreFiles.equal(fooPath, fooCopy)).isFalse();
  // should also assert that a Path that erroneously reports a size 0 can still be compared,
  // not sure how to do that with the Path API
 }
}

代码示例来源:origin: prestodb/presto

/**
 * Returns true if the files located by the given paths exist, are not directories, and contain
 * the same bytes.
 *
 * @throws IOException if an I/O error occurs
 * @since 22.0
 */
public static boolean equal(Path path1, Path path2) throws IOException {
 checkNotNull(path1);
 checkNotNull(path2);
 if (Files.isSameFile(path1, path2)) {
  return true;
 }
 /*
  * Some operating systems may return zero as the length for files denoting system-dependent
  * entities such as devices or pipes, in which case we must fall back on comparing the bytes
  * directly.
  */
 ByteSource source1 = asByteSource(path1);
 ByteSource source2 = asByteSource(path2);
 long len1 = source1.sizeIfKnown().or(0L);
 long len2 = source2.sizeIfKnown().or(0L);
 if (len1 != 0 && len2 != 0 && len1 != len2) {
  return false;
 }
 return source1.contentEquals(source2);
}

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

@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
  if (Files.isSameFile(dir, basePath)) {
    finishWalk();
  }
  return FileVisitResult.CONTINUE;
}

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

@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
  log.error("Error occured at {}: {}", file, exc.getMessage(), exc);
  if (basePath!=null && Files.isSameFile(file, basePath)) {
    finishWalk();
  }
  return FileVisitResult.CONTINUE;
}

代码示例来源:origin: de.pfabulist.lindwurm/niotest

@Test
public void testIsSameFileOnEqualPath() throws IOException {
  Path file = getNonExistingPath();
  assertThat( Files.isSameFile( file, file ) ).isTrue();
}

代码示例来源:origin: de.pfabulist.lindwurm/niotest

@Test
@Category({ CaseInsensitive.class, Writable.class })
public void testCaseInsensitivePathsPointToSameFile() throws IOException {
  Path file = dirTA().resolve( nameD() );
  // create file where last filename is mixed case
  Files.write( mixCase( file ), CONTENT );
  assertThat( Files.isSameFile( file, mixCase( file ) ) ).isTrue();
}

代码示例来源:origin: de.pfabulist.lindwurm/niotest

@Test
@Category( { SymLink.class } )
public void testMoveARelSymLink2() throws IOException {
  Files.createSymbolicLink( symLink(), FS.getPath( ".." ).resolve( nameA() ) );
  Path to = dirTB().resolve( nameB() );
  Files.move( symLink(), to );
  Files.write( to, CONTENT );
  assertThat( Files.isSameFile( to, absTA() ) ).isTrue();
}

代码示例来源:origin: dstl/baleen

private void assertFilesEquals(String s1, String s2) throws IOException {
  File f1 = new File(s1);
  File f2 = new File(s2);

  assertTrue(Files.isSameFile(f1.toPath(), f2.toPath()));
 }
}

代码示例来源:origin: de.pfabulist.lindwurm/niotest

@Test
@Category( { SymLink.class, DirSymLink.class } )
public void testDeepSymLinkIsSameFile() throws IOException {
  Files.createSymbolicLink( symLink(), targetDir() );
  Files.write( linkKid(), CONTENT );
  assertThat( Files.isSameFile( linkKid(), targetDir().resolve( linkKid().getFileName() ) ) ).isTrue();
}

相关文章

微信公众号

最新文章

更多