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" }
public static void writePluginServices(Iterable<String> plugins, File root) throws IOException { Path path = root.toPath().resolve(SERVICES_FILE); createDirectories(path.getParent()); try (Writer out = new OutputStreamWriter(new FileOutputStream(path.toFile()), UTF_8)) { for (String plugin : plugins) { out.write(plugin + "\n"); } } }
@Override public void visitFile(Path file, String relative) throws IOException { if (file.getFileName().toString().endsWith(".class")) { Files.copy(file, outRoot.resolve(relative)); } } });
/** * Copy file to working directory. * * @param filePath File path. * @return File name. * @throws IOException If coping failed. */ String copyToWorkDir(String filePath) throws IOException { Path srcFile = Paths.get(filePath); if (Files.exists(srcFile)) { checkDownloadFolder(); Path newDir = Paths.get(downloadFolder); Path fileName = srcFile.getFileName(); Files.copy(srcFile, newDir.resolve(fileName), StandardCopyOption.REPLACE_EXISTING); return fileName.toString(); } return null; }
@Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { Path relative = sourcePath.relativize(dir); Path target = targetPath.resolve(relative); File folder = target.toFile(); if (!folder.exists()) { mkdirs(folder); } return FileVisitResult.CONTINUE; }
@Override public File resolve(final CacheDescriptor item) { String version = item.getVersion() .replace("^", "") .replace("v", ""); String classifier = Optional.ofNullable(item.getClassifier()) .map(it -> "-" + it) .orElse(""); Path artifactId = Paths.get(item.getName()).resolve(version) .resolve(item.getName() + "-" + version + classifier + "." + item.getExtension()); return repository.resolve(groupId).resolve(artifactId).toAbsolutePath().toFile(); } }
static String resolveLocalRepoPath(String localRepoPath) { // todo decouple home folder resolution // find homedir String home = System.getenv("ZEPPELIN_HOME"); if (home == null) { home = System.getProperty("zeppelin.home"); } if (home == null) { home = ".."; } return Paths.get(home).resolve(localRepoPath).toAbsolutePath().toString(); }
private static void addPreconfLibs(String libs) { URL u = Main.class.getClassLoader().getResource(Main.class.getName().replace('.', '/') + ".class"); if (u != null) { String path = u.toString(); int delimiterPos = path.lastIndexOf('!'); if (delimiterPos > -1) { String jar = path.substring(9, delimiterPos); File jarFile = new File(jar); Path libRoot = new File(jarFile.getParent() + File.separator + "btrace-libs").toPath(); Path libFolder = libs != null ? libRoot.resolve(libs) : libRoot; if (Files.exists(libFolder)) { appendToBootClassPath(libFolder); appendToSysClassPath(libFolder); } else if (libs != null) { // for user provided libs config report error if the location does not exist DebugSupport.warning("Invalid 'libs' configuration [" + libs + "]. " + "Path '" + libFolder.toAbsolutePath().toString() + "' does not exist."); } } } }
@Test public void shouldNotThrowWhenOldRecordingLogsAreDeleted() throws IOException { createSegmentFile(recordingThreeId); final Path segmentFilePath = Paths.get(segmentFileName(recordingThreeId, 0)); final boolean segmentFileExists = Files.exists(archiveDir.toPath().resolve(segmentFilePath)); assumeThat(segmentFileExists, is(true)); final Catalog catalog = new Catalog(archiveDir, null, 0, MAX_ENTRIES, clock); catalog.close(); }
@Test public void shouldRemoveDirectory() throws Exception { Path path = Paths.get(Testing.Files.dataDir(), "test-dir"); assertThat(path.toFile().mkdirs()).isTrue(); Path file = path.resolve("file.txt"); assertThat(file.toFile().createNewFile()).isTrue(); Testing.Files.delete(path); assertThat(java.nio.file.Files.exists(path)).isFalse(); } }
throws IOException, KeyNotFoundException, AuthorizationException { synchronized (LocallyCachedTopologyBlob.class) { if (!Files.exists(topologyBasicBlobsRootDir)) { Files.createDirectories(topologyBasicBlobsRootDir); fsOps.setupStormCodeDir(owner, topologyBasicBlobsRootDir.toFile()); String resourcesJar = resourcesJar(); URL url = classloader.getResource(ServerConfigUtils.RESOURCES_SUBDIR); Path extractionDest = topologyBasicBlobsRootDir.resolve(type.getTempExtractionDir(LOCAL_MODE_JAR_VERSION)); if (resourcesJar != null) { LOG.info("Extracting resources from jar at {} to {}", resourcesJar, extractionDest); extractDirFromJar(urlConnection.getJarFileURL().getFile(), ServerConfigUtils.RESOURCES_SUBDIR, extractionDest); } else { fsOps.copyDirectory(new File(url.getFile()), extractionDest.toFile()); Path path = topologyBasicBlobsRootDir.resolve(type.getTempFileName(v)); fsOps.forceMkdir(path.getParent()); return path; Path extractionDest = topologyBasicBlobsRootDir.resolve(type.getTempExtractionDir(downloadMeta.getVersion())); extractDirFromJar(tmpLocation.toAbsolutePath().toString(), ServerConfigUtils.RESOURCES_SUBDIR, extractionDest);
@Test void shouldMakeFromCanonical() throws IOException, CommandFailed, IncorrectUsage, IncorrectFormat { Path dataDir = testDirectory.directory( "some-other-path" ).toPath(); Path databaseDir = dataDir.resolve( "databases/foo.db" ); Files.createDirectories( databaseDir ); Files.write( configDir.resolve( Config.DEFAULT_CONFIG_FILE_NAME ), singletonList( formatProperty( data_directory, dataDir ) ) ); new LoadCommand( homeDir, configDir, loader ) .execute( ArrayUtil.concat( new String[]{"--database=foo.db", "--from=foo.dump"} ) ); verify( loader ).load( eq( Paths.get( new File( "foo.dump" ).getCanonicalPath() ) ), any(), any() ); }
@Test public void testMakesureDirExists() throws Exception { Path dir = FileUtil.createTempDir(); String child1 = "child1"; Path child1Dir = dir.resolve(child1); FileUtil.makesureDirExists(child1Dir.toString()); assertThat(child1Dir).exists(); String child2 = "child2"; Path child2Dir = dir.resolve(child2); FileUtil.makesureDirExists(child2Dir); assertThat(child2Dir).exists(); String child3 = "child3"; Path child3Dir = dir.resolve(child3); FileUtil.makesureDirExists(child3Dir.toFile()); assertThat(child3Dir).exists(); }
@Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.move(file, haStorageDir.toPath().resolve(file.getFileName())); return FileVisitResult.CONTINUE; } });
private void readVersion() throws IOException { Path versionFile = topologyBasicBlobsRootDir.resolve(type.getVersionFileName()); if (!fsOps.fileExists(versionFile)) { version = NOT_DOWNLOADED_VERSION; } else { String ver = FileUtils.readFileToString(versionFile.toFile(), "UTF8").trim(); version = Long.parseLong(ver); } }