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

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

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

File.canWrite介绍

[英]Indicates whether the current context is allowed to write to this file.
[中]指示是否允许当前上下文写入此文件。

代码示例

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

private boolean directoryWritableAfterMkdirs(File directory) {
 boolean success = directory.mkdirs();
 return (success || directory.exists()) && directory.canWrite();
}

代码示例来源:origin: jeasonlzy/okhttp-OkGo

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

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

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

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

private static void validateCreateOutputDir(File dir) {
    if (!dir.exists()) {
      dir.mkdirs();
    }
    if (!dir.canWrite()) {
      throw new IllegalStateException(dir.getName() + " does not have write permissions.");
    }
    if (!dir.isDirectory()) {
      throw new IllegalStateException(dir.getName() + " is not a directory.");
    }
  }
}

代码示例来源:origin: bumptech/glide

@Override
 public File getCacheDirectory() {
  File internalCacheDirectory = getInternalCacheDirectory();
  // Already used internal cache, so keep using that one,
  // thus avoiding using both external and internal with transient errors.
  if ((null != internalCacheDirectory) && internalCacheDirectory.exists()) {
   return internalCacheDirectory;
  }
  File cacheDirectory = context.getExternalCacheDir();
  // Shared storage is not available.
  if ((cacheDirectory == null) || (!cacheDirectory.canWrite())) {
   return internalCacheDirectory;
  }
  if (diskCacheName != null) {
   return new File(cacheDirectory, diskCacheName);
  }
  return cacheDirectory;
 }
}, diskCacheSize);

代码示例来源:origin: yanzhenjie/NoHttp

public static boolean sdCardIsAvailable() {
  if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
    File sd = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
    return sd.canWrite();
  } else return false;
}

代码示例来源:origin: scouter-project/scouter

private static synchronized void openFile(String prefix) throws IOException {
  if (pw == null && StringUtil.isEmpty(conf.log_dir) == false) {
    File root = new File(conf.log_dir);
    if (root.canWrite() == false) {
      root.mkdirs();
    }
    if (root.canWrite() == false) {
      return;
    }
    if (conf.log_rotation_enabled) {
      File file = new File(conf.log_dir, "scouter-" + prefix + "-" + DateUtil.yyyymmdd() + ".log");
      FileWriter fw = new FileWriter(file, true);
      pw = new PrintWriter(fw);
      logfile = file;
    } else {
      File file = new File(conf.log_dir, "scouter-" + prefix + ".log");
      pw = new PrintWriter(new FileWriter(file, true));
      logfile = file;
    }
  }
}

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

static SrcKind get(File f) {
  if (f.exists() && f.isDirectory() && f.canWrite()) {
    return DIR;
  }
  if (!f.exists()) {
    if (f.getName().endsWith(".zip")) {
      return ZIP;
    }
    if (f.getName().endsWith(".z0p.gz")) {
      return Z0P_GZ;
    }
  }
  throw new IllegalArgumentException("Invalid src destination: " + f);
}

代码示例来源:origin: commons-io/commons-io

/**
 * Tests that we can write to the lock directory.
 *
 * @param lockDir  the File representing the lock directory
 * @throws IOException if we cannot write to the lock directory
 * @throws IOException if we cannot find the lock file
 */
private void testLockDir(final File lockDir) throws IOException {
  if (!lockDir.exists()) {
    throw new IOException(
        "Could not find lockDir: " + lockDir.getAbsolutePath());
  }
  if (!lockDir.canWrite()) {
    throw new IOException(
        "Could not write to lockDir: " + lockDir.getAbsolutePath());
  }
}

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

public static File resolveOutputDirectory(ProcessingEnvironment processingEnv) throws FileNotFoundException {
  File rootProject = FileHelper.findRootProject(processingEnv);
  // Target folder - Maven
  File targetFolder = new File(rootProject, "target");
  if (targetFolder.isDirectory() && targetFolder.canWrite()) {
    return targetFolder;
  }
  // Build folder - Gradle
  File buildFolder = new File(rootProject, "build");
  if (buildFolder.isDirectory() && buildFolder.canWrite()) {
    return buildFolder;
  }
  // Bin folder - Eclipse
  File binFolder = new File(rootProject, "bin");
  if (binFolder.isDirectory() && binFolder.canWrite()) {
    return binFolder;
  }
  // Fallback to projet root folder
  return rootProject;
}

代码示例来源:origin: org.codehaus.plexus/plexus-utils

private static void checkCanWrite( File destination )
  throws IOException
{
  // make sure we can write to destination
  if ( destination.exists() && !destination.canWrite() )
  {
    final String message = "Unable to open file " + destination + " for writing.";
    throw new IOException( message );
  }
}

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

private static void validateCreateOutputDir(File dir) {
  if (!dir.exists()) {
    dir.mkdirs();
  }
  if (!dir.canWrite()) {
    throw new IllegalStateException(dir.getName() + " does not have write permissions.");
  }
  if (!dir.isDirectory()) {
    throw new IllegalStateException(dir.getName() + " is not a directory.");
  }
}

