canonical example by Tabnine
public void pathUsage() { Path currentDir = Paths.get("."); // currentDir = "." Path fullPath = currentDir.toAbsolutePath(); // fullPath = "/Users/guest/workspace" Path one = currentDir.resolve("file.txt"); // one = "./file.txt" Path fileName = one.getFileName(); // fileName = "file.txt" }
/** * This implementation returns the name of the file. * @see java.nio.file.Path#getFileName() */ @Override public String getFilename() { return this.path.getFileName().toString(); }
@Override public void visitFile(Path file, String relative) throws IOException { if (file.getFileName().toString().endsWith(".class")) { Files.copy(file, outRoot.resolve(relative)); } } });
@Override public boolean apply(@Nullable URL input) { try { if (input == null) { return false; } final String fName = Paths.get(input.toURI()).getFileName().toString(); return fName.startsWith("druid") && fName.endsWith(".jar") && !fName.endsWith("selfcontained.jar"); } catch (URISyntaxException e) { throw Throwables.propagate(e); } } };
@Override public File[] listFiles( File directory, final FilenameFilter filter ) { try ( Stream<Path> listing = Files.list( path( directory ) ) ) { return listing .filter( entry -> filter.accept( entry.getParent().toFile(), entry.getFileName().toString() ) ) .map( Path::toFile ) .toArray( File[]::new ); } catch ( IOException e ) { return null; } }
private void removeAll(String baseName) throws IOException { try (DirectoryStream<Path> children = fsOps.newDirectoryStream(topologyBasicBlobsRootDir)) { for (Path p : children) { String fileName = p.getFileName().toString(); if (fileName.startsWith(baseName)) { fsOps.deleteIfExists(p.toFile()); } } } }
static Collection<String> getLocalizedUsers(Path localBaseDir) throws IOException { Path userCacheDir = getUserCacheDir(localBaseDir); if (!Files.exists(userCacheDir)) { return Collections.emptyList(); } return Files.list(userCacheDir).map((p) -> p.getFileName().toString()).collect(Collectors.toList()); }
outputDir = file.getParent(); if (outputDir == null) { outputDir = Paths.get("."); .forEach(uncheck(entry -> { progress.update(Optional.of(entry.name()), done.getAndIncrement() / (double) total); Path entryFile = outputDir.resolve(entry.name()); Files.createDirectories(entryFile.getParent()); Files.copy(entry.inputStream(), entryFile, REPLACE_EXISTING); String bundleName = outputDir.getFileName().toString(); Path propsFile = outputDir.getParent().resolve(bundleName + ".json"); BundleProps.write(propsFile, bundle);
protected static String calculateDefaultOutputDirectory(Path currentPath) { Path currentAbsolutePath = currentPath.toAbsolutePath(); Path parent = currentAbsolutePath.getParent(); if (currentAbsolutePath.getRoot().equals(parent)) { return parent.toString(); } else { Path currentNormalizedPath = currentAbsolutePath.normalize(); return "../" + currentNormalizedPath.getFileName().toString(); } }
/** * @param response Http response. * @param type Type. * @param path Path to file. * @throws IOException If failed. */ private static void handleRequest(HttpServletResponse response, String type, String path) throws IOException { Path path0 = Paths.get(path); response.setContentType(type); response.setHeader("Content-Disposition", "attachment; filename=\"" + path0.getFileName() + "\""); try (HttpOutput out = (HttpOutput)response.getOutputStream()) { out.sendContent(FileChannel.open(path0, StandardOpenOption.READ)); } }
private void backupDeployedJars(Map<Path, Path> jarFiles, RestoreScript restoreScript) throws IOException { Path jarsDirectory = backupDirectory.resolve(DEPLOYED_JARS_DIRECTORY); Files.createDirectories(jarsDirectory); for (Map.Entry<Path, Path> jarFileEntry : jarFiles.entrySet()) { Path jarFile = jarFileEntry.getKey(); Path originalFile = jarFileEntry.getValue(); Path destination = jarsDirectory.resolve(jarFile.getFileName()); moveFileOrDirectory(jarFile, destination); restoreScript.addFile(originalFile.toFile(), destination.toFile()); } }
private void processFileError(InputFile inputFile, SensorContext context) { Path ioFile = inputFile.file().toPath(); Path errorFile = ioFile.resolveSibling(ioFile.getFileName() + ERROR_EXTENSION).toAbsolutePath(); if (Files.exists(errorFile) && Files.isRegularFile(errorFile)) { LOG.debug("Processing " + errorFile.toString()); try { List<String> lines = Files.readAllLines(errorFile, context.fileSystem().encoding()); for (String line : lines) { if (StringUtils.isBlank(line) || line.startsWith("#")) { continue; } processLine(line, inputFile, context); } } catch (IOException e) { throw new IllegalStateException(e); } } }
private void moveFilesOrDirectories(Collection<Path> paths, Path targetDirectory) throws IOException { for (Path userFile : paths) { Path destination = targetDirectory.resolve(userFile.getFileName()); moveFileOrDirectory(userFile, destination); } }
@Override public FileVisitResult visitFile(Path filePath, BasicFileAttributes attrs) throws IOException { File file = filePath.toFile(); if (file.isDirectory()) { deleteDirectory(file); } else if (filePath.getFileName().equals(SHAREDMEMORY_FILE)) { return CONTINUE; } else if (!symLink || !filePath.equals(path)) { Files.delete(filePath); } return CONTINUE; }
public AtomicFileOutputStream(final Path path) throws IOException { final Path parent = path.getParent(); if (parent != null && parent.getNameCount() != 0 && ! Files.exists(parent)) { Files.createDirectories(parent); } current = new OpenState(Files.newOutputStream(path.resolveSibling(path.getFileName() + ".new"), CREATE, TRUNCATE_EXISTING), path); }
private void ensureDirectoryExists(Path filePath) { try { Files.createDirectories(filePath.getParent()); } catch (Exception e) { throw new RuntimeException("Cannot create directory for flexibleConfig " + filePath.getFileName() + "!"); } }
private Path tempPath() { Path parent = path.getParent(); File file = parent.toFile(); if (!file.exists()) { file.mkdirs(); } return parent.resolve(path.getFileName().toString() + '.' + tempSuffix()); }
/** * @return the name of current folder */ public static String getCurrentFolder() { return Paths.get("").toAbsolutePath().getFileName().toString(); }
public void copyOptJarsToLib(String jarNamePrefix) throws FileNotFoundException, IOException { final Optional<Path> reporterJarOptional; try (Stream<Path> logFiles = Files.walk(opt)) { reporterJarOptional = logFiles .filter(path -> path.getFileName().toString().startsWith(jarNamePrefix)) .findFirst(); } if (reporterJarOptional.isPresent()) { final Path optReporterJar = reporterJarOptional.get(); final Path libReporterJar = lib.resolve(optReporterJar.getFileName()); Files.copy(optReporterJar, libReporterJar); filesToDelete.add(new AutoClosablePath(libReporterJar)); } else { throw new FileNotFoundException("No jar could be found matching the pattern " + jarNamePrefix + "."); } }
@Override public FileVisitResult visitFile(Path candidate, BasicFileAttributes attrs) throws IOException { String fileName = candidate.getFileName().toString(); if (!fileName.endsWith(completedSuffix) && !isFileInTrackerDir(trackerDirCompletedFiles, candidate) && !fileName.startsWith(".") && includePattern.matcher(fileName).matches() && !ignorePattern.matcher(fileName).matches()) { candidateFiles.add(candidate.toFile()); } return FileVisitResult.CONTINUE; } });