List of usage examples for java.nio.file Files walkFileTree
public static Path walkFileTree(Path start, Set<FileVisitOption> options, int maxDepth, FileVisitor<? super Path> visitor) throws IOException
From source file:Test.java
public static void main(String[] args) { try {/* w ww . j a v a 2s . c o m*/ Path source = Paths.get("/home"); Path target = Paths.get("/backup"); Files.walkFileTree(source, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, new CopyDirectory(source, target)); } catch (IOException ex) { ex.printStackTrace(); } }
From source file:Search.java
public static void main(String[] args) throws IOException { Path searchFile = Paths.get("Demo.jpg"); Search walk = new Search(searchFile); EnumSet<FileVisitOption> opts = EnumSet.of(FileVisitOption.FOLLOW_LINKS); Iterable<Path> dirs = FileSystems.getDefault().getRootDirectories(); for (Path root : dirs) { if (!walk.found) { Files.walkFileTree(root, opts, Integer.MAX_VALUE, walk); }/* w ww . j a v a2s . com*/ } if (!walk.found) { System.out.println("The file " + searchFile + " was not found!"); } }
From source file:com.github.houbin217jz.thumbnail.Thumbnail.java
public static void main(String[] args) { Options options = new Options(); options.addOption("s", "src", true, "????????????"); options.addOption("d", "dst", true, ""); options.addOption("r", "ratio", true, "/??, 30%???0.3????????"); options.addOption("w", "width", true, "(px)"); options.addOption("h", "height", true, "?(px)"); options.addOption("R", "recursive", false, "???????"); HelpFormatter formatter = new HelpFormatter(); String formatstr = "java -jar thumbnail.jar " + "[-s/--src <path>] " + "[-d/--dst <path>] " + "[-r/--ratio double] " + "[-w/--width integer] " + "[-h/--height integer] " + "[-R/--recursive] "; CommandLineParser parser = new PosixParser(); CommandLine cmd = null;/*from w w w . j a v a 2s . c o m*/ try { cmd = parser.parse(options, args); } catch (ParseException e1) { formatter.printHelp(formatstr, options); return; } final Path srcDir, dstDir; final Integer width, height; final Double ratio; // if (cmd.hasOption("s")) { srcDir = Paths.get(cmd.getOptionValue("s")).toAbsolutePath(); } else { srcDir = Paths.get("").toAbsolutePath(); //?? } // if (cmd.hasOption("d")) { dstDir = Paths.get(cmd.getOptionValue("d")).toAbsolutePath(); } else { formatter.printHelp(formatstr, options); return; } if (!Files.exists(srcDir, LinkOption.NOFOLLOW_LINKS) || !Files.isDirectory(srcDir, LinkOption.NOFOLLOW_LINKS)) { System.out.println("[" + srcDir.toAbsolutePath() + "]??????"); return; } if (Files.exists(dstDir, LinkOption.NOFOLLOW_LINKS)) { if (!Files.isDirectory(dstDir, LinkOption.NOFOLLOW_LINKS)) { //???????? System.out.println("????????"); return; } } else { //???????? try { Files.createDirectories(dstDir); } catch (IOException e) { e.printStackTrace(); return; } } //?? if (cmd.hasOption("w") && cmd.hasOption("h")) { try { width = Integer.valueOf(cmd.getOptionValue("width")); height = Integer.valueOf(cmd.getOptionValue("height")); } catch (NumberFormatException e) { System.out.println("??????"); return; } } else { width = null; height = null; } //? if (cmd.hasOption("r")) { try { ratio = Double.valueOf(cmd.getOptionValue("r")); } catch (NumberFormatException e) { System.out.println("?????"); return; } } else { ratio = null; } if (width != null && ratio != null) { System.out.println("??????????????"); return; } if (width == null && ratio == null) { System.out.println("????????????"); return; } // int maxDepth = 1; if (cmd.hasOption("R")) { maxDepth = Integer.MAX_VALUE; } try { //Java 7 ??@see http://docs.oracle.com/javase/jp/7/api/java/nio/file/Files.html Files.walkFileTree(srcDir, EnumSet.of(FileVisitOption.FOLLOW_LINKS), maxDepth, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path path, BasicFileAttributes basicFileAttributes) throws IOException { //???&??? String filename = path.getFileName().toString().toLowerCase(); if (filename.endsWith(".jpg") || filename.endsWith(".jpeg")) { //Jpeg?? /* * relative??: * rootPath: /a/b/c/d * filePath: /a/b/c/d/e/f.jpg * rootPath.relativize(filePath) = e/f.jpg */ /* * resolve?? * rootPath: /a/b/c/output * relativePath: e/f.jpg * rootPath.resolve(relativePath) = /a/b/c/output/e/f.jpg */ Path dst = dstDir.resolve(srcDir.relativize(path)); if (!Files.exists(dst.getParent(), LinkOption.NOFOLLOW_LINKS)) { Files.createDirectories(dst.getParent()); } doResize(path.toFile(), dst.toFile(), width, height, ratio); } return FileVisitResult.CONTINUE; } }); } catch (IOException e) { e.printStackTrace(); } }
From source file:com.elemenopy.backupcopy.filesystem.WatcherManager.java
public static void init(BackupConfig config) throws IOException { backupConfig = config;// ww w . j a va 2s .com for (RootFolder rootFolder : config.getRootFolders()) { WatchService watcher = fileSystem.newWatchService(); //start monitoring the source folder TaskManager.runTask(new WatcherTask(rootFolder, watcher)); TaskManager.runTask(() -> { //traverse the source folder tree, registering subfolders on the watcher final WatcherRegisteringFileVisitor visitor = new WatcherRegisteringFileVisitor(rootFolder, watcher); try { EnumSet<FileVisitOption> opts = EnumSet.of(FOLLOW_LINKS); Files.walkFileTree(fileSystem.getPath(rootFolder.getPath()), opts, Integer.MAX_VALUE, visitor); } catch (IOException ex) { logger.error("Error while setting up directory watchers", ex); } }); } }
From source file:services.ImportExportService.java
public static List<DatabaseImage> findImportables(Path path) { final List<DatabaseImage> importable = new LinkedList<DatabaseImage>(); try {//from w w w . j a va 2 s. co m Files.walkFileTree(path, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException { if (PathService.isImage(path)) { DatabaseImage image = DatabaseImage.forPath(path); if (hasFileToImport(image) && !image.imported) { importable.add(image); } } return FileVisitResult.CONTINUE; } }); } catch (IOException io) { io.printStackTrace(); return importable; } return importable; }
From source file:com.elemenopy.backupcopy.filesystem.RootFolderSynchronizer.java
public void synchronize() throws IOException { Path sourceRoot = fileSystem.getPath(sourceRootFolder.getPath()); if (!sourceRoot.toFile().isDirectory()) { throw new IllegalArgumentException("Source path " + sourceRootFolder.getPath() + " is not a directory"); }//from w ww . java 2s . c om EnumSet<FileVisitOption> opts = EnumSet.of(FOLLOW_LINKS); Files.walkFileTree(sourceRoot, opts, Integer.MAX_VALUE, new SyncingFileVisitor(sourceRoot, destination)); }
From source file:org.sonar.core.util.FileUtils.java
private static void cleanDirectoryImpl(Path path) throws IOException { checkArgument(path.toFile().isDirectory(), "'%s' is not a directory", path); Files.walkFileTree(path, FOLLOW_LINKS, CleanDirectoryFileVisitor.VISIT_MAX_DEPTH, new CleanDirectoryFileVisitor(path)); }
From source file:org.esa.nest.util.FileIOUtils.java
public static void copyFolder(final Path source, final Path target) throws IOException { // follow links when copying files final EnumSet<FileVisitOption> opts = EnumSet.of(FileVisitOption.FOLLOW_LINKS); final CopyDirVisitor tc = new CopyDirVisitor(source, target, false); Files.walkFileTree(source, opts, Integer.MAX_VALUE, tc); }
From source file:de.thomasbolz.renamer.Renamer.java
/** * Step 1 of the renaming process://from w ww . j a va2 s . c om * Analysis steps in the renaming process. Walks the whole file tree of the source directory and creates a CopyTask * for each file or directory that is found and does not match the exclusion list. * @return a list of all CopyTasks */ public Map<Path, List<CopyTask>> prepareCopyTasks() { try { Files.walkFileTree(source, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, new SimpleFileVisitor<Path>() { /** * If we find a directory create/copy it and add a counter * * @param dir * @param attrs * @return * @throws java.io.IOException */ @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { Path targetdir = target.resolve(source.relativize(dir)); copyTasks.put(dir, new ArrayList<CopyTask>()); return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { if (!file.getFileName().toString().toLowerCase().matches(EXLUSION_REGEX)) { copyTasks.get(file.getParent()).add(new CopyTask(file, null)); } else { excludedFiles.add(file); } return FileVisitResult.CONTINUE; } }); } catch (IOException e) { e.printStackTrace(); } sortCopyTasks(); // generateTargetFilenames(); return copyTasks; }
From source file:org.sonar.process.FileUtils.java
private static void cleanDirectoryImpl(Path path) throws IOException { if (!path.toFile().isDirectory()) { throw new IllegalArgumentException(format("'%s' is not a directory", path)); }/*from ww w . j ava 2 s . c om*/ Files.walkFileTree(path, FOLLOW_LINKS, CleanDirectoryFileVisitor.VISIT_MAX_DEPTH, new CleanDirectoryFileVisitor(path)); }