本文整理汇总了Java中java.nio.file.attribute.DosFileAttributes类的典型用法代码示例。如果您正苦于以下问题:Java DosFileAttributes类的具体用法?Java DosFileAttributes怎么用?Java DosFileAttributes使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
DosFileAttributes类属于java.nio.file.attribute包,在下文中一共展示了DosFileAttributes类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: toString
import java.nio.file.attribute.DosFileAttributes; //导入依赖的package包/类
public static String toString(DosFileAttributes dosFileAttributes) {
StringBuilder builder = new StringBuilder();
if (dosFileAttributes.isArchive()) {
builder.append('A');
}
if (dosFileAttributes.isHidden()) {
builder.append('H');
}
if (dosFileAttributes.isReadOnly()) {
builder.append('R');
}
if (dosFileAttributes.isSystem()) {
builder.append('S');
}
return builder.toString();
}
示例2: resetFileAttributes
import java.nio.file.attribute.DosFileAttributes; //导入依赖的package包/类
private int resetFileAttributes(Context context, FileState fileState, Path file) throws IOException {
boolean attributesModified = false;
try {
BasicFileAttributes attributes;
if (SystemUtils.IS_OS_WINDOWS) {
DosFileAttributes dosFileAttributes = Files.readAttributes(file, DosFileAttributes.class);
attributes = dosFileAttributes;
attributesModified = resetDosPermissions(context, file, fileState, dosFileAttributes) || attributesModified;
} else {
PosixFileAttributes posixFileAttributes = Files.readAttributes(file, PosixFileAttributes.class);
attributes = posixFileAttributes;
attributesModified = resetPosixPermissions(file, fileState, posixFileAttributes) || attributesModified;
}
attributesModified = resetCreationTime(file, fileState, attributes) || attributesModified;
attributesModified = resetLastModified(file, fileState, attributes) || attributesModified;
attributesModified = resetSELinux(context, file, fileState) || attributesModified;
} catch (Exception ex) {
Logger.error(ex.getMessage());
}
return attributesModified ? 1 : 0;
}
示例3: canRetrieveTheStringVersion
import java.nio.file.attribute.DosFileAttributes; //导入依赖的package包/类
@Test
public void canRetrieveTheStringVersion() {
DosFileAttributes dosFileAttributes = mock(DosFileAttributes.class);
assertThat(DosFilePermissions.toString(dosFileAttributes)).isEqualTo("");
Mockito.when(dosFileAttributes.isArchive()).thenReturn(true);
assertThat(DosFilePermissions.toString(dosFileAttributes)).isEqualTo("A");
Mockito.when(dosFileAttributes.isHidden()).thenReturn(true);
assertThat(DosFilePermissions.toString(dosFileAttributes)).isEqualTo("AH");
Mockito.when(dosFileAttributes.isReadOnly()).thenReturn(true);
assertThat(DosFilePermissions.toString(dosFileAttributes)).isEqualTo("AHR");
Mockito.when(dosFileAttributes.isSystem()).thenReturn(true);
assertThat(DosFilePermissions.toString(dosFileAttributes)).isEqualTo("AHRS");
}
示例4: testSetGetSystem
import java.nio.file.attribute.DosFileAttributes; //导入依赖的package包/类
@Test
public void testSetGetSystem() throws Exception {
for(Path path : Arrays.asList(
Files.createDirectory(root.resolve("dir")),
Files.createFile(root.resolve("file")))) {
DosFileAttributes attributes = Files.readAttributes(path, DosFileAttributes.class);
assertFalse(attributes.isSystem());
DosFileAttributeView view = Files.getFileAttributeView(path, DosFileAttributeView.class);
view.setSystem(true);
attributes = Files.readAttributes(path, DosFileAttributes.class);
assertTrue(attributes.isSystem());
}
}
示例5: testSetGetArchiveUnix
import java.nio.file.attribute.DosFileAttributes; //导入依赖的package包/类
@IgnoreIf(FsType.WINDOWS)
@Test
public void testSetGetArchiveUnix() throws Exception {
for(Path path : Arrays.asList(
Files.createDirectory(root.resolve("dir")),
Files.createFile(root.resolve("file")))) {
DosFileAttributes attributes = Files.readAttributes(path, DosFileAttributes.class);
assertFalse(attributes.isArchive());
DosFileAttributeView view = Files.getFileAttributeView(path, DosFileAttributeView.class);
view.setArchive(true);
attributes = Files.readAttributes(path, DosFileAttributes.class);
assertTrue(attributes.isArchive());
}
}
示例6: testSetGetReadOnly
import java.nio.file.attribute.DosFileAttributes; //导入依赖的package包/类
@Test
public void testSetGetReadOnly() throws Exception {
for(Path path : Arrays.asList(
Files.createDirectory(root.resolve("dir")),
Files.createFile(root.resolve("file")))) {
DosFileAttributes attributes = Files.readAttributes(path, DosFileAttributes.class);
assertFalse(attributes.isReadOnly());
DosFileAttributeView view = Files.getFileAttributeView(path, DosFileAttributeView.class);
try {
view.setReadOnly(true);
attributes = Files.readAttributes(path, DosFileAttributes.class);
assertTrue(attributes.isReadOnly());
} finally {
view.setReadOnly(false);
}
}
}
示例7: testSetGetHidden
import java.nio.file.attribute.DosFileAttributes; //导入依赖的package包/类
@Test
public void testSetGetHidden() throws Exception {
for(Path path : Arrays.asList(
Files.createDirectory(root.resolve("dir")),
Files.createFile(root.resolve("file")))) {
DosFileAttributes attributes = Files.readAttributes(path, DosFileAttributes.class);
assertFalse(attributes.isHidden());
DosFileAttributeView view = Files.getFileAttributeView(path, DosFileAttributeView.class);
view.setHidden(true);
attributes = Files.readAttributes(path, DosFileAttributes.class);
assertTrue(attributes.isHidden());
}
}
示例8: getDosAttributesString
import java.nio.file.attribute.DosFileAttributes; //导入依赖的package包/类
/**
* @param path
* @return
*/
public static String getDosAttributesString(Path path) {
DosFileAttributeView basicView = Files.getFileAttributeView(path, DosFileAttributeView.class,
LinkOption.NOFOLLOW_LINKS);
StringBuilder attrs = new StringBuilder();
try {
// + all basic attributes
DosFileAttributes dosAttrs = basicView.readAttributes();
attrs.append(dosAttrs.isReadOnly() ? "r" : "-"); //$NON-NLS-1$ //$NON-NLS-2$
attrs.append(dosAttrs.isHidden() ? "h" : "-");//$NON-NLS-1$ //$NON-NLS-2$
attrs.append(dosAttrs.isArchive() ? "a" : "-");//$NON-NLS-1$ //$NON-NLS-2$
attrs.append(dosAttrs.isSystem() ? "s" : "-");//$NON-NLS-1$ //$NON-NLS-2$
}
catch (IOException e) {
log.warn("unable to read DOS attributes.", e); //$NON-NLS-1$
}
return attrs.toString();
}
示例9: isHidden
import java.nio.file.attribute.DosFileAttributes; //导入依赖的package包/类
@Override
public boolean isHidden(Path path) throws IOException {
// TODO(cgdecker): This should probably be configurable, but this seems fine for now
/*
* If the DOS view is supported, use the Windows isHidden method (check the dos:hidden
* attribute). Otherwise, use the Unix isHidden method (just check if the file name starts with
* ".").
*/
JimfsPath checkedPath = checkPath(path);
FileSystemView view = getDefaultView(checkedPath);
if (getFileStore(path).supportsFileAttributeView("dos")) {
return view
.readAttributes(checkedPath, DosFileAttributes.class, Options.NOFOLLOW_LINKS)
.isHidden();
}
return path.getNameCount() > 0
&& path.getFileName().toString().startsWith(".");
}
示例10: testAttributes
import java.nio.file.attribute.DosFileAttributes; //导入依赖的package包/类
@Test
public void testAttributes() {
DosFileAttributes attrs = provider.readAttributes(file);
assertThat(attrs.isHidden()).isFalse();
assertThat(attrs.isArchive()).isFalse();
assertThat(attrs.isReadOnly()).isFalse();
assertThat(attrs.isSystem()).isFalse();
file.setAttribute("dos", "hidden", true);
attrs = provider.readAttributes(file);
assertThat(attrs.isHidden()).isTrue();
assertThat(attrs.isArchive()).isFalse();
assertThat(attrs.isReadOnly()).isFalse();
assertThat(attrs.isSystem()).isFalse();
}
示例11: convert
import java.nio.file.attribute.DosFileAttributes; //导入依赖的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;
}
示例12: getFileMetadata
import java.nio.file.attribute.DosFileAttributes; //导入依赖的package包/类
/**
*
* @param fileLoc
* @return
*/
public static Map<String, String> getFileMetadata(Path fileLoc) {
Map<String, String> linkedHashMap = new LinkedHashMap<>();
try {
DosFileAttributes dosFileAttr = Files.readAttributes(fileLoc, DosFileAttributes.class);
linkedHashMap.put("File Name", fileLoc.getFileName().toString());
linkedHashMap.put("File Location", fileLoc.toString());
linkedHashMap.put("File Size", readableFileSize(dosFileAttr.size()));
linkedHashMap.put("Creation Time", getDateTime(dosFileAttr.creationTime()));
linkedHashMap.put("Last Accessed Time", getDateTime(dosFileAttr.lastAccessTime()));
linkedHashMap.put("Last Modified Time", getDateTime(dosFileAttr.lastModifiedTime()));
linkedHashMap.put("Is Directory?", dosFileAttr.isDirectory() ? "True" : "False");
linkedHashMap.put("Is Regular File?", dosFileAttr.isRegularFile() ? "True" : "False");
linkedHashMap.put("Is Symbolic Link?", dosFileAttr.isSymbolicLink() ? "True" : "False");
linkedHashMap.put("Is Archive?", dosFileAttr.isArchive() ? "True" : "False");
linkedHashMap.put("Is Hidden File?", dosFileAttr.isHidden() ? "True" : "False");
linkedHashMap.put("Is ReadOnly?", dosFileAttr.isReadOnly() ? "True" : "False");
linkedHashMap.put("Is System File?", dosFileAttr.isSystem() ? "True" : "False");
if (getOsName().equals("Linux")) {
PosixFileAttributes attr = Files.readAttributes(fileLoc, PosixFileAttributes.class);
String posixPerm = String.format("%s %s %s%n", attr.owner().getName(), attr.group().getName(),
PosixFilePermissions.toString(attr.permissions()));
linkedHashMap.put("Posix", posixPerm);
}
} catch (UnsupportedOperationException | IOException ex) {
System.err.println(ex.getMessage());
}
return linkedHashMap;
}
示例13: hashFilesInQueue
import java.nio.file.attribute.DosFileAttributes; //导入依赖的package包/类
private void hashFilesInQueue() throws InterruptedException {
Path file;
while ((file = filesToHashQueue.poll(100, TimeUnit.MILLISECONDS)) != null) {
try {
BasicFileAttributes attributes;
List<Attribute> fileAttributes = null;
if (SystemUtils.IS_OS_WINDOWS) {
DosFileAttributes dosFileAttributes = Files.readAttributes(file, DosFileAttributes.class);
fileAttributes = addAttribute(fileAttributes, FileAttribute.DosFilePermissions, DosFilePermissions.toString(dosFileAttributes));
attributes = dosFileAttributes;
} else {
PosixFileAttributes posixFileAttributes = Files.readAttributes(file, PosixFileAttributes.class);
fileAttributes = addAttribute(fileAttributes, FileAttribute.PosixFilePermissions, PosixFilePermissions.toString(posixFileAttributes.permissions()));
if (SELinux.ENABLED) {
fileAttributes = addAttribute(fileAttributes, FileAttribute.SELinuxLabel, SELinux.getLabel(context, file));
}
attributes = posixFileAttributes;
}
hashProgress.updateOutput(attributes.size());
FileHash fileHash = hashFile(file, attributes.size());
String normalizedFileName = FileUtil.getNormalizedFileName(file);
String relativeFileName = FileUtil.getRelativeFileName(rootDir, normalizedFileName);
fileStates.add(new FileState(relativeFileName, attributes, fileHash, fileAttributes));
} catch (Exception ex) {
Logger.newLine();
Logger.error("Skipping - Error hashing file '" + file + "'", ex, context.isDisplayStackTrace());
}
}
}
示例14: resetDosPermissions
import java.nio.file.attribute.DosFileAttributes; //导入依赖的package包/类
private boolean resetDosPermissions(Context context, Path file, FileState fileState, DosFileAttributes dosFileAttributes) {
String permissions = DosFilePermissions.toString(dosFileAttributes);
String previousPermissions = getAttribute(fileState, FileAttribute.DosFilePermissions);
if (previousPermissions != null && !Objects.equals(permissions, previousPermissions)) {
DosFilePermissions.setPermissions(context, file, previousPermissions);
Logger.out.printf("Set permissions: %s \t%s -> %s%n", fileState.getFileName(), permissions, previousPermissions);
return true;
}
return false;
}
示例15: openFolder
import java.nio.file.attribute.DosFileAttributes; //导入依赖的package包/类
/**
* open a folder and return it's contents
*
* @param folder
* @return
*/
public ArrayList<FSItem> openFolder(FSItem folder) {
ArrayList<FSItem> contents = new ArrayList<>();
try {
DirectoryStream<Path> directoryStream = Files.newDirectoryStream(Paths.get(folder.getFsItemPath()));
for (Path path : directoryStream) {
DosFileAttributes attrs = Files.readAttributes(path, DosFileAttributes.class);
FSItemProperties itemProperties = new FSItemProperties();
FSIcon itemIcon = new FSIcon();
FileSystemView fsv = FileSystemView.getFileSystemView();
ImageIcon icon = (ImageIcon) fsv.getSystemIcon(path.toFile());
itemIcon.setIconHeight((byte) icon.getIconHeight());
itemIcon.setIconWidth((byte) icon.getIconWidth());
itemIcon.setIconImage(this.createImage(icon.getImage()));
itemProperties.setItemCreationDate(attrs.creationTime().toString());
itemProperties.setItemLastAccessDate(attrs.lastAccessTime().toString());
itemProperties.setItemName(path.getFileName().toString());
itemProperties.setItemSize(attrs.size());
itemProperties.setHidden(attrs.isHidden());
FSItem item = new FSItem(path.toString());
item.setItemProperties(itemProperties);
item.setIcon(itemIcon);
contents.add(item);
}
return contents;
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
}