Example usage for java.nio.file.attribute BasicFileAttributes isRegularFile

List of usage examples for java.nio.file.attribute BasicFileAttributes isRegularFile

Introduction

In this page you can find the example usage for java.nio.file.attribute BasicFileAttributes isRegularFile.

Prototype

boolean isRegularFile();

Source Link

Document

Tells whether the file is a regular file with opaque content.

Usage

From source file:Main.java

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

    BasicFileAttributes attr = null;
    Path path = Paths.get("C:/tutorial/Java/JavaFX", "Topic.txt");

    attr = Files.readAttributes(path, BasicFileAttributes.class);

    System.out.println("Is regular file ? " + attr.isRegularFile());

}

From source file:Test.java

public static void main(String[] args) throws Exception {
    Path path = FileSystems.getDefault().getPath("/home/docs/users.txt");
    // BasicFileAttributes attributes = Files.readAttributes(path,
    // BasicFileAttributes.class);
    BasicFileAttributeView view = Files.getFileAttributeView(path, BasicFileAttributeView.class);
    BasicFileAttributes attributes = view.readAttributes();

    System.out.println("Creation Time: " + attributes.creationTime());
    System.out.println("Last Accessed Time: " + attributes.lastAccessTime());
    System.out.println("Last Modified Time: " + attributes.lastModifiedTime());
    System.out.println("File Key: " + attributes.fileKey());
    System.out.println("Directory: " + attributes.isDirectory());
    System.out.println("Other Type of File: " + attributes.isOther());
    System.out.println("Regular File: " + attributes.isRegularFile());
    System.out.println("Symbolic File: " + attributes.isSymbolicLink());
    System.out.println("Size: " + attributes.size());
}

From source file:com.ejisto.util.IOUtils.java

public static Collection<String> findAllClassesInJarFile(File jarFile) throws IOException {
    final List<String> ret = new ArrayList<>();
    Map<String, String> env = new HashMap<>();
    env.put("create", "false");
    try (FileSystem targetFs = FileSystems.newFileSystem(URI.create("jar:file:" + jarFile.getAbsolutePath()),
            env)) {/*from  w  ww . j a  va2  s .  c o m*/
        final Path root = targetFs.getPath("/");
        Files.walkFileTree(root, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                String ext = ".class";
                if (attrs.isRegularFile() && file.getFileName().toString().endsWith(ext)) {
                    String path = root.relativize(file).toString();
                    ret.add(translatePath(path.substring(0, path.length() - ext.length()), "/"));
                }
                return FileVisitResult.CONTINUE;
            }
        });
    }
    return ret;
}

From source file:de.bbe_consulting.mavento.helper.visitor.FileSizeVisitor.java

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {

    if (attrs.isRegularFile()) {
        sizeTotal.add(attrs.size());/*from  w  w  w.  jav a  2s.c om*/
    }
    return FileVisitResult.CONTINUE;
}

From source file:com.preferanser.server.client.DealUploader.java

private List<File> discoverJsonFiles(String jsonDealsPath) throws URISyntaxException, IOException {
    final List<File> jsonFiles = Lists.newArrayList();
    Path path = Paths.get(DealUploader.class.getResource(jsonDealsPath).toURI());
    Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
        @Override//from   w  w w. j ava 2 s  .c o m
        public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
            File file = path.toFile();
            if (attrs.isRegularFile() && file.getName().endsWith(".json"))
                jsonFiles.add(file);
            return super.visitFile(path, attrs);
        }
    });
    return jsonFiles;
}

From source file:edu.cwru.jpdg.Javac.java

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attr) {
    if (attr.isRegularFile()) {
        String ext = FilenameUtils.getExtension(file.toString());
        if (ext.equals("class")) {
            files.add(file.toFile());/*from   w ww .  j  a v a  2  s  .  co m*/
        }
    }
    return FileVisitResult.CONTINUE;
}

From source file:de.tiqsolutions.hdfs.BasicFileAttributeViewImpl.java