代码示例来源:origin: oldmanpushcart/greys-anatomy

private History initHistory() throws IOException {
  final File WORK_DIR = new File(WORKING_DIR);
  final File historyFile = new File(WORKING_DIR + separatorChar + HISTORY_FILENAME);
  if (WORK_DIR.canWrite()
      && WORK_DIR.canRead()
      && ((!historyFile.exists() && historyFile.createNewFile()) || historyFile.exists())) {
    return new FileHistory(historyFile);
  }
  return new MemoryHistory();
}

代码示例来源:origin: scouter-project/scouter

public static void main(String[] args) throws IOException {
  String path = getJarLocation(FileUtil.class);
  System.out.println(path);
  new File(path, "test.txt").createNewFile();
  System.out.println(new File(path).canWrite());
  System.out.println(new File(path).getAbsolutePath());
}

代码示例来源:origin: scouter-project/scouter

private static synchronized void openFile(String prefix) throws IOException {
  if (pw == null && StringUtil.isEmpty(conf.log_dir) == false) {
    File root = new File(conf.log_dir);
    if (root.canWrite() == false) {
      root.mkdirs();
    }
    if (root.canWrite() == false) {
      return;
    }
    if (conf.log_rotation_enabled) {
      File file = new File(conf.log_dir, "scouter-" + prefix + "-" + DateUtil.yyyymmdd() + ".log");
      FileWriter fw = new FileWriter(file, true);
      pw = new PrintWriter(fw);
      logfile = file;
    } else {
      File file = new File(conf.log_dir, "scouter-" + prefix + ".log");
      pw = new PrintWriter(new FileWriter(file, true));
      logfile = file;
    }
  }
}

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

/**
 * Set the parent directory where the server's logs and snapshots will be kept.
 * @param dataDir the parent directory for the server's logs and snapshots; may be null if a temporary directory will be used
 * @return this instance to allow method chaining; never null
 * @throws IllegalArgumentException if the supplied file is not a directory or not writable
 */
public ZookeeperServer setStateDirectory(File dataDir) {
  if ( dataDir != null && dataDir.exists() && !dataDir.isDirectory() && !dataDir.canWrite() && !dataDir.canRead() ) {
    throw new IllegalArgumentException("The directory must be readable and writable");
  }
  this.dataDir = dataDir;
  return this;
}

代码示例来源:origin: org.apache.commons/commons-io

/**
 * Tests that we can write to the lock directory.
 *
 * @param lockDir  the File representing the lock directory
 * @throws IOException if we cannot write to the lock directory
 * @throws IOException if we cannot find the lock file
 */
private void testLockDir(File lockDir) throws IOException {
  if (!lockDir.exists()) {
    throw new IOException(
        "Could not find lockDir: " + lockDir.getAbsolutePath());
  }
  if (!lockDir.canWrite()) {
    throw new IOException(
        "Could not write to lockDir: " + lockDir.getAbsolutePath());
  }
}

代码示例来源:origin: yanzhenjie/NoHttp

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

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

/**
 * Initializes temporary directory path. Path consists of base path
 * (either {@link #tmpDirPath} value or {@code java.io.tmpdir}
 * system property value if first is {@code null}) and path relative
 * to base one - {@link #DEPLOY_TMP_ROOT_NAME}/{@code local node ID}.
 *
 * @throws org.apache.ignite.spi.IgniteSpiException Thrown if temporary directory could not be created.
 */
private void initializeTemporaryDirectoryPath() throws IgniteSpiException {
  String tmpDirPath = this.tmpDirPath == null ? System.getProperty("java.io.tmpdir") : this.tmpDirPath;
  if (tmpDirPath == null)
    throw new IgniteSpiException("Error initializing temporary deployment directory.");
  File dir = new File(tmpDirPath + File.separator + DEPLOY_TMP_ROOT_NAME + File.separator +
    ignite.configuration().getNodeId());
  if (!U.mkdirs(dir))
    throw new IgniteSpiException("Error initializing temporary deployment directory: " + dir);
  if (!dir.isDirectory())
    throw new IgniteSpiException("Temporary deployment directory path is not a valid directory: " + dir);
  if (!dir.canRead() || !dir.canWrite())
    throw new IgniteSpiException("Can not write to or read from temporary deployment directory: " + dir);
  this.tmpDirPath = tmpDirPath;
  deployTmpDirPath = dir.getPath();
}

代码示例来源:origin: hs-web/hsweb-framework

private static State valid(File file) {
    File parentPath = file.getParentFile();

    if ((!parentPath.exists()) && (!parentPath.mkdirs())) {
      return new BaseState(false, AppInfo.FAILED_CREATE_FILE);
    }

    if (!parentPath.canWrite()) {
      return new BaseState(false, AppInfo.PERMISSION_DENIED);
    }

    return new BaseState(true);
  }
}

相关文章