下面列出了java.nio.file.attribute.BasicFileAttributeView#readAttributes ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 获取文件创建时间
*
* @param srcPath 文件绝对路径
* @return 时间
*/
public static Date getCreateTime(String srcPath) {
Path path = Paths.get(srcPath);
BasicFileAttributeView basicview = Files.getFileAttributeView(path, BasicFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
BasicFileAttributes attr;
try {
attr = basicview.readAttributes();
Date createDate = new Date(attr.creationTime().toMillis());
return createDate;
} catch (Exception e) {
e.printStackTrace();
}
Calendar cal = Calendar.getInstance();
cal.set(1970, 0, 1, 0, 0, 0);
return cal.getTime();
}
/**
* 获取文件创建时间
*
* @param srcPath 文件绝对路径
*
* @return 时间
*/
public static Date getCreateTime(String srcPath) {
final Path path = Paths.get(srcPath);
final BasicFileAttributeView basicview = Files.getFileAttributeView(path, BasicFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
BasicFileAttributes attr;
try {
attr = basicview.readAttributes();
final Date createDate = new Date(attr.creationTime().toMillis());
return createDate;
} catch (Exception e) {
e.printStackTrace();
}
final Calendar cal = Calendar.getInstance();
cal.set(1970, 0, 1, 0, 0, 0);
return cal.getTime();
}
/**
* 获取文件创建时间
*
* @param srcPath 文件绝对路径
* @return 时间
*/
public static Date getCreateTime(String srcPath) {
Path path = Paths.get(srcPath);
BasicFileAttributeView basicview = Files.getFileAttributeView(path, BasicFileAttributeView.class,
LinkOption.NOFOLLOW_LINKS);
BasicFileAttributes attr;
try {
attr = basicview.readAttributes();
Date createDate = new Date(attr.creationTime().toMillis());
return createDate;
} catch (Exception e) {
e.printStackTrace();
}
Calendar cal = Calendar.getInstance();
cal.set(1970, 0, 1, 0, 0, 0);
return cal.getTime();
}
/**
* 输入文件路径,返回这个文件的创建时间
* @param filePath 要获取创建时间的文件的路径,绝对路径
* @return 此文件创建的时间
*/
public static Date getCreateTime(String filePath){
Path path=Paths.get(filePath);
BasicFileAttributeView basicview=Files.getFileAttributeView(path, BasicFileAttributeView.class,LinkOption.NOFOLLOW_LINKS );
BasicFileAttributes attr;
try {
attr = basicview.readAttributes();
Date createDate = new Date(attr.creationTime().toMillis());
return createDate;
} catch (Exception e) {
e.printStackTrace();
}
Calendar cal = Calendar.getInstance();
cal.set(1970, 0, 1, 0, 0, 0);
return cal.getTime();
}
/**
* Get POSIX attributes for file.
*
* @param file File.
* @return BasicFileAttributes.
*/
@Nullable public static BasicFileAttributes basicAttributes(File file) {
BasicFileAttributes attrs = null;
try {
BasicFileAttributeView view = Files.getFileAttributeView(file.toPath(), BasicFileAttributeView.class);
if (view != null)
attrs = view.readAttributes();
}
catch (IOException e) {
throw new IgfsException("Failed to read basic file attributes: " + file.getAbsolutePath(), e);
}
return attrs;
}
@Test
public void testGetFileAttributeViewReadAttributes() throws IOException {
addDirectory("/foo/bar");
SFTPFileSystemProvider provider = new SFTPFileSystemProvider();
try (SFTPFileSystem fs = newFileSystem(provider, createEnv())) {
SFTPPath path = new SFTPPath(fs, "/foo/bar");
BasicFileAttributeView view = fs.provider().getFileAttributeView(path, BasicFileAttributeView.class);
assertNotNull(view);
BasicFileAttributes attributes = view.readAttributes();
assertTrue(attributes.isDirectory());
}
}
@Test
public void testView() throws IOException {
BasicFileAttributeView view = provider.view(fileLookup(), NO_INHERITED_VIEWS);
assertThat(view).isNotNull();
assertThat(view.name()).isEqualTo("basic");
BasicFileAttributes attrs = view.readAttributes();
assertThat(attrs.fileKey()).isEqualTo(0);
FileTime time = attrs.creationTime();
assertThat(attrs.lastAccessTime()).isEqualTo(time);
assertThat(attrs.lastModifiedTime()).isEqualTo(time);
view.setTimes(null, null, null);
attrs = view.readAttributes();
assertThat(attrs.creationTime()).isEqualTo(time);
assertThat(attrs.lastAccessTime()).isEqualTo(time);
assertThat(attrs.lastModifiedTime()).isEqualTo(time);
view.setTimes(FileTime.fromMillis(0L), null, null);
attrs = view.readAttributes();
assertThat(attrs.creationTime()).isEqualTo(time);
assertThat(attrs.lastAccessTime()).isEqualTo(time);
assertThat(attrs.lastModifiedTime()).isEqualTo(FileTime.fromMillis(0L));
}
public static void main(String[] args)
throws InterruptedException, IOException {
// Use 'which' to verify that 'nc' is available and skip the test
// if it is not.
Process proc = Runtime.getRuntime().exec("which nc");
InputStream stdout = proc.getInputStream();
int b = stdout.read();
proc.destroy();
if (b == -1) {
System.err.println("Netcat command unavailable; skipping test.");
return;
}
// Create a new sub-directory of the nominal test directory in which
// 'nc' will create the socket file.
String testSubDir = System.getProperty("test.dir", ".")
+ File.separator + TEST_SUB_DIR;
Path socketTestDir = Paths.get(testSubDir);
Files.createDirectory(socketTestDir);
// Set the path of the socket file.
String socketFilePath = testSubDir + File.separator
+ SOCKET_FILE_NAME;
// Create a process which executes the nc (netcat) utility to create
// a socket file at the indicated location.
FileSystem fs = FileSystems.getDefault();
try (WatchService ws = fs.newWatchService()) {
// Watch the test sub-directory to receive notification when an
// entry, i.e., the socket file, is added to the sub-directory.
WatchKey wk = socketTestDir.register(ws,
StandardWatchEventKinds.ENTRY_CREATE);
// Execute the 'nc' command.
proc = Runtime.getRuntime().exec(CMD_BASE + " " + socketFilePath);
// Wait until the socket file is created.
WatchKey key = ws.take();
if (key != wk) {
throw new RuntimeException("Unknown entry created - expected: "
+ wk.watchable() + ", actual: " + key.watchable());
}
wk.cancel();
}
// Verify that the socket file in fact exists.
Path socketPath = fs.getPath(socketFilePath);
if (!Files.exists(socketPath)) {
throw new RuntimeException("Socket file " + socketFilePath
+ " was not created by \"nc\" command.");
}
// Retrieve the most recent access and modification times of the
// socket file; print the values.
BasicFileAttributeView attributeView = Files.getFileAttributeView(
socketPath, BasicFileAttributeView.class);
BasicFileAttributes oldAttributes = attributeView.readAttributes();
FileTime oldAccessTime = oldAttributes.lastAccessTime();
FileTime oldModifiedTime = oldAttributes.lastModifiedTime();
System.out.println("Old times: " + oldAccessTime
+ " " + oldModifiedTime);
// Calculate the time to which the access and modification times of the
// socket file will be changed.
FileTime newFileTime =
FileTime.fromMillis(oldAccessTime.toMillis() + 1066);
try {
// Set the access and modification times of the socket file.
attributeView.setTimes(newFileTime, newFileTime, null);
// Retrieve the updated access and modification times of the
// socket file; print the values.
FileTime newAccessTime = null;
FileTime newModifiedTime = null;
BasicFileAttributes newAttributes = attributeView.readAttributes();
newAccessTime = newAttributes.lastAccessTime();
newModifiedTime = newAttributes.lastModifiedTime();
System.out.println("New times: " + newAccessTime + " "
+ newModifiedTime);
// Verify that the updated times have the expected values.
if ((newAccessTime != null && !newAccessTime.equals(newFileTime))
|| (newModifiedTime != null
&& !newModifiedTime.equals(newFileTime))) {
throw new RuntimeException("Failed to set correct times.");
}
} finally {
// Destry the process running netcat and delete the socket file.
proc.destroy();
Files.delete(socketPath);
}
}
/**
* Returns file "key" (e.g. inode) for the specified path
*/
private Object getKey(Path existing) throws IOException {
BasicFileAttributeView view = Files.getFileAttributeView(existing, BasicFileAttributeView.class);
BasicFileAttributes attributes = view.readAttributes();
return attributes.fileKey();
}
@BeforeClass
public static void setup() throws IOException {
Path home = Paths.get(HOME);
BasicFileAttributeView basicView = Files.getFileAttributeView(home, BasicFileAttributeView.class);
basicAttribs = basicView.readAttributes();
}