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

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

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

Files.isReadable介绍

暂无

代码示例

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

/**
 * This implementation checks whether the underlying file is marked as readable
 * (and corresponds to an actual file with content, not to a directory).
 * @see java.nio.file.Files#isReadable(Path)
 * @see java.nio.file.Files#isDirectory(Path, java.nio.file.LinkOption...)
 */
@Override
public boolean isReadable() {
  return (Files.isReadable(this.path) && !Files.isDirectory(this.path));
}

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

public boolean isReadable(Path path) {
 return Files.isReadable(path);
}

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

/**
 * This implementation checks whether the underlying file is marked as readable
 * (and corresponds to an actual file with content, not to a directory).
 * @see java.io.File#canRead()
 * @see java.io.File#isDirectory()
 */
@Override
public boolean isReadable() {
  return (this.file != null ? this.file.canRead() && !this.file.isDirectory() :
      Files.isReadable(this.filePath) && !Files.isDirectory(this.filePath));
}

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

/**
 * This implementation checks whether the underlying file is marked as readable
 * (and corresponds to an actual file with content, not to a directory).
 * @see java.nio.file.Files#isReadable(Path)
 * @see java.nio.file.Files#isDirectory(Path, java.nio.file.LinkOption...)
 */
@Override
public boolean isReadable() {
  return (Files.isReadable(this.path) && !Files.isDirectory(this.path));
}

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

private boolean isRegularFileAndReadable(Path path) {
    return path != null && Files.isRegularFile(path) && Files.isReadable(path);
  }
}

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

/**
 * This implementation checks whether the underlying file is marked as readable
 * (and corresponds to an actual file with content, not to a directory).
 * @see java.io.File#canRead()
 * @see java.io.File#isDirectory()
 */
@Override
public boolean isReadable() {
  return (this.file != null ? this.file.canRead() && !this.file.isDirectory() :
      Files.isReadable(this.filePath) && !Files.isDirectory(this.filePath));
}

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

public KerberosAuthentication(String principal, String keytabLocation)
{
  requireNonNull(principal, "principal is null");
  requireNonNull(keytabLocation, "keytabLocation is null");
  Path keytabPath = Paths.get(keytabLocation);
  checkArgument(exists(keytabPath), "keytab does not exist: " + keytabLocation);
  checkArgument(isReadable(keytabPath), "keytab is not readable: " + keytabLocation);
  this.principal = createKerberosPrincipal(principal);
  this.configuration = createConfiguration(this.principal.getName(), keytabLocation);
}

代码示例来源:origin: bwssytems/ha-bridge

private String repositoryReader(Path filePath) {
  String content = null;
  if(Files.notExists(filePath) || !Files.isReadable(filePath)){
    log.warn("Error reading the file: " + filePath + " - Does not exist or is not readable. continuing...");
    return null;
  }
  
  try {
    content = new String(Files.readAllBytes(filePath));
  } catch (IOException e) {
    log.error("Error reading the file: " + filePath + " message: " + e.getMessage(), e);
  }
  
  return content;
}

代码示例来源:origin: bwssytems/ha-bridge

private String configReader(Path filePath) {
  String content = null;
  if(Files.notExists(filePath) || !Files.isReadable(filePath)){
    log.warn("Error reading the file: " + filePath + " - Does not exist or is not readable. continuing...");
    return null;
  }
  try {
    content = new String(Files.readAllBytes(filePath));
  } catch (IOException e) {
    log.error("Error reading the file: " + filePath + " message: " + e.getMessage(), e);
  }
  
  return content;
}

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

/**
 * Answers if the given path denotes existing directory.
 *
 * @param path The directory path.
 * @return {@code True} if the given path denotes an existing directory.
 */
public static boolean exists(String path) {
  if (F.isEmpty(path))
    return false;
  Path p = Paths.get(path);
  return Files.exists(p) && Files.isDirectory(p) && Files.isReadable(p);
}

代码示例来源:origin: bwssytems/ha-bridge

private String repositoryReader(Path filePath) {

    String content = null;
    if(Files.notExists(filePath) || !Files.isReadable(filePath)){
      log.warn("Error reading the file: " + filePath + " - Does not exist or is not readable. continuing...");
      return null;
    }

    
    try {
      content = new String(Files.readAllBytes(filePath));
    } catch (IOException e) {
      log.error("Error reading the file: " + filePath + " message: " + e.getMessage(), e);
    }
    
    return content;
  }
}

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

public static <T> T parseJson(Path path, Class<T> javaType)
  {
    if (!path.isAbsolute()) {
      path = path.toAbsolutePath();
    }

    checkArgument(exists(path), "File does not exist: %s", path);
    checkArgument(isReadable(path), "File is not readable: %s", path);

    try {
      byte[] json = Files.readAllBytes(path);
      ObjectMapper mapper = new ObjectMapperProvider().get()
          .enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
      return mapper.readValue(json, javaType);
    }
    catch (IOException e) {
      throw new IllegalArgumentException(format("Invalid JSON file '%s' for '%s'", path, javaType), e);
    }
  }
}

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

/**
 * Checks if a storage directory path is accessible.
 *
 * @param path the given path
 * @return true if path exists, false otherwise
 */
public static boolean isStorageDirAccessible(String path) {
 Path filePath = Paths.get(path);
 return Files.exists(filePath)
   && Files.isReadable(filePath)
   && Files.isWritable(filePath)
   && Files.isExecutable(filePath);
}

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