Map<String, Object> readAttributes(String attributes) throws IOException {
    BasicFileAttributes attr = readAttributes();
    List<String> attrlist = Arrays.asList(attributes.split(","));
    boolean readall = attrlist.contains("*");
    Map<String, Object> ret = new HashMap<>();
    if (readall || attrlist.contains("fileKey"))
        ret.put("fileKey", attr.fileKey());
    if (readall || attrlist.contains("creationTime"))
        ret.put("creationTime", attr.creationTime());
    if (readall || attrlist.contains("isDirectory"))
        ret.put("isDirectory", attr.isDirectory());
    if (readall || attrlist.contains("isOther"))
        ret.put("isOther", attr.isOther());
    if (readall || attrlist.contains("isRegularFile"))
        ret.put("isRegularFile", attr.isRegularFile());
    if (readall || attrlist.contains("isSymbolicLink"))
        ret.put("isSymbolicLink", attr.isSymbolicLink());
    if (readall || attrlist.contains("lastAccessTime"))
        ret.put("lastAccessTime", attr.lastAccessTime());
    if (readall || attrlist.contains("lastModifiedTime"))
        ret.put("lastModifiedTime", attr.lastModifiedTime());
    if (readall || attrlist.contains("size"))
        ret.put("size", attr.size());
    return ret;//from  ww  w  .  j ava 2 s.c  o  m
}

From source file:gobblin.example.simplejson.SimpleJsonSource.java

