Example usage for java.nio.file Path getFileName

List of usage examples for java.nio.file Path getFileName

Introduction

In this page you can find the example usage for java.nio.file Path getFileName.

Prototype

Path getFileName();

Source Link

Document

Returns the name of the file or directory denoted by this path as a Path object.

Usage

From source file:it.sonarlint.cli.tools.SonarlintCli.java

public Path deployProject(String location) throws IOException {
    Path originalLoc = Paths.get("projects").resolve(location);
    String projectName = originalLoc.getFileName().toString();

    if (!Files.isDirectory(originalLoc)) {
        throw new IllegalArgumentException(
                "Couldn't find project directory: " + originalLoc.toAbsolutePath().toString());
    }/*from ww  w.  j a va 2  s . com*/

    cleanProject();
    project = Files.createTempDirectory(projectName);
    FileUtils.copyDirectory(originalLoc.toFile(), project.toFile());
    return project;
}

From source file:codes.thischwa.c5c.impl.JarFilemanagerMessageResolver.java

@Override
public void setServletContext(ServletContext servletContext) {
    ObjectMapper mapper = new ObjectMapper();
    try {/*from  w w w.  j  a  va 2s .c  o m*/
        if (JarPathResolver.insideJar(getMessagesFolderPath())) {
            CodeSource src = JarFilemanagerMessageResolver.class.getProtectionDomain().getCodeSource();
            URI uri = src.getLocation().toURI();
            logger.info("Message folder is inside jar: {}", uri);

            try (FileSystem jarFS = FileSystems.newFileSystem(uri, new HashMap<String, String>())) {
                if (jarFS.getRootDirectories().iterator().hasNext()) {
                    Path rootDirectory = jarFS.getRootDirectories().iterator().next();

                    Path langFolder = rootDirectory.resolve(getMessagesFolderPath());
                    if (langFolder != null) {
                        try (DirectoryStream<Path> langFolderStream = Files.newDirectoryStream(langFolder,
                                JS_FILE_MASK)) {
                            for (Path langFile : langFolderStream) {
                                String lang = langFile.getFileName().toString();
                                InputStream is = Files.newInputStream(langFile);
                                Map<String, String> langData = mapper.readValue(is,
                                        new TypeReference<HashMap<String, String>>() {
                                        });
                                collectLangData(lang, langData);
                            }
                        }
                    } else {
                        throw new RuntimeException("Folder in jar " + langFolder + " does not exists.");
                    }
                }
            }
        } else {
            File messageFolder = JarPathResolver.getFolder(getMessagesFolderPath());
            logger.info("Message folder resolved to: {}", messageFolder);

            if (messageFolder == null || !messageFolder.exists()) {
                throw new RuntimeException("Folder " + getMessagesFolderPath() + " does not exist");
            }

            for (File file : messageFolder.listFiles(jsFilter)) {
                String lang = FilenameUtils.getBaseName(file.getName());
                Map<String, String> langData = mapper.readValue(file,
                        new TypeReference<HashMap<String, String>>() {
                        });
                collectLangData(lang, langData);
            }
        }
    } catch (URISyntaxException | IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.blackducksoftware.integration.hub.detect.detector.clang.DependenciesListFileManager.java

private String getFilenameBase(final String filePathString) {
    final Path filePath = new File(filePathString).toPath();
    return FilenameUtils.removeExtension(filePath.getFileName().toString());
}

From source file:fi.johannes.kata.ocr.utils.structs.Filename.java

public Filename(Path filename) throws IOException {
    // TODO Not sure is it even necessary to validate Path, check
    if (CFilePathOperations.validatePath(filename.getFileName().toString())) {
        absolutePath = filename.toAbsolutePath();
        cacheStrings();// w ww. ja  v a 2 s . co m
    } else {
        throw new IOException();
    }
}

From source file:ws.doerr.cssinliner.server.PathSerializer.java

@Override
public void serialize(Path value, JsonGenerator gen, SerializerProvider serializers)
        throws IOException, JsonProcessingException {
    gen.writeStartObject();//  www . ja v a 2s .co  m
    gen.writeStringField("name", value.getFileName().toString());
    gen.writeStringField("folder", value.getParent().toString());
    gen.writeStringField("path", value.toString());
    gen.writeNumberField("modified", value.toFile().lastModified());
    gen.writeEndObject();
}

From source file:com.surevine.gateway.scm.gatewayclient.GatewayPackage.java

void createTar(final Path tarPath, final Path... paths) throws IOException, ArchiveException {
    ArchiveOutputStream os = new ArchiveStreamFactory().createArchiveOutputStream("tar",
            Files.newOutputStream(tarPath));
    try {//from  www .ja v a2s . c  o  m
        for (Path path : paths) {
            TarArchiveEntry entry = new TarArchiveEntry(path.toFile());
            entry.setName(path.getFileName().toString());
            os.putArchiveEntry(entry);
            Files.copy(path, os);
            os.closeArchiveEntry();
        }
    } finally {
        os.close();
    }
}

From source file:com.hpe.caf.worker.testing.preparation.PreparationResultProcessor.java

@Override
protected Path getSaveFilePath(TestItem<TInput, TExpected> testItem, TaskMessage message) {
    Path saveFilePath = super.getSaveFilePath(testItem, message);
    if (configuration.isStoreTestCaseWithInput()) {
        Path fileName = saveFilePath.getFileName();
        Path path = Paths.get(testItem.getInputData().getInputFile());
        saveFilePath = Paths.get(configuration.getTestDataFolder(),
                path.getParent() == null ? "" : path.getParent().toString(), fileName.toString());
    }//  w  w w  .java2 s  . co  m
    return saveFilePath;
}

From source file:com.dotcms.content.elasticsearch.business.ESIndexHelper.java

/**
 * Finds file within the directory named "snapshot-"
 * @param snapshotDirectory/*from   w  w  w  .  ja  va2  s.c  om*/
 * @return
 * @throws IOException
 */
public String findSnapshotName(File snapshotDirectory) throws IOException {
    String name = null;
    if (snapshotDirectory.isDirectory()) {
        class SnapshotVisitor extends SimpleFileVisitor<Path> {
            private String fileName;

            public String getFileName() {
                return fileName;
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                if (file.getFileName().toString().startsWith(SNAPSHOT_PREFIX)) {
                    fileName = file.getFileName().toString();
                    return FileVisitResult.TERMINATE;
                }
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                return FileVisitResult.CONTINUE;
            }
        }
        Path directory = Paths.get(snapshotDirectory.getAbsolutePath());
        SnapshotVisitor snapshotVisitor = new SnapshotVisitor();
        Files.walkFileTree(directory, snapshotVisitor);

        String fullName = snapshotVisitor.getFileName();
        if (fullName != null) {
            name = fullName.replaceFirst(SNAPSHOT_PREFIX, "");
        }
    }
    return name;
}

From source file:org.devgateway.toolkit.forms.service.DerbyDatabaseBackupService.java

/**
 * Gets the URL (directory/file) of the backupPath. Adds as prefixes the
 * last leaf of backup's location parent directory + {@link #databaseName}
 * If the backupPath does not have a parent, it uses the host name from
 * {@link InetAddress#getLocalHost()}/*from  w  ww.  j a va2  s  .  com*/
 * 
 * @param backupPath
 *            the parent directory for the backup
 * @return the backup url to be used by the backup procedure
 * @throws UnknownHostException
 */
private String createBackupURL(final String backupPath) {
    java.text.SimpleDateFormat todaysDate = new java.text.SimpleDateFormat("yyyyMMdd-HHmmss");
    String parent = null;
    Path originalPath = Paths.get(backupPath);
    Path filePath = originalPath.getFileName();
    if (filePath != null) {
        parent = filePath.toString();
    } else {
        try {
            // fall back to hostname instead
            parent = InetAddress.getLocalHost().getHostName();
        } catch (UnknownHostException e) {
            LOGGER.debug("Cannot get localhost/hostname! " + e);
            return null;
        }
    }

    String backupURL = backupPath + "/" + parent + "-" + databaseName + "-"
            + todaysDate.format((java.util.Calendar.getInstance()).getTime());
    return backupURL;
}

From source file:com.cloudbees.clickstack.util.Files2.java

/**
 * Copy given {@code srcFile} to given {@code destDir}.
 *
 * @param srcFile/*from   ww w.j  ava 2s .  c  o  m*/
 * @param destDir destination directory, must exist
 * @return
 * @throws RuntimeIOException
 */
@Nonnull
public static Path copyToDirectory(@Nonnull Path srcFile, @Nonnull Path destDir) throws RuntimeIOException {
    Preconditions.checkArgument(Files.exists(srcFile), "Src %s not found");
    Preconditions.checkArgument(!Files.isDirectory(srcFile), "Src %s is a directory");
    Preconditions.checkArgument(Files.exists(destDir), "Dest %s not found");
    Preconditions.checkArgument(Files.isDirectory(destDir), "Dest %s is not a directory");
    try {
        return Files.copy(srcFile, destDir.resolve(srcFile.getFileName()));
    } catch (IOException e) {
        throw new RuntimeIOException("Exception copying " + srcFile.getFileName() + " to " + srcFile, e);
    }
}