@Override
public Optional<Multimap<String, String>> validate() {
  final ArrayListMultimap<String, String> errors = ArrayListMultimap.create();
  final Path path = Paths.get(path());
  if (!Files.exists(path)) {
    errors.put("path", "The file does not exist.");
  } else if (!Files.isReadable(path)) {
    errors.put("path", "The file cannot be read.");
  }
  return errors.isEmpty() ? Optional.empty() : Optional.of(errors);
}

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

@Override
public Optional<Multimap<String, String>> validate() {
  final ArrayListMultimap<String, String> errors = ArrayListMultimap.create();
  final Path path = Paths.get(path());
  if (!Files.exists(path)) {
    errors.put("path", "The file does not exist.");
  } else if (!Files.isReadable(path)) {
    errors.put("path", "The file cannot be read.");
  }
  return errors.isEmpty() ? Optional.empty() : Optional.of(errors);
}

代码示例来源:origin: floragunncom/search-guard

public static void checkPath(String keystoreFilePath, String fileNameLogOnly) {
  
  if (keystoreFilePath == null || keystoreFilePath.length() == 0) {
    throw new ElasticsearchException("Empty file path for "+fileNameLogOnly);
  }
  
  if (Files.isDirectory(Paths.get(keystoreFilePath), LinkOption.NOFOLLOW_LINKS)) {
    throw new ElasticsearchException("Is a directory: " + keystoreFilePath+" Expected a file for "+fileNameLogOnly);
  }
  if(!Files.isReadable(Paths.get(keystoreFilePath))) {
    throw new ElasticsearchException("Unable to read " + keystoreFilePath + " ("+Paths.get(keystoreFilePath)+"). Please make sure this files exists and is readable regarding to permissions. Property: "+fileNameLogOnly);
  }
}

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

/**
 * Set permission of log file so that logviewer can serve the file.
 *
 * @param fileName log file
 */
public void setLogFilePermission(String fileName) throws IOException {
  File file = new File(logRootDir, fileName).getCanonicalFile();
  boolean runAsUser = ObjectReader.getBoolean(stormConf.get(SUPERVISOR_RUN_WORKER_AS_USER), false);
  File parent = new File(logRootDir, fileName).getParentFile();
  Optional<File> mdFile = (parent == null) ? Optional.empty() : getMetadataFileForWorkerLogDir(parent);
  Optional<String> topoOwner = mdFile.isPresent()
      ? Optional.of(getTopologyOwnerFromMetadataFile(mdFile.get().getCanonicalPath()))
      : Optional.empty();
  if (runAsUser && topoOwner.isPresent() && file.exists() && !Files.isReadable(file.toPath())) {
    LOG.debug("Setting permissions on file {} with topo-owner {}", fileName, topoOwner);
    try {
      ClientSupervisorUtils.processLauncherAndWait(stormConf, topoOwner.get(),
          Lists.newArrayList("blob", file.getCanonicalPath()), null,
          "setup group read permissions for file: " + fileName);
    } catch (IOException e) {
      numSetPermissionsExceptions.mark();
      throw e;
    }
  }
}

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

private SSLEngineConfigurator buildSslEngineConfigurator(Path certFile, Path keyFile, String keyPassword)
    throws GeneralSecurityException, IOException {
  if (keyFile == null || !Files.isRegularFile(keyFile) || !Files.isReadable(keyFile)) {
    throw new InvalidKeyException("Unreadable or missing private key: " + keyFile);
  }
  if (certFile == null || !Files.isRegularFile(certFile) || !Files.isReadable(certFile)) {
    throw new CertificateException("Unreadable or missing X.509 certificate: " + certFile);
  }
  final SSLContextConfigurator sslContextConfigurator = new SSLContextConfigurator();
  final char[] password = firstNonNull(keyPassword, "").toCharArray();
  final KeyStore keyStore = PemKeyStore.buildKeyStore(certFile, keyFile, password);
  sslContextConfigurator.setKeyStorePass(password);
  sslContextConfigurator.setKeyStoreBytes(KeyStoreUtils.getBytes(keyStore, password));
  final SSLContext sslContext = sslContextConfigurator.createSSLContext(true);
  return new SSLEngineConfigurator(sslContext, false, false, false);
}

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

@Override
protected void doStart() throws Exception {
  Path path = Paths.get(config.path());
  fileInfo = FileInfo.forPath(path);
  if (!Files.isReadable(path)) {
    LOG.warn("Cannot read database file {}", config.path());
    setError(new IllegalStateException("Cannot read database file " + config.path()));
  } else {
    try {
      this.databaseReader.set(loadReader(path.toFile()));
    } catch (Exception e) {
      LOG.warn("Unable to read data base file {}", config.path());
      setError(e);
    }
  }
}

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

@Test
public void canReadMemInfoFileIfExists() {
 final long size = this.util.getOsTotalFreeMemorySize();
 final Path memFile = Paths.get("/proc/meminfo");
 if (!(Files.isRegularFile(memFile) && Files.isReadable(memFile))) {
  assertTrue(size == 0);
 }
 // todo HappyRay: investigate why size returned is 0 on Travis only but works on my Linux machine.
 // I can't find a way to get to the Gradle test report on Travis which makes debugging difficult.
}

相关文章

微信公众号

最新文章

更多