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

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

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

File.canRead介绍

[英]Indicates whether the current context is allowed to read from this file.
[中]指示是否允许从该文件读取当前上下文。

代码示例

代码示例来源: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: jeasonlzy/okhttp-OkGo

/**
 * If the folder can be read.
 *
 * @param path path.
 * @return True: success, or false: failure.
 */
public static boolean canRead(String path) {
  return new File(path).canRead();
}

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

/**
 * read lines.
 *
 * @param file file.
 * @return lines.
 * @throws IOException
 */
public static String[] readLines(File file) throws IOException {
  if (file == null || !file.exists() || !file.canRead()) {
    return new String[0];
  }
  return readLines(new FileInputStream(file));
}

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

public static void checkJarFile(URL jar) throws IOException {
  File jarFile;
  try {
    jarFile = new File(jar.toURI());
  } catch (URISyntaxException e) {
    throw new IOException("JAR file path is invalid '" + jar + "'");
  }
  if (!jarFile.exists()) {
    throw new IOException("JAR file does not exist '" + jarFile.getAbsolutePath() + "'");
  }
  if (!jarFile.canRead()) {
    throw new IOException("JAR file can't be read '" + jarFile.getAbsolutePath() + "'");
  }
  // TODO: Check if proper JAR file
}

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

private File loadTxnFile(String txnLogFileName) throws TxnLogToolkitException {
  File logFile = new File(txnLogFileName);
  if (!logFile.exists() || !logFile.canRead()) {
    throw new TxnLogToolkitException(ExitCode.UNEXPECTED_ERROR.getValue(), "File doesn't exist or not readable: %s", logFile);
  }
  return logFile;
}

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

protected static File getCVSFile(String parent, String name) {
  try {
    File CVSdir = new File(parent, "CVS");
    if (CVSdir.isDirectory() && CVSdir.canRead()) {
      File root = new File(CVSdir, "Root");
      if (root.canRead()) {
        return readCVSRoot(root, CVSdir, name);
      }
    }
  } catch (Exception e) {
    LOGGER.log(Level.WARNING,
        "Failed to retrieve CVS file of parent: " + parent + ", name: " + name, e);
  }
  return null;
}

代码示例来源:origin: ming1016/study

static boolean isReadableFile(String path) {
  File f = new File(path);
  return f.canRead() && f.isFile();
}

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

/**
 * @return true iff the argument is a readable directory
 */
public static boolean isReadableDir(File d) {
  return d.exists() && d.isDirectory() && d.canRead();
}

代码示例来源:origin: Tencent/tinker

public static final boolean isLegalFile(File file) {
  return file != null && file.exists() && file.canRead() && file.isFile() && file.length() > 0;
}

代码示例来源:origin: remkop/picocli

private void expandArgumentFile(String fileName, List<String> arguments, Set<String> visited) {
  File file = new File(fileName);
  if (!file.canRead()) {
    if (tracer.isInfo()) {tracer.info("File %s does not exist or cannot be read; treating argument literally%n", fileName);}
    arguments.add("@" + fileName);
  } else if (visited.contains(file.getAbsolutePath())) {
    if (tracer.isInfo()) {tracer.info("Already visited file %s; ignoring...%n", file.getAbsolutePath());}
  } else {
    expandValidArgumentFile(fileName, file, arguments, visited);
  }
}
private void expandValidArgumentFile(String fileName, File file, List<String> arguments, Set<String> visited) {

代码示例来源:origin: AsyncHttpClient/async-http-client

public FileMultipartPart(FilePart part, byte[] boundary) {
 super(part, boundary);
 File file = part.getFile();
 if (!file.exists()) {
  throw new IllegalArgumentException("File part doesn't exist: " + file.getAbsolutePath());
 } else if (!file.canRead()) {
  throw new IllegalArgumentException("File part can't be read: " + file.getAbsolutePath());
 }
 length = file.length();
}

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

logger.trace("Searching directory [" + dir.getAbsolutePath() +
    "] for files matching pattern [" + fullPattern + "]");
String currPath = StringUtils.replace(content.getAbsolutePath(), File.separator, "/");
if (content.isDirectory() && getPathMatcher().matchStart(fullPattern, currPath + "/")) {
  if (!content.canRead()) {
    if (logger.isDebugEnabled()) {
      logger.debug("Skipping subdirectory [" + dir.getAbsolutePath() +
          "] because the application is not allowed to read the directory");

代码示例来源:origin: AsyncHttpClient/async-http-client

public FilePart(String name, File file, String contentType, Charset charset, String fileName, String contentId, String transferEncoding) {
 super(name,
     contentType,
     charset,
     fileName != null ? fileName : file.getName(),
     contentId,
     transferEncoding);
 if (!assertNotNull(file, "file").isFile())
  throw new IllegalArgumentException("File is not a normal file " + file.getAbsolutePath());
 if (!file.canRead())
  throw new IllegalArgumentException("File is not readable " + file.getAbsolutePath());
 this.file = file;
}

代码示例来源:origin: naman14/Timber

@Override
public boolean accept(File pathname) {
  String name = pathname.getName();
  return !".".equals(name) && !"..".equals(name) && pathname.canRead() && (pathname.isDirectory()  || (pathname.isFile() && checkFileExt(name)));
}

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

private void checkFilesAccessibility(File bundledPlugins, File externalPlugins) {
  boolean bundled = bundledPlugins.canRead();
  boolean external = externalPlugins.canRead();
  if (!bundled || !external) {
    String folder = bundled ? externalPlugins.getAbsolutePath() : bundledPlugins.getAbsolutePath();
    LOG.error("Could not read plugins. Please check access rights on files in folder: {}.", folder);
    throw new FileAccessRightsCheckException(String.format("Could not read plugins. Please make sure that the user running GoCD can access %s", folder));
  }
}

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

/**
 * Validates {@code file} is an existing file that is readable.
 *
 * @param file the File to test
 * @return the provided file, if all validation passes
 * @throws IllegalArgumentException if validation fails
 */
private static File validateFile(File file) throws IllegalArgumentException {
 if (!file.isFile()) {
  throw new IllegalArgumentException("Path is not a file: " + file);
 }
 if (!file.canRead()) {
  throw new IllegalArgumentException("Unable to read file: " + file);
 }
 return file;
}

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

/**
 * @param filePath the file path to be validated
 * @return Returns null if valid otherwise error message
 */
public static String validateFileInput(String filePath) {
  File file = new File(filePath);
  if (!file.exists()) {
    return "File '" + file.getAbsolutePath() + "' does not exist.";
  }
  if (!file.canRead()) {
    return "Read permission is denied on the file '" + file.getAbsolutePath() + "'";
  }
  if (file.isDirectory()) {
    return "'" + file.getAbsolutePath() + "' is a direcory. it must be a file.";
  }
  return null;
}

代码示例来源:origin: rubenlagus/TelegramBots

private static void validateServerKeystoreFile(String keyStore) throws TelegramApiRequestException {
    File file = new File(keyStore);
    if (!file.exists() || !file.canRead()) {
      throw new TelegramApiRequestException("Can't find or access server keystore file.");
    }
  }
}

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

PersistFS(File root) {
 _root = root;
 _dir = new File(root, "ice" + H2O.API_PORT);
 // Make the directory as-needed
 root.mkdirs();
 if( !(root.isDirectory() && root.canRead() && root.canWrite()) ) {
  Log.die("ice_root not a read/writable directory");
 }
}

代码示例来源:origin: ming1016/study

static boolean isReadableFile(String path) {
  File f = new File(path);
  return f.canRead() && f.isFile();
}

相关文章