/** * 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)); }
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."); } } }
@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);
public static boolean sdCardIsAvailable() { if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { File sd = new File(Environment.getExternalStorageDirectory().getAbsolutePath()); return sd.canWrite(); } else return false; }
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; } } }
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); }
/** * 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()); } }
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; }
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."); } }
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(); }
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()); }
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; } } }
/** * 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; }
/** * 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()); } }
/** * 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(); }
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); } }