本文整理匯總了Java中java.nio.file.Files.getAttribute方法的典型用法代碼示例。如果您正苦於以下問題:Java Files.getAttribute方法的具體用法?Java Files.getAttribute怎麽用?Java Files.getAttribute使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.nio.file.Files
的用法示例。
在下文中一共展示了Files.getAttribute方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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: main
import java.nio.file.Files; //導入方法依賴的package包/類
public static void main(String[] args) throws IOException {
try (Scanner scanner = new Scanner(System.in)) {
System.out.println("Enter path to the directory you want to mirror:");
Path p = Paths.get(scanner.nextLine());
int uid = (int) Files.getAttribute(p, "unix:uid");
int gid = (int) Files.getAttribute(p, "unix:gid");
System.out.println("Enter mount point:");
Path m = Paths.get(scanner.nextLine());
if (Files.isDirectory(p) && Files.isDirectory(m)) {
try (FuseNioAdapter fs = AdapterFactory.createReadWriteAdapter(p)) {
fs.mount(m, false, false, new String[]{"-ouid="+uid, "-ogid="+gid, "-ovolname=FUSE-NIO-Adapter", "-oauto_xattr", "-oatomic_o_trunc"});
System.out.println("Mounted successfully. Enter anything to stop the server...");
System.in.read();
fs.umount();
System.out.println("Unmounted successfully. Exiting...");
} catch (Exception e) {
e.printStackTrace();
}
} else {
System.err.println("Invalid directory.");
}
}
}
示例3: main
import java.nio.file.Files; //導入方法依賴的package包/類
public static void main(String[] args) throws IOException {
try (Scanner scanner = new Scanner(System.in)) {
System.out.println("Enter path to the directory you want to mirror:");
Path p = Paths.get(scanner.nextLine());
int uid = (int) Files.getAttribute(p, "unix:uid");
int gid = (int) Files.getAttribute(p, "unix:gid");
System.out.println("Enter mount point:");
Path m = Paths.get(scanner.nextLine());
if (Files.isDirectory(p) && Files.isDirectory(m)) {
try (FuseNioAdapter fs = AdapterFactory.createReadWriteAdapter(p)) {
fs.mount(m, false, false, new String[]{"-ouid="+uid, "-ogid="+gid, "-oatomic_o_trunc"});
System.out.println("Mounted successfully. Enter anything to stop the server...");
System.in.read();
fs.umount();
System.out.println("Unmounted successfully. Exiting...");
} catch (Exception e) {
e.printStackTrace();
}
} else {
System.err.println("Invalid directory.");
}
}
}
示例4: getFileAttributes
import java.nio.file.Files; //導入方法依賴的package包/類
@Test
public void getFileAttributes() throws IOException {
Object obj = Files.getAttribute(new File("/Users/fathead/temp/file3").toPath(), "unix:ino");
System.out.println(obj);
}
示例5: getUserId
import java.nio.file.Files; //導入方法依賴的package包/類
/**
*
* @param filename the file name
* @return the file user id
* @throws FileSystemOperationException
*/
private long getUserId(
String filename ) {
try {
Integer uid = ( Integer ) Files.getAttribute( new File( filename ).toPath(), "unix:uid",
LinkOption.NOFOLLOW_LINKS );
return uid.longValue();
} catch( Exception e ) {
throw new FileSystemOperationException( "Could not get UID for '" + filename + "'", e );
}
}
示例6: getGroupId
import java.nio.file.Files; //導入方法依賴的package包/類
/**
*
* @param filename file name
* @return the file group id
* @throws FileSystemOperationException
*/
private long getGroupId(
String filename ) {
try {
Integer gid = ( Integer ) Files.getAttribute( new File( filename ).toPath(), "unix:gid",
LinkOption.NOFOLLOW_LINKS );
return gid.longValue();
} catch( Exception e ) {
throw new FileSystemOperationException( "Could not get GID for '" + filename + "'", e );
}
}
示例7: getInode
import java.nio.file.Files; //導入方法依賴的package包/類
private long getInode(File file) throws IOException {
long inode = (long) Files.getAttribute(file.toPath(), "unix:ino");
return inode;
}
示例8: loadAndCheck
import java.nio.file.Files; //導入方法依賴的package包/類
private int loadAndCheck(Path p, AuthTimeWithHash time,
KerberosTime currTime)
throws IOException, KrbApErrException {
int missed = 0;
if (Files.isSymbolicLink(p)) {
throw new IOException("Symlink not accepted");
}
try {
Set<PosixFilePermission> perms =
Files.getPosixFilePermissions(p);
if (uid != -1 &&
(Integer)Files.getAttribute(p, "unix:uid") != uid) {
throw new IOException("Not mine");
}
if (perms.contains(PosixFilePermission.GROUP_READ) ||
perms.contains(PosixFilePermission.GROUP_WRITE) ||
perms.contains(PosixFilePermission.GROUP_EXECUTE) ||
perms.contains(PosixFilePermission.OTHERS_READ) ||
perms.contains(PosixFilePermission.OTHERS_WRITE) ||
perms.contains(PosixFilePermission.OTHERS_EXECUTE)) {
throw new IOException("Accessible by someone else");
}
} catch (UnsupportedOperationException uoe) {
// No POSIX permissions? Ignore it.
}
chan = Files.newByteChannel(p, StandardOpenOption.WRITE,
StandardOpenOption.READ);
long timeLimit = currTime.getSeconds() - readHeader(chan);
long pos = 0;
boolean seeNewButNotSame = false;
while (true) {
try {
pos = chan.position();
AuthTime a = AuthTime.readFrom(chan);
if (a instanceof AuthTimeWithHash) {
if (time.equals(a)) {
// Exact match, must be a replay
throw new KrbApErrException(Krb5.KRB_AP_ERR_REPEAT);
} else if (time.isSameIgnoresHash(a)) {
// Two different authenticators in the same second.
// Remember it
seeNewButNotSame = true;
}
} else {
if (time.isSameIgnoresHash(a)) {
// Two authenticators in the same second. Considered
// same if we haven't seen a new style version of it
if (!seeNewButNotSame) {
throw new KrbApErrException(Krb5.KRB_AP_ERR_REPEAT);
}
}
}
if (a.ctime < timeLimit) {
missed++;
} else {
missed--;
}
} catch (BufferUnderflowException e) {
// Half-written file?
chan.position(pos);
break;
}
}
return missed;
}
示例9: loadAndCheck
import java.nio.file.Files; //導入方法依賴的package包/類
private int loadAndCheck(Path p, AuthTimeWithHash time,
KerberosTime currTime)
throws IOException, KrbApErrException {
int missed = 0;
if (Files.isSymbolicLink(p)) {
throw new IOException("Symlink not accepted");
}
try {
Set<PosixFilePermission> perms =
Files.getPosixFilePermissions(p);
if (uid != -1 &&
(Integer)Files.getAttribute(p, "unix:uid") != uid) {
throw new IOException("Not mine");
}
if (perms.contains(PosixFilePermission.GROUP_READ) ||
perms.contains(PosixFilePermission.GROUP_WRITE) ||
perms.contains(PosixFilePermission.GROUP_EXECUTE) ||
perms.contains(PosixFilePermission.OTHERS_READ) ||
perms.contains(PosixFilePermission.OTHERS_WRITE) ||
perms.contains(PosixFilePermission.OTHERS_EXECUTE)) {
throw new IOException("Accessible by someone else");
}
} catch (UnsupportedOperationException uoe) {
// No POSIX permissions? Ignore it.
}
chan = Files.newByteChannel(p, StandardOpenOption.WRITE,
StandardOpenOption.READ);
long timeLimit = currTime.getSeconds() - readHeader(chan);
long pos = 0;
boolean seeNewButNotSame = false;
while (true) {
try {
pos = chan.position();
AuthTime a = AuthTime.readFrom(chan);
if (a instanceof AuthTimeWithHash) {
if (time.equals(a)) {
// Exact match, must be a replay
throw new KrbApErrException(Krb5.KRB_AP_ERR_REPEAT);
} else if (time.sameTimeDiffHash((AuthTimeWithHash)a)) {
// Two different authenticators in the same second.
// Remember it
seeNewButNotSame = true;
}
} else {
if (time.isSameIgnoresHash(a)) {
// Two authenticators in the same second. Considered
// same if we haven't seen a new style version of it
if (!seeNewButNotSame) {
throw new KrbApErrException(Krb5.KRB_AP_ERR_REPEAT);
}
}
}
if (a.ctime < timeLimit) {
missed++;
} else {
missed--;
}
} catch (BufferUnderflowException e) {
// Half-written file?
chan.position(pos);
break;
}
}
return missed;
}