本文整理匯總了Java中java.nio.file.Files.getFileAttributeView方法的典型用法代碼示例。如果您正苦於以下問題:Java Files.getFileAttributeView方法的具體用法?Java Files.getFileAttributeView怎麽用?Java Files.getFileAttributeView使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.nio.file.Files
的用法示例。
在下文中一共展示了Files.getFileAttributeView方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getInode
import java.nio.file.Files; //導入方法依賴的package包/類
private long getInode(File file) throws IOException {
UserDefinedFileAttributeView view = null;
// windows system and file customer Attribute
if (OS_WINDOWS.equals(os)) {
view = Files.getFileAttributeView(file.toPath(), UserDefinedFileAttributeView.class);// 把文件的內容屬性值放置在view裏麵?
try {
ByteBuffer buffer = ByteBuffer.allocate(view.size(INODE));// view.size得到inode屬性值大小
view.read(INODE, buffer);// 把屬性值放置在buffer中
buffer.flip();
return Long.parseLong(Charset.defaultCharset().decode(buffer).toString());// 返回編碼後的inode的屬性值
}
catch (NoSuchFileException e) {
long winode = random.nextLong();
view.write(INODE, Charset.defaultCharset().encode(String.valueOf(winode)));
return winode;
}
}
long inode = (long) Files.getAttribute(file.toPath(), "unix:ino");// 返回unix的inode的屬性值
return inode;
}
示例2: makeFileNonReadable
import java.nio.file.Files; //導入方法依賴的package包/類
private static void makeFileNonReadable(String file) throws IOException {
Path filePath = Paths.get(file);
Set<String> supportedAttr = filePath.getFileSystem().supportedFileAttributeViews();
if (supportedAttr.contains("posix")) {
Files.setPosixFilePermissions(filePath, PosixFilePermissions.fromString("-w--w----"));
} else if (supportedAttr.contains("acl")) {
UserPrincipal fileOwner = Files.getOwner(filePath);
AclFileAttributeView view = Files.getFileAttributeView(filePath, AclFileAttributeView.class);
AclEntry entry = AclEntry.newBuilder()
.setType(AclEntryType.DENY)
.setPrincipal(fileOwner)
.setPermissions(AclEntryPermission.READ_DATA)
.build();
List<AclEntry> acl = view.getAcl();
acl.add(0, entry);
view.setAcl(acl);
}
}
示例3: testWritingKeepsFileAttributes
import java.nio.file.Files; //導入方法依賴的package包/類
/**
* Test for bug 240953 - Netbeans Deletes User Defined Attributes.
*
* @throws java.io.IOException
*/
public void testWritingKeepsFileAttributes() throws IOException {
final String attName = "User_Attribute";
final String attValue = "User_Attribute_Value";
if (Utilities.isWindows()) {
clearWorkDir();
File f = new File(getWorkDir(), "fileWithAtts.txt");
f.createNewFile();
UserDefinedFileAttributeView attsView = Files.getFileAttributeView(
f.toPath(), UserDefinedFileAttributeView.class);
ByteBuffer buffer = Charset.defaultCharset().encode(attValue);
attsView.write(attName, buffer);
buffer.rewind();
attsView.read(attName, buffer);
buffer.flip();
String val = Charset.defaultCharset().decode(buffer).toString();
assertEquals(attValue, val);
FileObject fob = FileUtil.toFileObject(f);
OutputStream os = fob.getOutputStream();
try {
os.write(55);
} finally {
os.close();
}
buffer.rewind();
attsView.read(attName, buffer);
buffer.flip();
String val2 = Charset.defaultCharset().decode(buffer).toString();
assertEquals(attValue, val2);
}
}
示例4: getCustomerAttr
import java.nio.file.Files; //導入方法依賴的package包/類
@Test
public void getCustomerAttr() throws IOException {
Path target = Paths.get("/Users/fathead/temp/file4");
UserDefinedFileAttributeView view2 = Files.getFileAttributeView(target, UserDefinedFileAttributeView.class);
ByteBuffer buf = ByteBuffer.allocate(view2.size(name));
view2.read(name, buf);
buf.flip();
String value = Charset.defaultCharset().decode(buf).toString();
System.out.println("value=" + value);
}
示例5: save
import java.nio.file.Files; //導入方法依賴的package包/類
/** Write the keystore to the given config directory. */
void save(Path configDir) throws Exception {
char[] password = this.keystorePassword.get().getPassword();
SimpleFSDirectory directory = new SimpleFSDirectory(configDir);
// write to tmp file first, then overwrite
String tmpFile = KEYSTORE_FILENAME + ".tmp";
try (IndexOutput output = directory.createOutput(tmpFile, IOContext.DEFAULT)) {
CodecUtil.writeHeader(output, KEYSTORE_FILENAME, FORMAT_VERSION);
output.writeByte(password.length == 0 ? (byte)0 : (byte)1);
output.writeString(type);
output.writeString(secretFactory.getAlgorithm());
ByteArrayOutputStream keystoreBytesStream = new ByteArrayOutputStream();
keystore.get().store(keystoreBytesStream, password);
byte[] keystoreBytes = keystoreBytesStream.toByteArray();
output.writeInt(keystoreBytes.length);
output.writeBytes(keystoreBytes, keystoreBytes.length);
CodecUtil.writeFooter(output);
}
Path keystoreFile = keystorePath(configDir);
Files.move(configDir.resolve(tmpFile), keystoreFile, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE);
PosixFileAttributeView attrs = Files.getFileAttributeView(keystoreFile, PosixFileAttributeView.class);
if (attrs != null) {
// don't rely on umask: ensure the keystore has minimal permissions
attrs.setPermissions(PosixFilePermissions.fromString("rw-------"));
}
}
示例6: installConfig
import java.nio.file.Files; //導入方法依賴的package包/類
/**
* Copies the files from {@code tmpConfigDir} into {@code destConfigDir}.
* Any files existing in both the source and destination will be skipped.
*/
private void installConfig(PluginInfo info, Path tmpConfigDir, Path destConfigDir) throws Exception {
if (Files.isDirectory(tmpConfigDir) == false) {
throw new UserException(ExitCodes.IO_ERROR, "config in plugin " + info.getName() + " is not a directory");
}
Files.createDirectories(destConfigDir);
setFileAttributes(destConfigDir, CONFIG_DIR_PERMS);
final PosixFileAttributeView destConfigDirAttributesView =
Files.getFileAttributeView(destConfigDir.getParent(), PosixFileAttributeView.class);
final PosixFileAttributes destConfigDirAttributes =
destConfigDirAttributesView != null ? destConfigDirAttributesView.readAttributes() : null;
if (destConfigDirAttributes != null) {
setOwnerGroup(destConfigDir, destConfigDirAttributes);
}
try (DirectoryStream<Path> stream = Files.newDirectoryStream(tmpConfigDir)) {
for (Path srcFile : stream) {
if (Files.isDirectory(srcFile)) {
throw new UserException(ExitCodes.DATA_ERROR, "Directories not allowed in config dir for plugin " + info.getName());
}
Path destFile = destConfigDir.resolve(tmpConfigDir.relativize(srcFile));
if (Files.exists(destFile) == false) {
Files.copy(srcFile, destFile);
setFileAttributes(destFile, CONFIG_FILES_PERMS);
if (destConfigDirAttributes != null) {
setOwnerGroup(destFile, destConfigDirAttributes);
}
}
}
}
IOUtils.rm(tmpConfigDir); // clean up what we just copied
}
示例7: setFileAttributes
import java.nio.file.Files; //導入方法依賴的package包/類
/**
* Sets the attributes for a path iff posix attributes are supported
*/
private static void setFileAttributes(final Path path, final Set<PosixFilePermission> permissions) throws IOException {
PosixFileAttributeView fileAttributeView = Files.getFileAttributeView(path, PosixFileAttributeView.class);
if (fileAttributeView != null) {
Files.setPosixFilePermissions(path, permissions);
}
}
示例8: setExecutable
import java.nio.file.Files; //導入方法依賴的package包/類
public static void setExecutable ( final Path path, final boolean state ) throws IOException
{
final UserDefinedFileAttributeView ua = Files.getFileAttributeView ( path, UserDefinedFileAttributeView.class );
if ( state )
{
ua.write ( ATTR_EXECUTE, ByteBuffer.wrap ( marker ) );
}
else
{
ua.delete ( ATTR_EXECUTE );
}
}
示例9: getExecutable
import java.nio.file.Files; //導入方法依賴的package包/類
public static boolean getExecutable ( final Path path ) throws IOException
{
final UserDefinedFileAttributeView ua = Files.getFileAttributeView ( path, UserDefinedFileAttributeView.class );
if ( !ua.list ().contains ( ATTR_EXECUTE ) )
{
// check first, otherwise the size() call with give an exception
return false;
}
final ByteBuffer buf = ByteBuffer.allocate ( ua.size ( ATTR_EXECUTE ) );
ua.read ( ATTR_EXECUTE, buf );
buf.flip ();
return Boolean.parseBoolean ( CHARSET.decode ( buf ).toString () );
}
示例10: chmod
import java.nio.file.Files; //導入方法依賴的package包/類
private static void chmod(final Path file, final String perms) throws IOException {
final PosixFileAttributeView view =
Files.getFileAttributeView(file, PosixFileAttributeView.class);
if (view == null) {
// OS (Windows) doesn't support POSIX file attributes
return;
}
view.setPermissions(PosixFilePermissions.fromString("rwxr-xr-x"));
}
示例11: removeReadOnly
import java.nio.file.Files; //導入方法依賴的package包/類
public static void removeReadOnly(Path path) throws IOException {
DosFileAttributeView att = Files.getFileAttributeView(path, DosFileAttributeView.class);
if (att != null) {
att.setReadOnly(false);
}
Files.delete(path);
}
示例12: getFileAttributeView
import java.nio.file.Files; //導入方法依賴的package包/類
@Override
public <V extends FileAttributeView> V getFileAttributeView(Path file,
Class<V> type,
LinkOption... options)
{
return Files.getFileAttributeView(unwrap(file), type, options);
}
示例13: setCustomerAttr
import java.nio.file.Files; //導入方法依賴的package包/類
@Test
public void setCustomerAttr() throws IOException {
Path target = Paths.get("/Users/fathead/temp/file4");
UserDefinedFileAttributeView view = Files.getFileAttributeView(target, UserDefinedFileAttributeView.class);
view.write(name, Charset.defaultCharset().encode("pinelet"));
}
示例14: chmod
import java.nio.file.Files; //導入方法依賴的package包/類
@Override
public void chmod(File f, int mode) throws IOException {
PosixFileAttributeView fileAttributeView = Files.getFileAttributeView(f.toPath(), PosixFileAttributeView.class);
fileAttributeView.setPermissions(convertToPermissionsSet(mode));
}
示例15: setFileCreationDate
import java.nio.file.Files; //導入方法依賴的package包/類
public static void setFileCreationDate(String filePath, Date creationDate) throws IOException
{
BasicFileAttributeView attributes = Files.getFileAttributeView(Paths.get(filePath), BasicFileAttributeView.class);
FileTime time = FileTime.fromMillis(creationDate.getTime());
attributes.setTimes(time, time, time);
}