@Override
public List<WorkUnit> getWorkunits(SourceState state) {
    List<WorkUnit> workUnits = Lists.newArrayList();

    if (!state.contains(ConfigurationKeys.SOURCE_FILEBASED_FILES_TO_PULL)) {
        return workUnits;
    }/*from  w  w  w.  j  a v  a  2s. c  o  m*/

    // Create a single snapshot-type extract for all files
    Extract extract = new Extract(state, Extract.TableType.SNAPSHOT_ONLY,
            state.getProp(ConfigurationKeys.EXTRACT_NAMESPACE_NAME_KEY, "ExampleNamespace"), "ExampleTable");

    String filesToPull = state.getProp(ConfigurationKeys.SOURCE_FILEBASED_FILES_TO_PULL);
    File tempFileDir = new File("test_temp/"); // TODO: Delete the dir after completion.
    tempFileDir.mkdir();
    String tempFileDirAbsolute = "";
    try {
        tempFileDirAbsolute = tempFileDir.getCanonicalPath(); // Retrieve absolute path of temp folder
    } catch (IOException e) {
        e.printStackTrace();
    }

    int nameCount = 0;
    int csvCount = 0;
    for (String file : Splitter.on(',').omitEmptyStrings().split(filesToPull)) {
        Iterator it = FileUtils.iterateFiles(new File(file), null, true);
        while (it.hasNext()) {
            try {
                File newFile = (File) it.next();
                String basePath = newFile.getCanonicalPath(); // Retrieve absolute path of source
                Path path = newFile.toPath();

                //call to rest api:, provide with file basePath
                String extension = "";
                System.out.println("basePath is" + basePath);
                int i = basePath.lastIndexOf('.');
                System.out.println("i");
                if (i > 0) {
                    extension = basePath.substring(i + 1);
                }

                String url_file_name = "";
                int j = basePath.lastIndexOf('/');
                if (j > 0) {
                    url_file_name = basePath.substring(j + 1);
                }

                //hand off to rest api
                if (extension.equals("csv")) {
                    System.out.println("CSVCSVCSV");
                    csvCount += 1;
                    System.out.println("CURL________________________________________");
                    //Include basePath, filename, location you want to store file
                    System.out.println(
                            "curl http://localhost:8080" + basePath + "/" + Integer.toString(nameCount));
                    //10.0.2.2 is localhost from vagrant
                    // Insert the nameCount so that it can be joined back later.
                    Process p = Runtime.getRuntime()
                            .exec("curl http://localhost:8080" + basePath + "/" + Integer.toString(nameCount));
                    // String myUrl = "http://localhost:8080/parse" + basePath + "&" + url_file_name + "&" + tempFileDirAbsolute;
                    // System.out.println("------------------------------");
                    // System.out.println(myUrl);
                    // try {
                    //   URL url = new URL(myUrl);
                    //   HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    //   connection.setRequestMethod("GET");
                    //   connection.connect();
                    // } catch (Exception e) {
                    //   e.printStackTrace();
                    // }

                }
                // Print filename and associated metadata 
                System.out.println(basePath);
                BasicFileAttributes attr = Files.readAttributes(path, BasicFileAttributes.class);
                // System.out.println("  creationTime: " + attr.creationTime());
                // System.out.println("  lastAccessTime: " + attr.lastAccessTime());
                // System.out.println("  lastModifiedTime: " + attr.lastModifiedTime());
                // System.out.println("  isDirectory: " + attr.isDirectory());
                // System.out.println("  isOther: " + attr.isOther());
                // System.out.println("  isRegularFile: " + attr.isRegularFile());
                // System.out.println("  isSymbolicLink: " + attr.isSymbolicLink());
                // System.out.println("  size: " + attr.size()); 
                // System.out.println(" ");

                //creating intermediate JSON
                JSONObject intermediate = new JSONObject();
                intermediate.put("filename", basePath);
                intermediate.put("timestamp", String.valueOf((new Date()).getTime()));
                intermediate.put("namespace", getMacAddress());

                intermediate.put("creationTime", String.valueOf(attr.creationTime()));
                intermediate.put("lastAccessTime", String.valueOf(attr.lastAccessTime()));
                intermediate.put("lastModifiedTime", String.valueOf(attr.lastModifiedTime()));
                intermediate.put("isDirectory", String.valueOf(attr.isDirectory()));
                intermediate.put("isOther", String.valueOf(attr.isOther()));
                intermediate.put("isRegularFile", String.valueOf(attr.isRegularFile()));
                intermediate.put("isSymbolicLink", String.valueOf(attr.isSymbolicLink()));
                intermediate.put("size", attr.size());

                // Create intermediate temp file
                nameCount += 1;
                String intermediateName = "/generated" + String.valueOf(nameCount) + ".json";
                String finalName = tempFileDirAbsolute + intermediateName;
                FileWriter generated = new FileWriter(finalName);
                generated.write(intermediate.toJSONString());
                generated.flush();
                generated.close();

                // Create one work unit for each file to pull
                WorkUnit workUnit = new WorkUnit(state, extract);
                workUnit.setProp(SOURCE_FILE_KEY, finalName);
                workUnits.add(workUnit);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        // write out number of files found to temp file
        try {
            FileWriter numCsvFiles = new FileWriter(tempFileDirAbsolute + "/numCsvFiles.txt");
            numCsvFiles.write("" + csvCount);
            numCsvFiles.flush();
            numCsvFiles.close();

            FileWriter numFiles = new FileWriter(tempFileDirAbsolute + "/numFiles.txt");
            numFiles.write("" + nameCount);
            numFiles.flush();
            numFiles.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    return workUnits;
}

From source file:org.cryptomator.frontend.webdav.servlet.DavFolder.java

@Override
public DavResourceIterator getMembers() {
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(path)) {
        List<DavResource> children = new ArrayList<>();
        for (Path childPath : stream) {
            BasicFileAttributes childAttr = Files.readAttributes(childPath, BasicFileAttributes.class);
            DavLocatorImpl childLocator = locator.resolveChild(childPath.getFileName().toString());
            if (childAttr.isDirectory()) {
                DavFolder childFolder = factory.createFolder(childLocator, childPath, Optional.of(childAttr),
                        session);//from  w  ww  .j a  va2 s .  c  o m
                children.add(childFolder);
            } else if (childAttr.isRegularFile()) {
                DavFile childFile = factory.createFile(childLocator, childPath, Optional.of(childAttr),
                        session);
                children.add(childFile);
            }
        }
        return new DavResourceIteratorImpl(children);
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}

From source file:org.dishevelled.thumbnail.examples.ThumbnailDrop.java

/**
 * Return true if the specified path is a non-empty regular file.
 *
 * @param path path//from w w  w .  ja v  a  2  s .  c om
 * @return true if the specified path is a non-empty regular file
 * @throws IOException if an I/O error occurs
 */
static boolean isNonEmpty(final Path path) throws IOException {
    BasicFileAttributes attr = Files.readAttributes(path, BasicFileAttributes.class);
    return attr.isRegularFile() && (attr.size() > 0L);
}