本文整理汇总了Java中java.nio.file.Files.setAttribute方法的典型用法代码示例。如果您正苦于以下问题:Java Files.setAttribute方法的具体用法?Java Files.setAttribute怎么用?Java Files.setAttribute使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.file.Files
的用法示例。
在下文中一共展示了Files.setAttribute方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import java.nio.file.Files; //导入方法依赖的package包/类
public static void main(String args[]) throws Exception {
try {
tmpFile = File.createTempFile("FileOpenTest", "suffix");
// Opening Writable Normal File..
test(true);
// Opening Writable Hidden File..
Files.setAttribute(tmpFile.toPath(), "dos:hidden", true);
test(false);
// Opening Read-Only Hidden File..
Files.setAttribute(tmpFile.toPath(), "dos:hidden", false);
tmpFile.setReadOnly();
test(false);
// Opening Read-Only Normal File..
Files.setAttribute(tmpFile.toPath(), "dos:hidden", true);
test(false);
} finally {
tmpFile.delete();
}
}
示例2: hideFile
import java.nio.file.Files; //导入方法依赖的package包/类
private static void hideFile(File file) {
String os = System.getProperty("os.name").toLowerCase();
try {
if (os.startsWith("windows")) {
Files.setAttribute(file.toPath(), "dos:hidden", true);
}
} catch (IOException e) {}
}
示例3: setAttribute
import java.nio.file.Files; //导入方法依赖的package包/类
@Override
public void setAttribute(Path file, String attribute, Object value, LinkOption... options)
throws IOException
{
triggerEx(file, "setAttribute");
Files.setAttribute(unwrap(file), attribute, value, options);
}
示例4: createFile
import java.nio.file.Files; //导入方法依赖的package包/类
/**
* 创建文件,如果目标存在则删除
*/
public static File createFile(String path, boolean isHidden) throws IOException {
File file = new File(path);
deleteIfExists(file);
file.createNewFile();
File newFile = new File(path);
newFile.createNewFile();
if (OsUtil.isWindows()) {
Files.setAttribute(newFile.toPath(), "dos:hidden", isHidden);
}
return file;
}
示例5: createDir
import java.nio.file.Files; //导入方法依赖的package包/类
/**
* 创建文件夹,如果目标存在则删除
*/
public static File createDir(String path, boolean isHidden) throws IOException {
File file = new File(path);
deleteIfExists(file);
File newFile = new File(path);
newFile.mkdir();
if (OsUtil.isWindows()) {
Files.setAttribute(newFile.toPath(), "dos:hidden", isHidden);
}
return file;
}
示例6: PathBuilder
import java.nio.file.Files; //导入方法依赖的package包/类
private PathBuilder() {
try {
String os = System.getProperty("os.name").toLowerCase();
if (os.contains("win")) {
path = Paths.get(System.getProperty("user.home"), "pgSqlBlocks");
Files.createDirectories(path);
Files.setAttribute(path, "dos:hidden", Boolean.TRUE, LinkOption.NOFOLLOW_LINKS);
} else {
path = Paths.get(System.getProperty("user.home"), ".pgSqlBlocks");
Files.createDirectories(path);
}
} catch (IOException e) {
LOG.error(String.format("Ошибка создания директории %s: %s", path, e.getMessage()));
}
}
示例7: setFileHiddenAttribute
import java.nio.file.Files; //导入方法依赖的package包/类
@Override
public void setFileHiddenAttribute(
String sourceFile,
boolean hidden ) {
sourceFile = IoUtils.normalizeFilePath(sourceFile, osType);
checkFileExistence(new File(sourceFile));
final String errMsg = "Could not " + (hidden
? "set"
: "unset")
+ " the hidden attribute of file '" + sourceFile + "'";
if (OperatingSystemType.getCurrentOsType().isWindows()) {
try {
Path path = Paths.get(sourceFile);
DosFileAttributes attr;
attr = Files.readAttributes(path, DosFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
boolean goHidden = attr.isHidden();
if (!hidden && goHidden) {
Files.setAttribute(path, "dos:hidden", false, LinkOption.NOFOLLOW_LINKS);
} else if (hidden && !goHidden) {
Files.setAttribute(path, "dos:hidden", true, LinkOption.NOFOLLOW_LINKS);
}
} catch (IOException e) {
throw new FileSystemOperationException(errMsg, e);
}
} else if (OperatingSystemType.getCurrentOsType().isUnix()) {
// a '.' prefix makes the file hidden
String filePath = IoUtils.getFilePath(sourceFile);
String fileName = IoUtils.getFileName(sourceFile);
if (hidden) {
if (fileName.startsWith(".")) {
log.warn("File '" + sourceFile + "' is already hidden. No changes are made!");
return;
} else {
fileName = "." + fileName;
}
} else {
if (!fileName.startsWith(".")) {
log.warn("File '" + sourceFile + "' is already NOT hidden. No changes are made!");
return;
} else {
fileName = fileName.substring(1);
}
}
renameFile(sourceFile, filePath + fileName, false);
} else {
throw new FileSystemOperationException(errMsg + ": Unknown OS type");
}
}