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

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

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

Files.getFileAttributeView介绍

暂无

代码示例

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

/**
 * Get POSIX attributes for file.
 *
 * @param file File.
 * @return PosixFileAttributes.
 */
@Nullable public static PosixFileAttributes posixAttributes(File file) {
  PosixFileAttributes attrs = null;
  try {
    PosixFileAttributeView view = Files.getFileAttributeView(file.toPath(), PosixFileAttributeView.class);
    if (view != null)
      attrs = view.readAttributes();
  }
  catch (IOException e) {
    throw new IgfsException("Failed to read POSIX attributes: " + file.getAbsolutePath(), e);
  }
  return attrs;
}

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

/**
 * Get POSIX attributes for file.
 *
 * @param file File.
 * @return BasicFileAttributes.
 */
@Nullable public static BasicFileAttributes basicAttributes(File file) {
  BasicFileAttributes attrs = null;
  try {
    BasicFileAttributeView view = Files.getFileAttributeView(file.toPath(), BasicFileAttributeView.class);
    if (view != null)
      attrs = view.readAttributes();
  }
  catch (IOException e) {
    throw new IgfsException("Failed to read basic file attributes: " + file.getAbsolutePath(), e);
  }
  return attrs;
}

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

/**
 * Changes the local file's user.
 *
 * @param path that will change owner
 * @param user the new user
 */
public static void changeLocalFileUser(String path, String user) throws IOException {
 UserPrincipalLookupService lookupService =
   FileSystems.getDefault().getUserPrincipalLookupService();
 PosixFileAttributeView view =
   Files.getFileAttributeView(Paths.get(path), PosixFileAttributeView.class,
     LinkOption.NOFOLLOW_LINKS);
 UserPrincipal userPrincipal = lookupService.lookupPrincipalByName(user);
 view.setOwner(userPrincipal);
}

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

/**
 * Changes the local file's group.
 *
 * @param path that will change owner
 * @param group the new group
 */
public static void changeLocalFileGroup(String path, String group) throws IOException {
 UserPrincipalLookupService lookupService =
   FileSystems.getDefault().getUserPrincipalLookupService();
 PosixFileAttributeView view =
   Files.getFileAttributeView(Paths.get(path), PosixFileAttributeView.class,
     LinkOption.NOFOLLOW_LINKS);
 GroupPrincipal groupPrincipal = lookupService.lookupPrincipalByGroupName(group);
 view.setGroup(groupPrincipal);
}

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

/**
 * Sets the {@link Path}'s last modified time and last access time to
 * the given valid times.
 *
 * @param mtime the modification time to set (only if no less than zero).
 * @param atime the access time to set (only if no less than zero).
 * @throws IOException if setting the times fails.
 */
@Override
public void setTimes(Path p, long mtime, long atime) throws IOException {
 try {
  BasicFileAttributeView view = Files.getFileAttributeView(
    pathToFile(p).toPath(), BasicFileAttributeView.class);
  FileTime fmtime = (mtime >= 0) ? FileTime.fromMillis(mtime) : null;
  FileTime fatime = (atime >= 0) ? FileTime.fromMillis(atime) : null;
  view.setTimes(fmtime, fatime, null);
 } catch (NoSuchFileException e) {
  throw new FileNotFoundException("File " + p + " does not exist");
 }
}

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

PosixFileAttributeView attrs = Files.getFileAttributeView(file.toPath(), PosixFileAttributeView.class);

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

private void setFileCreationDate(File f, long time) throws IOException {
  BasicFileAttributeView attributes = Files.getFileAttributeView(f.toPath(), BasicFileAttributeView.class);
  FileTime creationTime = FileTime.fromMillis(time);
  attributes.setTimes(creationTime, creationTime, creationTime);
 }
}

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

/** {@inheritDoc} */
@Override public void setTimes(IgfsPath path, long modificationTime, long accessTime) throws IgniteException {
  Path p = fileForPath(path).toPath();
  if (!Files.exists(p))
    throw new IgfsPathNotFoundException("Failed to set times (path not found): " + path);
  try {
    Files.getFileAttributeView(p, BasicFileAttributeView.class)
      .setTimes(
        (modificationTime >= 0) ? FileTime.from(modificationTime, TimeUnit.MILLISECONDS) : null,
        (accessTime >= 0) ? FileTime.from(accessTime, TimeUnit.MILLISECONDS) : null,
        null);
  }
  catch (IOException e) {
    throw new IgniteException("Failed to set times for path: " + path, e);
  }
}

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

@Test
public void testEquals_equalsTester() {
 new EqualsTester()
   .addEqualityGroup(
     Files.getFileAttributeView(
       Paths.get(URI.create("gs://red/rum")), CloudStorageFileAttributeView.class),
     Files.getFileAttributeView(
       Paths.get(URI.create("gs://red/rum")), CloudStorageFileAttributeView.class))
   .addEqualityGroup(
     Files.getFileAttributeView(
       Paths.get(URI.create("gs://red/lol/dog")), CloudStorageFileAttributeView.class))
   .testEquals();
}

代码示例来源:origin: zalando/zalenium

public static void setFilePermissions(Path filePath) throws IOException {
  if ("root".equalsIgnoreCase(ZaleniumConfiguration.getCurrentUser())) {
    UserPrincipal hostUid = filePath.getFileSystem()
        .getUserPrincipalLookupService().lookupPrincipalByName(ZaleniumConfiguration.getHostUid());
    Files.setOwner(filePath, hostUid);
    GroupPrincipal hostGid = filePath.getFileSystem()
        .getUserPrincipalLookupService().lookupPrincipalByGroupName(ZaleniumConfiguration.getHostGid());
    Files.getFileAttributeView(filePath, PosixFileAttributeView.class).setGroup(hostGid);
  }
}

