Java Code Examples for java.nio.file.attribute.FileOwnerAttributeView

Following code examples demonstrate how to use java.nio.file.attribute.FileOwnerAttributeViewfrom java. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use java.nio.file.attribute.FileOwnerAttributeViewand various code implementation of this class.

    public static void main(String[] args) throws IOException{
    	
            Path testPath = Paths.get("E:\\EclipseBulider\\j-util\\src\\main\\webapp\\WEB-INF\\lib\\mybatis-generator-core-1.3.2.jar");
            BasicFileAttributeView basicView = Files.
                    getFileAttributeView(testPath, BasicFileAttributeView.class);
            BasicFileAttributes basicFileAttributes = basicView.readAttributes();

            System.out.println("创建时间:" + new Date(basicFileAttributes.creationTime()
                    .toMillis()));

            System.out.println("最后访问时间:" + new Date(basicFileAttributes.
                    lastAccessTime().toMillis()));

            System.out.println("最后修改时间:" + new Date(basicFileAttributes.
                    lastModifiedTime().toMillis()));

            System.out.println("文件大小:" + basicFileAttributes.size());

            FileOwnerAttributeView ownerView = Files.getFileAttributeView(testPath, 
                    FileOwnerAttributeView.class);

            System.out.println("文件所有者:" + ownerView.getOwner());


    } 


	public static void main(String[] args) throws IOException {
		try {
			Path path = Paths.get("C:\\poems\\luci1.txt");
			
			FileOwnerAttributeView foav =
			 Files.getFileAttributeView(
				path,FileOwnerAttributeView.class);

			UserPrincipal owner = foav.getOwner();
			System.out.format("Original owner of %s is %s%n",
			                  path, owner.getName());
			
			FileSystem fs = FileSystems.getDefault();
			UserPrincipalLookupService upls =
				fs.getUserPrincipalLookupService();
			
			UserPrincipal newOwner = 
				upls.lookupPrincipalByName("brice");
			foav.setOwner(newOwner);

			UserPrincipal changedOwner = foav.getOwner();
			System.out.format("New owner of %s is %s%n",
			                  path, changedOwner.getName());
		} 
		catch (UnsupportedOperationException | IOException e) {
			e.printStackTrace();
		} 
	} 

    public static void main(String[] args) throws IOException {
        Path testPath = Paths.get("src/le10/AttributeViewTest.java");
        BasicFileAttributeView basicView = Files.getFileAttributeView(
                testPath, BasicFileAttributeView.class);
        BasicFileAttributes basicAttribs = basicView.readAttributes();
        System.out.println("创建时间:" + new Date(basicAttribs.creationTime().toMillis()));
        System.out.println("最后访问时间:" + new Date(basicAttribs.lastAccessTime().toMillis()));
        System.out.println("最后修改时间:" + new Date(basicAttribs.lastModifiedTime().toMillis()));
        System.out.println("文件大小:" + basicAttribs.size());
        FileOwnerAttributeView fileOwnAttrs = Files.getFileAttributeView(
                testPath, FileOwnerAttributeView.class);
        System.out.println("文件拥有者:" + fileOwnAttrs.getOwner());
    } 

	public static void main(String[] args) {
		UserPrincipal owner = null;
		Path path = Paths.get(System.getProperty("user.home"), "Downloads", "dojo.pdf");
		FileOwnerAttributeView foav = Files.getFileAttributeView(path, FileOwnerAttributeView.class);
		
		try {
			owner = path.getFileSystem().getUserPrincipalLookupService().lookupPrincipalByName("administrator");
			foav.setOwner(owner);
		} catch (IOException e) {
			System.err.println(e);
		}
		
		System.out.println(owner);
	} 

	public static void main(String[] args) {
		Path path = Paths.get("C:/application_logs/spring_jta_transaction/transaction.log");
		UserPrincipal owner = null;
		FileOwnerAttributeView foav = null;
		try {
			owner = (UserPrincipal) Files.getAttribute(path, "owner:owner", NOFOLLOW_LINKS);
			foav = Files.getFileAttributeView(path, FileOwnerAttributeView.class);
			String role_owner = foav.getOwner().getName();
			System.out.println("FileOwnerAttributeView role:"+role_owner);
		}catch(IOException ioe) {
			ioe.printStackTrace();
		}
	} 

	public static void main(String[] args) {
		AclFileAttributeView a;
		BasicFileAttributeView b;
		DosFileAttributeView d;
		FileOwnerAttributeView f;
		PosixFileAttributeView p;
	} 

    public static void main(String[] args) throws IOException {
        final Path derbyLogPath = Paths.get("derby.log");
        final BasicFileAttributes fileAttributes = Files.readAttributes(derbyLogPath, BasicFileAttributes.class);
        System.out.println("Creation time: " + fileAttributes.creationTime());
        System.out.println("Size: " + fileAttributes.size());

        final BasicFileAttributeView basicFileAttributeView = Files.getFileAttributeView(derbyLogPath, BasicFileAttributeView.class);
        basicFileAttributeView.setTimes(null, null, null);
        System.out.println("Name: " + basicFileAttributeView.name());

        final FileOwnerAttributeView fileOwnerAttributeView = Files.getFileAttributeView(derbyLogPath, FileOwnerAttributeView.class);
        System.out.println("Owner: " + fileOwnerAttributeView.getOwner());

        final UserPrincipalLookupService userPrincipalLookupService = FileSystems.getDefault().getUserPrincipalLookupService();
        final UserPrincipal mxf = userPrincipalLookupService.lookupPrincipalByName("mxf");
        System.out.println(mxf);
    } 

    public static void main(String[] args) throws IOException {

        Path path = Paths.get("D:\\daily\\BR_BRAND_CRM_zfb_20111215", "brandcrm.properties");

        UserPrincipal gaopengxiang = path.getFileSystem().getUserPrincipalLookupService().lookupPrincipalByName("gaopengxiang");

        System.out.println(gaopengxiang.getName());


        Files.setOwner(path, gaopengxiang);


        FileOwnerAttributeView fileAttributeView = Files.getFileAttributeView(path, FileOwnerAttributeView.class, LinkOption.NOFOLLOW_LINKS);
        UserPrincipal owner = fileAttributeView.getOwner();

        Files.setOwner(path,owner);

        fileAttributeView.setOwner(owner);

        Files.setAttribute(path, "owner:owner", owner, LinkOption.NOFOLLOW_LINKS);

    } 

	public static void main(String[] args) {
		Path path = Paths.get(System.getProperty("user.home"), "Downloads", "dojo.pdf");
		FileOwnerAttributeView foav = Files.getFileAttributeView(path, FileOwnerAttributeView.class);
		
		try {
			String owner = foav.getOwner().getName();
			System.out.println(owner);
		} catch (IOException e) {
			System.err.println(e);
		}
	} 

	public static void main(String[] args) {
		Path dire = Paths.get("source\\dalieOCP\\fileAndDirectoryAttributes");
		Path file = Paths.get("Execute.txt");
		FileOwnerAttributeView fileOwnerView = Files.getFileAttributeView(dire.resolve(file), FileOwnerAttributeView.class);
		try {
			UserPrincipal userPrincipal = fileOwnerView.getOwner();
		
		} catch (IOException e){
			e.printStackTrace();
		}
	} 

	public void getOwner() {
		FileOwnerAttributeView attributes = Files.getFileAttributeView(path, FileOwnerAttributeView.class);
		try {
			System.out.println(attributes.getOwner().getName()
			);
		} catch (IOException e) {
			e.printStackTrace();
		}
	} 

    public static void main(String[] args) {
        Path path = Paths.get("C:/home/docs/users.txt");
        try {
            FileOwnerAttributeView view = Files.getFileAttributeView(path, FileOwnerAttributeView.class);
            UserPrincipal userPrincipal = view.getOwner();
            System.out.println(userPrincipal.getName());
           

        } catch (IOException e) {
            e.printStackTrace();
        }
    } 

	public FileInfo process(final File file) throws Exception {
		Path path = Paths.get(file.getPath());
		FileInfo fileInfo = new FileInfo();
		fileInfo.setFileName(file.getName());
		fileInfo.setFilePath(file.getAbsolutePath());
		fileInfo.setSize(file.length());
		fileInfo.setLastModified(file.lastModified());
		FileOwnerAttributeView ownerAttributeView = Files.getFileAttributeView(path, FileOwnerAttributeView.class);
		fileInfo.setOwner(ownerAttributeView.getOwner().getName());
		BasicFileAttributes attributes = Files.readAttributes(path, BasicFileAttributes.class);
		fileInfo.setCreated(attributes.creationTime().toMillis());

		return fileInfo;
	} 

    public boolean check(File file, String criteria) {
        FileOwnerAttributeView ownerAttributeView = Files.getFileAttributeView(file.toPath(), FileOwnerAttributeView.class);
        try {
            UserPrincipal owner = ownerAttributeView.getOwner();
            return owner.getName().equalsIgnoreCase(criteria);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    } 

	 public static void main(String[] args) throws Exception{

	        Path path = Paths.get("/home/jianglibo/pjava.txt");
	        FileOwnerAttributeView view = Files.getFileAttributeView(path, FileOwnerAttributeView.class);
	        UserPrincipalLookupService lookupService = FileSystems.getDefault().getUserPrincipalLookupService();
	        UserPrincipal userPrincipal = lookupService.lookupPrincipalByName("tomcat");
	        view.setOwner(userPrincipal);
	        System.out.println("Owner: " + view.getOwner().getName());
	} 

    public static void main(String[] args) throws IOException {
        Path path = Paths.get("/tmp/docs/users.txt");

        FileOwnerAttributeView view = Files.getFileAttributeView(path, FileOwnerAttributeView.class);
        UserPrincipal userPrincipal = view.getOwner();
        System.out.println(userPrincipal.getName());
    } 

	private static void testName() throws IOException {
		System.out.println("==>testName");
		Path file = Paths.get("E:", "temp", "abc.txt");
		FileOwnerAttributeView view = Files.getFileAttributeView(file, FileOwnerAttributeView.class);
		System.out.println(view.name());
	} 

	private static void testGetOwner() throws IOException {
		System.out.println("==>testGetOwner");
		Path file = Paths.get("E:", "temp", "abc.txt");
		FileOwnerAttributeView view = Files.getFileAttributeView(file, FileOwnerAttributeView.class);
		System.out.println(view.getOwner());
	} 

    public static void main(String[] args) {
        Path path = Paths.get("C:/home/docs/users.txt");
        FileOwnerAttributeView view = Files.getFileAttributeView(path, FileOwnerAttributeView.class);
        try {
            UserPrincipalLookupService lookupService = FileSystems.getDefault().getUserPrincipalLookupService();
            UserPrincipal userPrincipal = lookupService.lookupPrincipalByName("mary");

            view.setOwner(userPrincipal);
            System.out.println("Owner: " + view.getOwner().getName());
        } catch (IOException ex) {
            ex.printStackTrace();
        }


    } 

    @Test
    public default void verifyGenericType()
    throws Exception {
        final Class<SUT> sut = createNewSUT();
        assertTrue("This j8unit test interface is used with a generic type that is illegaly not assignable to FileOwnerAttributeView.class!",
                   FileOwnerAttributeView.class.isAssignableFrom(sut));
    } 

    public static String read(Path pathToReadForOwnerAttributes) throws IOException {
        FileOwnerAttributeView ownerAttributeView = Files.getFileAttributeView(pathToReadForOwnerAttributes, FileOwnerAttributeView.class);
        UserPrincipal owner = ownerAttributeView.getOwner();
        return owner.getName();
    } 

    public static void main(String[] args) throws IOException {
        Path path = Paths.get("/tmp/docs/users.txt");

        FileOwnerAttributeView view = Files.getFileAttributeView(path, FileOwnerAttributeView.class);
        UserPrincipalLookupService lookupService = FileSystems.getDefault().getUserPrincipalLookupService();
        UserPrincipal userPrincipal = lookupService.lookupPrincipalByName("jennifer");

        view.setOwner(userPrincipal);
        System.out.println("Owner : " + view.getOwner().getName());
    } 

	public static void main(String[] args) {
		Path path = Paths.get("C:");

		try {
			FileStore fs = Files.getFileStore(path);
			printDetails(fs, AclFileAttributeView.class);
			printDetails(fs, BasicFileAttributeView.class);
			printDetails(fs, DosFileAttributeView.class);
			printDetails(fs, FileOwnerAttributeView.class);
			printDetails(fs, PosixFileAttributeView.class);
			printDetails(fs, UserDefinedFileAttributeView.class);
		} 
		catch (IOException ex) {
			ex.printStackTrace();
		}
	} 

Advertisement
Javadoc
A file attribute view that supports reading or updating the owner of a file. This file attribute vie

w is intended for file system implementations that support a file attribute that represents an identity that is the owner of the file. Often the owner of a file is the identity of the entity that created the file.

The #getOwner getOwner or #setOwner setOwner methods may be used to read or update the owner of the file.

The java.nio.file.Files#getAttribute getAttribute and java.nio.file.Files#setAttribute setAttribute methods may also be used to read or update the owner. In that case, the owner attribute is identified by the name "owner", and the value of the attribute is a UserPrincipal. @since 1.7

Read More
Advertisement