Java Code Examples for java.nio.file.attribute.BasicFileAttributeView#readAttributes()

The following examples show how to use java.nio.file.attribute.BasicFileAttributeView#readAttributes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SensUtils.java    From SENS with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 获取文件创建时间
 *
 * @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();
}
 
Example 2
Source File: HaloUtils.java    From stone with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 获取文件创建时间
 *
 * @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();
}
 
Example 3
Source File: HaloUtils.java    From blog-sharon with Apache License 2.0 6 votes vote down vote up
/**
 * 获取文件创建时间
 *
 * @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();
}
 
Example 4
Source File: FileUtil.java    From xnx3 with Apache License 2.0 6 votes vote down vote up
/**
 * 输入文件路径,返回这个文件的创建时间
 * @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();  
}
 
Example 5
Source File: LocalFileSystemUtils.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 6
Source File: SFTPFileSystemProviderTest.java    From sftp-fs with Apache License 2.0 5 votes vote down vote up
@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());
    }
}
 
Example 7
Source File: BasicAttributeProviderTest.java    From jimfs with Apache License 2.0 5 votes vote down vote up
@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));
}
 
Example 8
Source File: UnixSocketFile.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
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);
    }
}
 
Example 9
Source File: WindowsFS.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/** 
 * 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();
}
 
Example 10
Source File: BasicAttribsIntegrationTest.java    From tutorials with MIT License 4 votes vote down vote up
@BeforeClass
public static void setup() throws IOException {
    Path home = Paths.get(HOME);
    BasicFileAttributeView basicView = Files.getFileAttributeView(home, BasicFileAttributeView.class);
    basicAttribs = basicView.readAttributes();
}