Example usage for java.nio.file.attribute FileOwnerAttributeView getOwner

List of usage examples for java.nio.file.attribute FileOwnerAttributeView getOwner

Introduction

In this page you can find the example usage for java.nio.file.attribute FileOwnerAttributeView getOwner.

Prototype

UserPrincipal getOwner() throws IOException;

Source Link

Document

Read the file owner.

Usage

From source file:Test.java

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

From source file:Test.java

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

    UserPrincipalLookupService lookupService = FileSystems.getDefault().getUserPrincipalLookupService();
    userPrincipal = lookupService.lookupPrincipalByName("users");
    view.setOwner(userPrincipal);//from ww  w  .ja v a2s  . c  o m
    System.out.println("UserPrincipal set: " + userPrincipal.getName());

}

From source file:Main.java

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

    Path path = Paths.get("C:\\Java_Dev\\test1.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);/* w  w w. j  a  v  a  2 s  .c o m*/

    UserPrincipal changedOwner = foav.getOwner();
    System.out.format("New owner  of  %s  is %s%n", path, changedOwner.getName());

}

From source file:Test.java

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

    view.setOwner(userPrincipal);//from ww w  .j a v  a 2s  .co  m
    System.out.println("Owner: " + view.getOwner().getName());
}

From source file:Test.java

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

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

}

From source file:fr.pilato.elasticsearch.crawler.fs.framework.FsCrawlerUtil.java

/**
 * Determines the 'owner' of the file.//from   w ww.  j  a v  a  2 s.co m
 */
public static String getOwnerName(final File file) {
    try {
        final Path path = Paths.get(file.getAbsolutePath());
        final FileOwnerAttributeView ownerAttributeView = Files.getFileAttributeView(path,
                FileOwnerAttributeView.class);
        return ownerAttributeView != null ? ownerAttributeView.getOwner().getName() : null;
    } catch (Exception e) {
        logger.warn("Failed to determine 'owner' of {}: {}", file, e.getMessage());
        return null;
    }
}

From source file:oracle.kv.sample.fileloader.FileLoader.java

/**
 * After successful validation this method is run to load the content of a
 * TXT file into the given table//from   ww w.  j  a v  a  2s .c  om
 * 
 * @throws IOException
 */
