本文整理匯總了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");
}
}