本文整理匯總了Java中java.nio.file.Files.isWritable方法的典型用法代碼示例。如果您正苦於以下問題:Java Files.isWritable方法的具體用法?Java Files.isWritable怎麽用?Java Files.isWritable使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.nio.file.Files
的用法示例。
在下文中一共展示了Files.isWritable方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: createDirectory
import java.nio.file.Files; //導入方法依賴的package包/類
private static Path createDirectory(Path path) {
Path createdPath = path;
if (!Files.exists(path)) {
try {
Files.createDirectory(path);
if (!Files.isWritable(path)) {
createdPath = null;
}
}
catch (IOException e) {
String msg = "Unable to create a directory at " + path + ".";
LoggerFactory.getLogger(Initializer.class).error(msg, e);
createdPath = null;
}
}
return createdPath;
}
示例2: getPermissions
import java.nio.file.Files; //導入方法依賴的package包/類
/**
* Returns formatted permissions for current file.
* @param path Current file.
* @return Formatted permissions.
*/
private String getPermissions(Path path) {
char directory = '-';
if (Files.isDirectory(path))
directory = 'd';
char readable = '-';
if (Files.isReadable(path))
readable = 'r';
char writeable = '-';
if (Files.isWritable(path))
writeable = 'w';
char executable = '-';
if (Files.isExecutable(path))
executable = 'x';
return new StringBuilder().append(directory).append(readable)
.append(writeable).append(executable).toString();
}
示例3: checkWritePath
import java.nio.file.Files; //導入方法依賴的package包/類
/**
* Check if the provided path is a directory and is readable/writable/executable
*
* If the path doesn't exist, attempt to create a directory
*
* @param path the path to check
* @throws IOException if the directory cannot be created or is not accessible
*/
public static void checkWritePath(String path) throws IOException {
java.nio.file.Path npath = new File(path).toPath();
// Attempt to create the directory if it doesn't exist
if (Files.notExists(npath, LinkOption.NOFOLLOW_LINKS)) {
Files.createDirectories(npath);
return;
}
if (!Files.isDirectory(npath)) {
throw new IOException(format("path %s is not a directory.", npath));
}
if (!Files.isReadable(npath)) {
throw new IOException(format("path %s is not readable.", npath));
}
if (!Files.isWritable(npath)) {
throw new IOException(format("path %s is not writable.", npath));
}
if (!Files.isExecutable(npath)) {
throw new IOException(format("path %s is not executable.", npath));
}
}
示例4: performListing
import java.nio.file.Files; //導入方法依賴的package包/類
private Set<File> performListing(final File directory, final FileFilter filter, final boolean recurseSubdirectories) {
Path p = directory.toPath();
if (!Files.isWritable(p) || !Files.isReadable(p)) {
throw new IllegalStateException("Directory '" + directory + "' does not have sufficient permissions (i.e., not writable and readable)");
}
final Set<File> queue = new HashSet<>();
if (!directory.exists()) {
return queue;
}
final File[] children = directory.listFiles();
if (children == null) {
return queue;
}
for (final File child : children) {
if (child.isDirectory()) {
if (recurseSubdirectories) {
queue.addAll(performListing(child, filter, recurseSubdirectories));
}
} else if (filter.accept(child)) {
queue.add(child);
}
}
return queue;
}
示例5: performListing
import java.nio.file.Files; //導入方法依賴的package包/類
private Set<File> performListing(final File directory, final FileFilter filter, final boolean recurseSubdirectories) {
Path p = directory.toPath();
if (!Files.isWritable(p) || !Files.isReadable(p)) {
throw new IllegalStateException("Directory '" + directory + "' does not have sufficient permissions (i.e., not writable and readable)");
}
final Set<File> queue = new HashSet<File>();
if (!directory.exists()) {
return queue;
}
final File[] children = directory.listFiles();
if (children == null) {
return queue;
}
for (final File child : children) {
if (child.isDirectory()) {
if (recurseSubdirectories) {
queue.addAll(performListing(child, filter, recurseSubdirectories));
}
} else if (filter.accept(child)) {
queue.add(child);
}
}
return queue;
}
示例6: Solution
import java.nio.file.Files; //導入方法依賴的package包/類
public Solution(String pathToFile) {
try {
Path filePath = Paths.get(pathToFile);
fileData = new ConcreteFileData(Files.isHidden(filePath), Files.isExecutable(filePath),
Files.isDirectory(filePath), Files.isWritable(filePath));
} catch (Exception e) {
fileData = new NullFileData (e);
}
}
示例7: validateDumpDir
import java.nio.file.Files; //導入方法依賴的package包/類
private static void validateDumpDir(Path path) {
if (!Files.exists(path)) {
throw new IllegalArgumentException("Directory " + path + " does not exist");
} else if (!Files.isDirectory(path)) {
throw new IllegalArgumentException("Path " + path + " is not a directory");
} else if (!Files.isWritable(path)) {
throw new IllegalArgumentException("Directory " + path + " is not writable");
}
}
示例8: isParentWritable
import java.nio.file.Files; //導入方法依賴的package包/類
private boolean isParentWritable(Path path) {
Path parent = path.getParent();
if (parent == null) {
parent = path.toAbsolutePath().getParent();
}
return parent != null && Files.isWritable(parent);
}
示例9: storeDataSources
import java.nio.file.Files; //導入方法依賴的package包/類
public static void storeDataSources(List<DataSource> dataSourceList) {
Path path = Paths.get(dataSourcesStorage);
if (!Files.isWritable(path)) {
throw new IllegalStateException("data sources path must be writable");
}
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(path.toAbsolutePath().toString()))) {
for (DataSource dataSource : dataSourceList) {
oos.writeObject(dataSource);
}
oos.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
示例10: convert
import java.nio.file.Files; //導入方法依賴的package包/類
protected PathAttributes convert(final java.nio.file.Path file) throws IOException {
final boolean isPosix = session.isPosixFilesystem();
final PathAttributes attributes = new PathAttributes();
final Class<? extends BasicFileAttributes> provider = isPosix ? PosixFileAttributes.class : DosFileAttributes.class;
final BasicFileAttributes a = Files.readAttributes(file, provider, LinkOption.NOFOLLOW_LINKS);
if(Files.isRegularFile(file)) {
attributes.setSize(a.size());
}
attributes.setModificationDate(a.lastModifiedTime().toMillis());
attributes.setCreationDate(a.creationTime().toMillis());
attributes.setAccessedDate(a.lastAccessTime().toMillis());
if(isPosix) {
attributes.setOwner(((PosixFileAttributes) a).owner().getName());
attributes.setGroup(((PosixFileAttributes) a).group().getName());
attributes.setPermission(new Permission(PosixFilePermissions.toString(((PosixFileAttributes) a).permissions())));
}
else {
Permission.Action actions = Permission.Action.none;
if(Files.isReadable(file)) {
actions = actions.or(Permission.Action.read);
}
if(Files.isWritable(file)) {
actions = actions.or(Permission.Action.write);
}
if(Files.isExecutable(file)) {
actions = actions.or(Permission.Action.execute);
}
attributes.setPermission(new Permission(
actions, Permission.Action.none, Permission.Action.none
));
}
return attributes;
}
示例11: canWrite
import java.nio.file.Files; //導入方法依賴的package包/類
/** Return true if the file can be written. */
@Override
public boolean canWrite() {
return Files.isWritable(file);
}
示例12: createFileFilter
import java.nio.file.Files; //導入方法依賴的package包/類
private FileFilter createFileFilter(final ProcessContext context) {
final long minSize = context.getProperty(MIN_SIZE).asDataSize(DataUnit.B).longValue();
final Double maxSize = context.getProperty(MAX_SIZE).asDataSize(DataUnit.B);
final long minAge = context.getProperty(MIN_AGE).asTimePeriod(TimeUnit.MILLISECONDS);
final Long maxAge = context.getProperty(MAX_AGE).asTimePeriod(TimeUnit.MILLISECONDS);
final boolean ignoreHidden = context.getProperty(IGNORE_HIDDEN_FILES).asBoolean();
final Pattern filePattern = Pattern.compile(context.getProperty(FILE_FILTER).getValue());
final String indir = context.getProperty(DIRECTORY).evaluateAttributeExpressions().getValue();
final boolean recurseDirs = context.getProperty(RECURSE).asBoolean();
final String pathPatternStr = context.getProperty(PATH_FILTER).getValue();
final Pattern pathPattern = (!recurseDirs || pathPatternStr == null) ? null : Pattern.compile(pathPatternStr);
final boolean keepOriginal = context.getProperty(KEEP_SOURCE_FILE).asBoolean();
return new FileFilter() {
@Override
public boolean accept(final File file) {
if (minSize > file.length()) {
return false;
}
if (maxSize != null && maxSize < file.length()) {
return false;
}
final long fileAge = System.currentTimeMillis() - file.lastModified();
if (minAge > fileAge) {
return false;
}
if (maxAge != null && maxAge < fileAge) {
return false;
}
if (ignoreHidden && file.isHidden()) {
return false;
}
if (pathPattern != null) {
Path reldir = Paths.get(indir).relativize(file.toPath()).getParent();
if (reldir != null && !reldir.toString().isEmpty()) {
if (!pathPattern.matcher(reldir.toString()).matches()) {
return false;
}
}
}
//Verify that we have at least read permissions on the file we're considering grabbing
if (!Files.isReadable(file.toPath())) {
return false;
}
//Verify that if we're not keeping original that we have write permissions on the directory the file is in
if (keepOriginal == false && !Files.isWritable(file.toPath().getParent())) {
return false;
}
return filePattern.matcher(file.getName()).matches();
}
};
}
示例13: setup
import java.nio.file.Files; //導入方法依賴的package包/類
/**
* Setup all the files and directories needed for the tests
*
* @return writable directory created that needs to be deleted when done
* @throws RuntimeException
*/
private static File setup() throws RuntimeException {
// First do some setup in the temporary directory (using same logic as
// FileHandler for %t pattern)
String tmpDir = System.getProperty("java.io.tmpdir"); // i.e. %t
if (tmpDir == null) {
tmpDir = System.getProperty("user.home");
}
File tmpOrHomeDir = new File(tmpDir);
// Create a writable directory here (%t/writable-dir)
File writableDir = new File(tmpOrHomeDir, WRITABLE_DIR);
if (!createFile(writableDir, true)) {
throw new RuntimeException("Test setup failed: unable to create"
+ " writable working directory "
+ writableDir.getAbsolutePath() );
}
// writableDirectory and its contents will be deleted after the test
// that uses it
// Create a plain file which we will attempt to use as a directory
// (%t/not-a-dir)
File notAdir = new File(tmpOrHomeDir, NOT_A_DIR);
if (!createFile(notAdir, false)) {
throw new RuntimeException("Test setup failed: unable to a plain"
+ " working file " + notAdir.getAbsolutePath() );
}
notAdir.deleteOnExit();
// Create a non-writable directory (%t/non-writable-dir)
File nonWritableDir = new File(tmpOrHomeDir, NON_WRITABLE_DIR);
if (!createFile(nonWritableDir, true)) {
throw new RuntimeException("Test setup failed: unable to create"
+ " a non-"
+ "writable working directory "
+ nonWritableDir.getAbsolutePath() );
}
nonWritableDir.deleteOnExit();
// make it non-writable
Path path = nonWritableDir.toPath();
final boolean nonWritable = nonWritableDir.setWritable(false);
final boolean isWritable = Files.isWritable(path);
if (nonWritable && !isWritable) {
runNonWritableDirTest = true;
System.out.println("Created non writable dir for "
+ getOwner(path) + " at: " + path.toString());
} else {
runNonWritableDirTest = false;
System.out.println( "Test Setup WARNING: unable to make"
+ " working directory " + nonWritableDir.getAbsolutePath()
+ "\n\t non-writable for " + getOwner(path)
+ " on platform " + System.getProperty("os.name"));
}
// make sure non-existent directory really doesn't exist
File nonExistentDir = new File(tmpOrHomeDir, NON_EXISTENT_DIR);
if (nonExistentDir.exists()) {
nonExistentDir.delete();
}
System.out.println("Setup completed - writableDir is: " + writableDir.getPath());
return writableDir;
}
示例14: genDotFile
import java.nio.file.Files; //導入方法依賴的package包/類
private GenDotFile genDotFile(Path dir) throws BadArgs {
if (Files.exists(dir) && (!Files.isDirectory(dir) || !Files.isWritable(dir))) {
throw new BadArgs("err.invalid.path", dir.toString());
}
return new GenDotFile(dir);
}
示例15: genModuleInfo
import java.nio.file.Files; //導入方法依賴的package包/類
private GenModuleInfo genModuleInfo(Path dir, boolean openModule) throws BadArgs {
if (Files.exists(dir) && (!Files.isDirectory(dir) || !Files.isWritable(dir))) {
throw new BadArgs("err.invalid.path", dir.toString());
}
return new GenModuleInfo(dir, openModule);
}