private void loadData() throws IOException {
    if (inputPathStr != null) {
        dir = new File(inputPathStr);
        File file = null;
        File[] files = null;
        int len = 0;

        // If input path is a directory then load data from all the files
        // that are under the directory. Make sure all the files are of same
        // type
        // i.e. TXT (mix and match is not allowed)
        if (dir.exists()) {
            System.out.println("There are " + dir.listFiles().length + " to be loaded.");
            if (dir.isDirectory()) {
                files = dir.listFiles();
                len = files.length;

                // loop through all the files and load the content one by
                // one
                for (int i = 0; i < len; i++) {
                    file = files[i];
                    try {
                        Path filePath = Paths.get(file.getPath());
                        BasicFileAttributes attr = Files.readAttributes(filePath, BasicFileAttributes.class,
                                LinkOption.NOFOLLOW_LINKS);
                        DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
                        FileOwnerAttributeView fileOwnerAttributeView = Files.getFileAttributeView(filePath,
                                FileOwnerAttributeView.class);
                        UserPrincipal userPrincipal = fileOwnerAttributeView.getOwner();

                        id = Integer.toString(i);
                        fileDate = formatter.format(attr.lastAccessTime().toMillis());
                        fileOwner = userPrincipal.getName();
                        binaryFile = FileUtils.readFileToByteArray(file);

                        row = table.createRow();
                        row.put("id", id);
                        row.put("date", fileDate.toString());
                        row.put("owner", fileOwner);
                        row.put("file", binaryFile);

                        tableh.put(row, null, null);
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            } else {
                System.out.println("There are no Files to Load");
            }
        }
    }

    if (fileId != null) {
        getData();
    }
}

From source file:org.akaza.openclinica.controller.SystemController.java

public ArrayList<HashMap<String, Object>> displayOwnerShipForTomcatDirectory(File file) throws IOException {
    ArrayList<HashMap<String, Object>> listOfHashMaps = new ArrayList<>();
    HashMap<String, Object> hashMap = null;
    if (file.isDirectory()) {
        hashMap = new HashMap<String, Object>();
        hashMap.put("Read Access", getReadAccess(file));
        hashMap.put("Write Access", getWriteAccess(file));

        Path path = Paths.get(file.getCanonicalPath());
        FileOwnerAttributeView ownerAttributeView = Files.getFileAttributeView(path,
                FileOwnerAttributeView.class);
        UserPrincipal owner = ownerAttributeView.getOwner();

        // hashMap.put("ownership", owner.getName());
        hashMap.put("Folder Name", file.getName());
        listOfHashMaps.add(hashMap);/*  ww  w. j a va 2s.  c  om*/
        int dirCount = getNumberOfSubFolders(file.getCanonicalPath().toString());
        if (dirCount != 0) {
            hashMap.put("Sub Folders", displayOwnerShipForTomcatSubDirectories(file));
        }
    }
    return listOfHashMaps;
}

From source file:org.akaza.openclinica.controller.SystemController.java

public ArrayList<HashMap<String, Object>> displayOwnerShipForTomcatSubDirectories(File dir) throws IOException {
    ArrayList<HashMap<String, Object>> listOfHashMaps = new ArrayList<>();
    HashMap<String, Object> hashMap = null;
    File[] files = dir.listFiles();
    for (File file : files) {
        if (file.isDirectory()) {
            hashMap = new HashMap<String, Object>();
            hashMap.put("Read Access", getReadAccess(file));
            hashMap.put("Write Access", getWriteAccess(file));

            Path path = Paths.get(file.getCanonicalPath());
            FileOwnerAttributeView ownerAttributeView = Files.getFileAttributeView(path,
                    FileOwnerAttributeView.class);
            UserPrincipal owner = ownerAttributeView.getOwner();

            // hashMap.put("ownership", owner.getName());
            hashMap.put("Folder Name", file.getName());
            listOfHashMaps.add(hashMap);
            int dirCount = getNumberOfSubFolders(file.getCanonicalPath().toString());
            if (dirCount != 0) {
                // hashMap.put("Sub Folders", displayOwnerShipForTomcatSubDirectories(file));
            }//from w ww  .ja v  a 2  s  .  c  om
        }
    }
    return listOfHashMaps;
}

From source file:org.cryptomator.cryptofs.CryptoFileSystemImpl.java

private void copyAttributes(Path src, Path dst) throws IOException {
    Set<Class<? extends FileAttributeView>> supportedAttributeViewTypes = fileStore
            .supportedFileAttributeViewTypes();
    if (supportedAttributeViewTypes.contains(BasicFileAttributeView.class)) {
        BasicFileAttributes srcAttrs = Files.readAttributes(src, BasicFileAttributes.class);
        BasicFileAttributeView dstAttrView = Files.getFileAttributeView(dst, BasicFileAttributeView.class);
        dstAttrView.setTimes(srcAttrs.lastModifiedTime(), srcAttrs.lastAccessTime(), srcAttrs.creationTime());
    }//from   w w w  .  j av a 2  s  . c om
    if (supportedAttributeViewTypes.contains(FileOwnerAttributeView.class)) {
        FileOwnerAttributeView srcAttrView = Files.getFileAttributeView(src, FileOwnerAttributeView.class);
        FileOwnerAttributeView dstAttrView = Files.getFileAttributeView(dst, FileOwnerAttributeView.class);
        dstAttrView.setOwner(srcAttrView.getOwner());
    }
    if (supportedAttributeViewTypes.contains(PosixFileAttributeView.class)) {
        PosixFileAttributes srcAttrs = Files.readAttributes(src, PosixFileAttributes.class);
        PosixFileAttributeView dstAttrView = Files.getFileAttributeView(dst, PosixFileAttributeView.class);
        dstAttrView.setGroup(srcAttrs.group());
        dstAttrView.setPermissions(srcAttrs.permissions());
    }
    if (supportedAttributeViewTypes.contains(DosFileAttributeView.class)) {
        DosFileAttributes srcAttrs = Files.readAttributes(src, DosFileAttributes.class);
        DosFileAttributeView dstAttrView = Files.getFileAttributeView(dst, DosFileAttributeView.class);
        dstAttrView.setArchive(srcAttrs.isArchive());
        dstAttrView.setHidden(srcAttrs.isHidden());
        dstAttrView.setReadOnly(srcAttrs.isReadOnly());
        dstAttrView.setSystem(srcAttrs.isSystem());
    }
}