代码示例来源:origin: org.apache.ant/ant

private void tryDos(Path p, boolean failIfDosIsNotSupported) {
  log("Falling back to DosFileAttributeView");
  boolean readOnly = !isWritable();
  DosFileAttributeView view = Files.getFileAttributeView(p, DosFileAttributeView.class);
  if (view != null) {
    try {
      view.setReadOnly(readOnly);
    } catch (IOException ioe) {
      maybeThrowException(ioe, "Failed to set permissions on '%s' due to %s",
                p, ioe.getMessage());
    } catch (SecurityException uoe) {
      maybeThrowException(null,
        "the SecurityManager denies role accessUserInformation or write access for SecurityManager.checkWrite for resource '%s'",
        p);
    }
  } else {
    String msg = String.format(
      "the associated path '%s' does not support the DosFileAttributeView",
      p);
    if (failIfDosIsNotSupported) {
      throw new BuildException(msg);
    }
    log("Warning: " + msg, Project.MSG_ERR);
  }
}

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

@Test
public void testReadAttributes_pseudoDirectory() throws IOException {
 Path dir = Paths.get(URI.create("gs://red/rum/"));
 CloudStorageFileAttributeView lazyAttributes =
   Files.getFileAttributeView(dir, CloudStorageFileAttributeView.class);
 assertThat(lazyAttributes.readAttributes())
   .isInstanceOf(CloudStoragePseudoDirectoryAttributes.class);
}

代码示例来源:origin: eclipse-vertx/vert.x

public Void perform() {
  try {
   Path target = vertx.resolveFile(path).toPath();
   UserPrincipalLookupService service = target.getFileSystem().getUserPrincipalLookupService();
   UserPrincipal userPrincipal = user == null ? null : service.lookupPrincipalByName(user);
   GroupPrincipal groupPrincipal = group == null ? null : service.lookupPrincipalByGroupName(group);
   if (groupPrincipal != null) {
    PosixFileAttributeView view = Files.getFileAttributeView(target, PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
    if (view == null) {
     throw new FileSystemException("Change group of file not supported");
    }
    view.setGroup(groupPrincipal);
   }
   if (userPrincipal != null) {
    Files.setOwner(target, userPrincipal);
   }
  } catch (SecurityException e) {
   throw new FileSystemException("Accessed denied for chown on " + path);
  } catch (IOException e) {
   throw new FileSystemException(e);
  }
  return null;
 }
};

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

@Test
public void testReadAttributes() throws IOException {
 Files.write(path, HAPPY, CloudStorageOptions.withCacheControl("potato"));
 CloudStorageFileAttributeView lazyAttributes =
   Files.getFileAttributeView(path, CloudStorageFileAttributeView.class);
 assertThat(lazyAttributes.readAttributes().cacheControl().get()).isEqualTo("potato");
}

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

/** {@inheritDoc} */
@Override public T2<Long, Long> times(String path) throws IOException {
  BasicFileAttributes attrs = Files.getFileAttributeView(path(path), BasicFileAttributeView.class)
    .readAttributes();
  return new T2<>(attrs.lastModifiedTime().toMillis(), attrs.lastAccessTime().toMillis());
}

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

@Test
public void testName() throws IOException {
 Files.write(path, HAPPY, CloudStorageOptions.withCacheControl("potato"));
 CloudStorageFileAttributeView lazyAttributes =
   Files.getFileAttributeView(path, CloudStorageFileAttributeView.class);
 assertThat(lazyAttributes.name()).isEqualTo("gcs");
}

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

/** {@inheritDoc} */
@Override public Map<String, String> properties(final String path) throws IOException {
  Path p = path(path);
  PosixFileAttributes attrs = Files.getFileAttributeView(p, PosixFileAttributeView.class).readAttributes();
  Map<String, String> props = new HashMap<>();
  props.put(IgfsUtils.PROP_USER_NAME, attrs.owner().getName());
  props.put(IgfsUtils.PROP_GROUP_NAME, attrs.group().getName());
  props.put(IgfsUtils.PROP_PERMISSION, permissions(path));
  return props;
}

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

@Test
public void testReadAttributes_notFound_throwsNoSuchFileException() throws IOException {
 CloudStorageFileAttributeView lazyAttributes =
   Files.getFileAttributeView(path, CloudStorageFileAttributeView.class);
 thrown.expect(NoSuchFileException.class);
 lazyAttributes.readAttributes();
}

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

/** {@inheritDoc} */
@Override public String permissions(String path) throws IOException {
  Path p = path(path);
  PosixFileAttributeView attrView = Files.getFileAttributeView(p, PosixFileAttributeView.class);
  if (attrView == null)
    throw new UnsupportedOperationException("Posix file attributes not available");
  int perm = 0;
  for(PosixFilePermission pfp : attrView.readAttributes().permissions())
    perm |= (1 << 8 - pfp.ordinal());
  return '0' + Integer.toOctalString(perm);
}

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

@Test
 public void testNullness() throws NoSuchMethodException, SecurityException {
  new NullPointerTester()
    .ignore(CloudStorageFileAttributeView.class.getMethod("equals", Object.class))
    .setDefault(FileTime.class, FileTime.fromMillis(0))
    .testAllPublicInstanceMethods(
      Files.getFileAttributeView(path, CloudStorageFileAttributeView.class));
 }
}

相关文章

微信公众号

最新文章

更多