Java Code Examples for java.nio.file.Files#newDirectoryStream()
The following examples show how to use
java.nio.file.Files#newDirectoryStream() .
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: RollingAppenderOnStartupTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@BeforeClass public static void beforeClass() throws Exception { if (Files.exists(Paths.get("target/onStartup"))) { try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(Paths.get(DIR))) { for (final Path path : directoryStream) { Files.delete(path); } Files.delete(Paths.get(DIR)); } } Files.createDirectory(new File(DIR).toPath()); Path target = Paths.get(DIR, FILENAME); Files.copy(Paths.get(SOURCE, FILENAME), target, StandardCopyOption.COPY_ATTRIBUTES); FileTime newTime = FileTime.from(Instant.now().minus(1, ChronoUnit.DAYS)); Files.getFileAttributeView(target, BasicFileAttributeView.class).setTimes(newTime, newTime, newTime); }
Example 2
Source File: CodeStoreAndPathTest.java From hottub with GNU General Public License v2.0 | 6 votes |
@Test public void changeUserDirTest() throws ScriptException, IOException { System.setProperty("nashorn.persistent.code.cache", codeCache); final NashornScriptEngineFactory fac = new NashornScriptEngineFactory(); final ScriptEngine e = fac.getScriptEngine(ENGINE_OPTIONS_NOOPT); final Path codeCachePath = getCodeCachePath(false); final String newUserDir = "build/newUserDir"; // Now changing current working directory System.setProperty("user.dir", System.getProperty("user.dir") + File.separator + newUserDir); try { // Check that a new compiled script is stored in existing code cache e.eval(code1); final DirectoryStream<Path> stream = Files.newDirectoryStream(codeCachePath); checkCompiledScripts(stream, 1); // Setting to default current working dir } finally { System.setProperty("user.dir", oldUserDir); } }
Example 3
Source File: NodeEnvironment.java From crate with Apache License 2.0 | 6 votes |
private static List<Path> collectIndexSubPaths(NodePath[] nodePaths, Predicate<Path> subPathPredicate) throws IOException { List<Path> indexSubPaths = new ArrayList<>(); for (NodePath nodePath : nodePaths) { Path indicesPath = nodePath.indicesPath; if (Files.isDirectory(indicesPath)) { try (DirectoryStream<Path> indexStream = Files.newDirectoryStream(indicesPath)) { for (Path indexPath : indexStream) { if (Files.isDirectory(indexPath)) { try (Stream<Path> shardStream = Files.list(indexPath)) { shardStream.filter(subPathPredicate) .map(Path::toAbsolutePath) .forEach(indexSubPaths::add); } } } } } } return indexSubPaths; }
Example 4
Source File: NioExtensions.java From groovy with Apache License 2.0 | 6 votes |
/** * Invokes the closure for each 'child' file in this 'parent' folder/directory. * Both regular files and subfolders/subdirectories can be processed depending * on the fileType enum value. * * @param self a Path (that happens to be a folder/directory) * @param fileType if normal files or directories or both should be processed * @param closure the closure to invoke * @throws java.io.FileNotFoundException if the given directory does not exist * @throws IllegalArgumentException if the provided Path object does not represent a directory * @since 2.3.0 */ public static void eachFile(final Path self, final FileType fileType, @ClosureParams(value = SimpleType.class, options = "java.nio.file.Path") final Closure closure) throws IOException { //throws FileNotFoundException, IllegalArgumentException { checkDir(self); // TODO GroovyDoc doesn't parse this file as our java.g doesn't handle this JDK7 syntax try (DirectoryStream<Path> stream = Files.newDirectoryStream(self)) { for (Path path : stream) { if (fileType == FileType.ANY || (fileType != FileType.FILES && Files.isDirectory(path)) || (fileType != FileType.DIRECTORIES && Files.isRegularFile(path))) { closure.call(path); } } } }
Example 5
Source File: Util.java From pattypan with MIT License | 6 votes |
public static File[] getFilesAllowedToUpload(File directory, boolean includeSubdirectories) { ArrayList<File> files = new ArrayList<>(); try (DirectoryStream<Path> stream = Files.newDirectoryStream(directory.toPath())) { for (Path path : stream) { if (path.toFile().isDirectory() && includeSubdirectories) { File[] dirFiles = getFilesAllowedToUpload(path.toFile(), includeSubdirectories); ArrayList<File> dirFilesList = new ArrayList<>(Arrays.asList(dirFiles)); files.addAll(dirFilesList); } else if (isFileAllowedToUpload(path.toFile().getName())) { files.add(path.toFile()); } } } catch (IOException e) { } return files.stream().toArray(File[]::new); }
Example 6
Source File: LoadAndStoreXML.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * Test loadFromXML with malformed documents */ static void testLoadWithMalformedDoc(Path dir) throws IOException { try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "*.xml")) { for (Path file: stream) { System.out.println("testLoadWithMalformedDoc, file=" + file.getFileName()); try (InputStream in = Files.newInputStream(file)) { Properties props = new Properties(); try { props.loadFromXML(in); throw new RuntimeException("InvalidPropertiesFormatException not thrown"); } catch (InvalidPropertiesFormatException x) { System.out.println(x); } } } } }
Example 7
Source File: SimpleLogHandlerTest.java From bazel with Apache License 2.0 | 5 votes |
@Test public void testRotateLimitBytes() throws Exception { FakeClock clock = new FakeClock(Instant.parse("2018-01-01T12:00:00Z"), ZoneOffset.UTC); SimpleLogHandler handler = SimpleLogHandler.builder() .setPrefix(tmp.getRoot() + File.separator + "limits") .setFormatter(new TrivialFormatter()) .setRotateLimitBytes(16) .setClockForTesting(clock) .build(); Optional<Path> symlinkPath = handler.getSymbolicLinkPath(); handler.publish(new LogRecord(Level.SEVERE, "1234567" /* 8 bytes including "\n" */)); Path firstLogPath = handler.getCurrentLogFilePath().get(); clock.set(Instant.parse("2018-01-01T12:00:01Z")); // Ensure the next file has a different name. handler.publish(new LogRecord(Level.SEVERE, "1234567" /* 8 bytes including "\n" */)); Path secondLogPath = handler.getCurrentLogFilePath().get(); handler.publish(new LogRecord(Level.SEVERE, "1234567" /* 8 bytes including "\n" */)); handler.close(); if (symlinkPath.isPresent()) { // The symlink path is expected to be present on non-Windows platforms; see tests above. assertThat(Files.isSymbolicLink(symlinkPath.get())).isTrue(); assertThat(Files.readSymbolicLink(symlinkPath.get()).toString()) .isEqualTo(secondLogPath.getFileName().toString()); } assertThat(Files.size(firstLogPath)).isEqualTo(16L /* including two "\n" */); assertThat(Files.size(secondLogPath)).isEqualTo(8L /* including "\n" */); try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(tmp.getRoot().toPath())) { assertThat(dirStream).hasSize(3); } }
Example 8
Source File: APITest.java From openjdk-8 with GNU General Public License v2.0 | 5 votes |
private void listFiles(Path dir, Set<Path> files) throws IOException { try (DirectoryStream<Path> ds = Files.newDirectoryStream(dir)) { for (Path f: ds) { if (Files.isDirectory(f)) listFiles(f, files); else if (Files.isRegularFile(f)) files.add(f); } } }
Example 9
Source File: Creator.java From jmbe with GNU General Public License v3.0 | 5 votes |
/** * Recursively finds the specified filename in the specified directory * * @param downloadDirectory to search * @param fileName to discover * @return discovered file path or null */ public static Path getFile(Path downloadDirectory, String fileName) { try { DirectoryStream<Path> stream = Files.newDirectoryStream(downloadDirectory); Iterator<Path> it = stream.iterator(); while(it.hasNext()) { Path path = it.next(); if(Files.isDirectory(path) && !path.equals(getOutputDirectory(downloadDirectory))) { Path subPath = getFile(path, fileName); if(subPath != null && subPath.endsWith(fileName)) { return subPath; } } else if(path.endsWith(fileName)) { return path; } } } catch(IOException ioe) { System.out.println("Error while searching for [" + fileName + "]"); System.exit(EXIT_CODE_IO_ERROR); } return null; }
Example 10
Source File: FileBasedApplicationImportExportManager.java From carbon-apimgt with Apache License 2.0 | 5 votes |
/** * Queries the list of directories available under a root directory path * * @param path full path of the root directory * @return Set of directory path under the root directory given by path * @throws IOException if an error occurs while listing directories */ private Set<String> getDirectoryList(String path) throws IOException { Set<String> directoryNames = new HashSet<>(); try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(Paths.get(path))) { for (Path directoryPath : directoryStream) { directoryNames.add(directoryPath.toString()); } } return directoryNames; }
Example 11
Source File: DiffUtils.java From swagger2markup with Apache License 2.0 | 5 votes |
public static void assertThatAllFilesAreEqual(Path expectedDirectory, Path actualDirectory, String reportName) { Path reportPath = Paths.get("build/diff-report/", reportName); try { try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(expectedDirectory)) { for (Path expectedFile : directoryStream) { Path actualFile = actualDirectory.resolve(expectedFile.getFileName()); LOGGER.info("Diffing file '{}' with '{}'", actualFile, expectedFile); DiffAssertions.assertThat(actualFile).isEqualTo(expectedFile, reportPath); } } } catch (IOException e) { throw new RuntimeException("Failed to assert that all files are equal", e); } }
Example 12
Source File: FileUtil.java From testgrid with Apache License 2.0 | 5 votes |
/** * This method returns the list of files in a given directory after filtering a given glob pattern. * * @param directory the directory in which the files should be look-up * @param glob the glob pattern to be filtered * @return list of files * @throws IOException thrown when an error occurred while accessing files on directory. */ public static List<String> getFilesOnDirectory(String directory, String glob) throws IOException { List<String> files = new ArrayList<>(); try (DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get(directory), glob)) { for (Path entry : stream) { files.add(entry.toString()); } } catch (IOException e) { throw new IOException("Error occurred while getting files on directory + " + directory, e); } return files; }
Example 13
Source File: NodeEnvironment.java From crate with Apache License 2.0 | 5 votes |
/** * This is a best effort to ensure that we actually have write permissions to write in all our data directories. * This prevents disasters if nodes are started under the wrong username etc. */ private void assertCanWrite() throws IOException { for (Path path : nodeDataPaths()) { // check node-paths are writable tryWriteTempFile(path); } for (String indexFolderName : this.availableIndexFolders()) { for (Path indexPath : this.resolveIndexFolder(indexFolderName)) { // check index paths are writable Path indexStatePath = indexPath.resolve(MetaDataStateFormat.STATE_DIR_NAME); tryWriteTempFile(indexStatePath); tryWriteTempFile(indexPath); try (DirectoryStream<Path> stream = Files.newDirectoryStream(indexPath)) { for (Path shardPath : stream) { String fileName = shardPath.getFileName().toString(); if (Files.isDirectory(shardPath) && fileName.chars().allMatch(Character::isDigit)) { Path indexDir = shardPath.resolve(ShardPath.INDEX_FOLDER_NAME); Path statePath = shardPath.resolve(MetaDataStateFormat.STATE_DIR_NAME); Path translogDir = shardPath.resolve(ShardPath.TRANSLOG_FOLDER_NAME); tryWriteTempFile(indexDir); tryWriteTempFile(translogDir); tryWriteTempFile(statePath); tryWriteTempFile(shardPath); } } } } } }
Example 14
Source File: ExecutionEnvironment.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
private void verifySymLinks(String bindir) throws IOException { File binDir = new File(bindir); System.err.println("verifying links in: " + bindir); File isaDir = new File(binDir, getArch()).getAbsoluteFile(); if (!isaDir.exists()) { throw new RuntimeException("dir: " + isaDir + " does not exist"); } try (DirectoryStream<Path> ds = Files.newDirectoryStream(binDir.toPath())) { for (Path p : ds) { if (symlinkExcludes.matcher(p.toString()).matches() || Files.isDirectory(p, NOFOLLOW_LINKS)) { continue; } Path link = new File(isaDir, p.getFileName().toString()).toPath(); if (Files.isSymbolicLink(link)) { Path target = Files.readSymbolicLink(link); if (target.startsWith("..") && p.endsWith(target.getFileName())) { // System.out.println(target + " OK"); continue; } System.err.println("target:" + target); System.err.println("file:" + p); } throw new RuntimeException("could not find link to " + p); } } }
Example 15
Source File: StandardDocFileFactory.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
/** If the file is a directory, list its contents. */ public Iterable<DocFile> list() throws IOException { List<DocFile> files = new ArrayList<DocFile>(); try (DirectoryStream<Path> ds = Files.newDirectoryStream(file)) { for (Path f: ds) { files.add(new StandardDocFile(f)); } } return files; }
Example 16
Source File: CodeStoreAndPathTest.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
@Test public void codeCacheTest() throws ScriptException, IOException { System.setProperty("nashorn.persistent.code.cache", codeCache); final NashornScriptEngineFactory fac = new NashornScriptEngineFactory(); final ScriptEngine e = fac.getScriptEngine(ENGINE_OPTIONS_NOOPT); final Path codeCachePath = getCodeCachePath(false); e.eval(code1); e.eval(code2); e.eval(code3);// less than minimum size for storing // adding code1 and code2. final DirectoryStream<Path> stream = Files.newDirectoryStream(codeCachePath); checkCompiledScripts(stream, 2); }
Example 17
Source File: Main.java From openjdk-8 with GNU General Public License v2.0 | 5 votes |
void scan(Path p) throws IOException { if (Files.isDirectory(p)) { for (Path c: Files.newDirectoryStream(p)) { scan(c); } } else if (isTidyFile(p)) { scan(Files.readAllLines(p, Charset.defaultCharset())); } }
Example 18
Source File: PathArchiveRepository.java From Nicobar with Apache License 2.0 | 5 votes |
@Override public Map<ModuleId, Long> getArchiveUpdateTimes() throws IOException { Map<ModuleId, Long> updateTimes = new LinkedHashMap<ModuleId, Long>(); DirectoryStream<Path> archiveDirs = Files.newDirectoryStream(rootDir, DIRECTORY_FILTER); for (Path archiveDir: archiveDirs) { Path absoluteArchiveDir = rootDir.resolve(archiveDir); long lastUpdateTime = Files.getLastModifiedTime(absoluteArchiveDir).toMillis(); ModuleId moduleId = ModuleId.fromString(archiveDir.getFileName().toString()); updateTimes.put(moduleId, lastUpdateTime); } return updateTimes; }
Example 19
Source File: TempFileWarningLogger.java From SearchServices with GNU Lesser General Public License v3.0 | 5 votes |
@SuppressWarnings("RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE") public void removeFiles() { try(DirectoryStream<Path> stream = Files.newDirectoryStream(dir, glob)) { for (Path file : stream) { file.toFile().delete(); } } catch (IOException e) { log.debug("Unable to delete temp file", e); } }
Example 20
Source File: JsonFileBasedLoader.java From bonita-ui-designer with GNU General Public License v2.0 | 4 votes |
@Override public Map<String, List<T>> findByObjectIds(Path directory, List<String> objectIds) throws IOException { if (!exists(directory) || objectIds.isEmpty()) { return emptyMap(); } Map<String, List<T>> map = new HashMap<>(); //Each component has its own files in a directory named with its id try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(directory, "[!.]*")) { for (Path componentDirectory : directoryStream) { //The directory name is the component id String id = componentDirectory.getFileName().toString(); Path componentFile = componentDirectory.resolve(id + ".json"); if (!exists(componentFile)) { continue; } String content = new String(readAllBytes(componentFile)); String contentWithoutSpaces = removeSpaces(content); T object; try { object = objectMapper.fromJson(content.getBytes(), type); } catch (IOException ex) { throw new IOException("Json mapping error for " + componentFile, ex); } for (String objectId : objectIds) { //We consider only another objects Path objectPath = resolve(directory, objectId); if (objectPath == null || !objectPath.equals(componentFile) && exists(componentFile)) { if (contentWithoutSpaces.contains(format("\"id\":\"%s\"", objectId))) { List<T> objects = map.computeIfAbsent(objectId, k -> new ArrayList<>()); objects.add(object); } } } } } return map; }