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:objective.taskboard.utils.ZipUtilsTest.java

@Test
public void whenZipAndUnzipToADestinyWithNonexistentParentFolder_BothShouldBeGenerated()
        throws IOException, URISyntaxException {
    Path source = Paths.get(getClass().getResource("zipUtilsTest.json").toURI());
    Path destiny = Paths.get("parent", "zipAndUnzipFile.zip");
    Path assertDir = Paths.get("parentAssert", "zipAndUnzipFile");

    try {//from   w  ww.j a  v  a 2  s  .  c  om
        zip(source, destiny);

        assertTrue("Zip file should be generated", exists(destiny));
        assertThat(size(destiny), greaterThan(0L));

        unzip(destiny.toFile(), assertDir);

        File file = assertDir.resolve(source.getFileName()).toFile();
        assertTrue("JSON file should be generated", file.exists());

        String actual = IOUtils.toString(asResource(file).getInputStream(), ENCODE_UTF_8);
        String expected = IOUtils.toString(asResource(source).getInputStream(), ENCODE_UTF_8);

        assertEquals("JSON", expected, actual);
    } finally {
        deleteQuietly(destiny.getParent().toFile());
        deleteQuietly(assertDir.getParent().toFile());
    }
}

From source file:dk.dma.ais.downloader.QueryService.java

/**
 * Asynchronously loads the given file//from   ww w.ja v a2 s. c  o m
 * @param url the URL to load
 * @param path the path to save the file to
 */
private Future<Path> asyncLoadFile(final String url, final Path path) {
    Callable<Path> job = () -> {
        long t0 = System.currentTimeMillis();

        // For the resulting file, drop the ".download" suffix
        String name = path.getFileName().toString();
        name = name.substring(0, name.length() - DOWNLOAD_SUFFIX.length());

        try {

            // Set up a few timeouts and fetch the attachment
            URLConnection con = new URL(url).openConnection();
            con.setConnectTimeout(60 * 1000); // 1 minute
            con.setReadTimeout(60 * 60 * 1000); // 1 hour

            if (!StringUtils.isEmpty(authHeader)) {
                con.setRequestProperty("Authorization", authHeader);
            }

            try (ReadableByteChannel rbc = Channels.newChannel(con.getInputStream());
                    FileOutputStream fos = new FileOutputStream(path.toFile())) {
                fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
            }
            log.info(String.format("Copied %s -> %s in %d ms", url, path, System.currentTimeMillis() - t0));

        } catch (Exception e) {
            log.log(Level.SEVERE, "Failed downloading " + url + ": " + e.getMessage());

            // Delete the old file
            if (Files.exists(path)) {
                try {
                    Files.delete(path);
                } catch (IOException e1) {
                    log.finer("Failed deleting old file " + path);
                }
            }

            // Save an error file
            Path errorFile = path.getParent().resolve(name + ".err.txt");
            try (PrintStream err = new PrintStream(new FileOutputStream(errorFile.toFile()))) {
                e.printStackTrace(err);
            } catch (IOException ex) {
                log.finer("Failed generating error file " + errorFile);
            }
            return errorFile;
        }

        Path resultPath = path.getParent().resolve(name);
        try {
            Files.move(path, resultPath);
        } catch (IOException e) {
            log.log(Level.SEVERE, "Failed renaming path " + path + ": " + e.getMessage());
        }
        return resultPath;
    };

    log.info("Submitting new job: " + url);
    return processPool.submit(job);
}

From source file:objective.taskboard.utils.ZipUtilsTest.java

@Test
public void whenZipAndUnzipFolder_BothShouldBeGenerated() throws IOException {
    Path source = createTempDirectory("zipAndUnzipFolder");
    Path sourceFile = createTempFile(source, "zipAndUnzipFolder", EXTENSION_JSON);
    Path destiny = createTempFile("zipAndUnzipFolder", EXTENSION_ZIP);
    Path assertDir = createTempDirectory("zipAndUnzipFolderAssert");

    try {/*from ww  w  .  ja v  a2s.  c  om*/
        zip(source, destiny);

        assertTrue("Zip file should be generated", exists(destiny));
        assertThat(size(destiny), greaterThan(0L));

        unzip(destiny.toFile(), assertDir);

        File file = assertDir.resolve(sourceFile.getFileName()).toFile();
        assertTrue("JSON file should be generated", file.exists());

        String actual = IOUtils.toString(asResource(file).getInputStream(), ENCODE_UTF_8);
        String expected = IOUtils.toString(asResource(sourceFile).getInputStream(), ENCODE_UTF_8);

        assertEquals("JSON", expected, actual);
    } finally {
        deleteQuietly(source.toFile());
        deleteQuietly(destiny.toFile());
        deleteQuietly(assertDir.toFile());
    }
}

From source file:onl.area51.filesystem.ftp.client.FTPClient.java

/**
 * Store a Path to the remote server/*from w ww  . ja va  2 s.  c om*/
 * <p>
 * @param target
 *               <p>
 * @throws IOException
 */
default void store(Path target) throws IOException {
    store(target.getFileName().toString(), target);
}

From source file:com.movilizer.mds.webservice.services.UploadFileService.java

protected UploadResponse uploadDocumentSync(Path documentFilePath, long systemId, String password,
        String documentPool, String documentKey, String language, String ackKey,
        Integer connectionTimeoutInMillis) {
    if (logger.isDebugEnabled()) {
        logger.debug(String.format(Messages.PERFORMING_UPLOAD, systemId));
    }/*from   w  w  w.j  a v a 2  s.c o  m*/
    UploadResponse result = uploadSync(
            movilizerUpload.getForm(documentFilePath.toFile(), systemId, password, documentPool, documentKey,
                    language, getSuffixFromFilename(documentFilePath.getFileName()), ackKey),
            connectionTimeoutInMillis);
    if (logger.isInfoEnabled()) {
        logger.info(Messages.UPLOAD_COMPLETE);
    }
    return result;
}

From source file:com.facebook.buck.java.JarDirectoryStepTest.java

@Test
public void shouldNotComplainWhenDuplicateDirectoryNamesAreAdded() throws IOException {
    Path zipup = folder.newFolder();

    Path first = createZip(zipup.resolve("first.zip"), "dir/example.txt", "dir/root1file.txt");
    Path second = createZip(zipup.resolve("second.zip"), "dir/example.txt", "dir/root2file.txt",
            "com/example/Main.class");

    JarDirectoryStep step = new JarDirectoryStep(new ProjectFilesystem(zipup), Paths.get("output.jar"),
            ImmutableSet.of(first.getFileName(), second.getFileName()), "com.example.Main",
            /* manifest file */ null);

    ExecutionContext context = TestExecutionContext.newInstance();

    int returnCode = step.execute(context);

    assertEquals(0, returnCode);//from  w ww.  j  av a2 s.  c o m

    Path zip = zipup.resolve("output.jar");

    // The three below plus the manifest and Main.class.
    assertZipFileCountIs(5, zip);
    assertZipContains(zip, "dir/example.txt", "dir/root1file.txt", "dir/root2file.txt");
}

From source file:com.datafibers.kafka.connect.FileGenericSourceTask.java

@Override
public List<SourceRecord> poll() throws InterruptedException {
    if (!inProgressPaths.isEmpty()) {
        try {//ww w . ja  v a 2  s  .  c  om
            Path currentPath = inProgressPaths.remove(0);
            processedPaths.add(currentPath);
            filename = currentPath.getFileName().toString();
            fileInProcessing = FileUtils.getFile(currentPath.toString() + FILENAME_EXT_PROCESSING);
            fileProcessed = FileUtils.getFile(currentPath.toString() + FILENAME_EXT_PROCESSED);
            FileUtils.moveFile(FileUtils.getFile(currentPath.toString()), fileInProcessing);

            stream = new FileInputStream(fileInProcessing);

            Map<String, Object> offset = context.offsetStorageReader()
                    .offset(Collections.singletonMap(FILENAME_FIELD, filename));
            if (offset != null && !overwrite) {
                log.info("Found previous offset, will not process {}", filename);
                return null;
            } else
                streamOffset = 0L;

            reader = new BufferedReader(new InputStreamReader(stream));
            log.info("Opened {} for reading", filename);
        } catch (IOException e) {
            throw new ConnectException(String.format("Unable to open file %", filename), e);
        }
    } else {
        log.warn("********* Waiting for file that meets the glob criteria! *********");
        synchronized (this) {
            this.wait(interval);
            findMatch();
        }
        return null;
    }

    ArrayList<SourceRecord> records = new ArrayList<SourceRecord>();
    //StringBuilder fileContent = new StringBuilder();

    try {
        final BufferedReader readerCopy;
        synchronized (this) {
            readerCopy = reader;
        }
        if (readerCopy == null)
            return null;

        int nread = 0;
        while (readerCopy.ready()) {
            nread = readerCopy.read(buffer, offset, buffer.length - offset);
            log.trace("Read {} bytes from {}", nread, filename);

            if (nread > 0) {
                offset += nread;
                if (offset == buffer.length) {
                    char[] newbuf = new char[buffer.length * 2];
                    System.arraycopy(buffer, 0, newbuf, 0, buffer.length);
                    buffer = newbuf;
                }

                String line;
                do {
                    line = extractLine();
                    if (line != null) {
                        line = line.trim();
                        log.trace("Read a line from {}", filename);
                        if (records == null)
                            records = new ArrayList<>();
                        /*                records.add(new SourceRecord(offsetKey(filename), offsetValue(streamOffset), topic,
                                              dataSchema, structDecodingRoute(line, filename)));*/
                        if (schemaValidate) {
                            records.add(new SourceRecord(offsetKey(filename), offsetValue(streamOffset), topic,
                                    dataSchema, structDecodingRoute(line, filename)));
                        } else {
                            log.info("STRING SCHEMA Processing");
                            records.add(new SourceRecord(offsetKey(filename), offsetValue(streamOffset), topic,
                                    Schema.STRING_SCHEMA, line));
                        }

                    }
                    new ArrayList<SourceRecord>();
                } while (line != null);
            }
        }

        // Finish processing and rename as processed.
        FileUtils.moveFile(fileInProcessing, fileProcessed);

        if (nread <= 0)
            synchronized (this) {
                this.wait(1000);
            }

        return records;

    } catch (IOException e) {
        throw new ConnectException(String.format("Unable to read file %", filename), e);
    }

}

From source file:fr.duminy.jbackup.core.archive.CompressorTest.java

@Theory
public void testCompress(Data data, boolean useListener, EntryType entryType) throws Throwable {
    // preparation of archiver & mocks
    boolean relativeEntries = EntryType.RELATIVE.equals(entryType);
    ErrorType errorType = ErrorType.NO_ERROR;
    ArchiveOutputStream mockOutput = mock(ArchiveOutputStream.class);
    ArgumentCaptor<String> pathArgument = ArgumentCaptor.forClass(String.class);
    doAnswer(new Answer() {
        @Override/*from w  ww.j  a va  2  s  . co m*/
        public Object answer(InvocationOnMock invocation) throws Throwable {
            InputStream input = (InputStream) invocation.getArguments()[1];
            IOUtils.copy(input, new ByteArrayOutputStream());
            return null;
        }
    }).when(mockOutput).addEntry(pathArgument.capture(), any(InputStream.class));

    ArchiveFactory mockFactory = createMockArchiveFactory(mockOutput);
    Path baseDirectory = createBaseDirectory();
    TaskListener listener = useListener ? mock(TaskListener.class) : null;

    final ArchiveParameters archiveParameters = new ArchiveParameters(createArchivePath(), relativeEntries);
    Map<Path, List<Path>> expectedFilesBySource = data.createFiles(baseDirectory, archiveParameters);
    List<Path> expectedFiles = mergeFiles(expectedFilesBySource);
    Map<String, Path> expectedEntryToFile = new HashMap<>();

    try {
        errorType.setUp(expectedFiles);

        // test compression
        compress(mockFactory, archiveParameters, listener, null);

        // assertions
        verify(mockFactory, times(1)).create(any(OutputStream.class));
        verifyNoMoreInteractions(mockFactory);

        for (Map.Entry<Path, List<Path>> sourceEntry : expectedFilesBySource.entrySet()) {
            for (Path file : sourceEntry.getValue()) {
                assertTrue("test self-check:  files must be absolute", file.isAbsolute());
                final String expectedEntry;
                if (relativeEntries) {
                    Path source = sourceEntry.getKey();
                    if (Files.isDirectory(source)) {
                        expectedEntry = source.getParent().relativize(file).toString();
                    } else {
                        expectedEntry = file.getFileName().toString();
                    }
                } else {
                    expectedEntry = file.toString();
                }
                expectedEntryToFile.put(expectedEntry, file);
                verify(mockOutput, times(1)).addEntry(eq(expectedEntry), any(InputStream.class));
            }
        }
        verify(mockOutput, times(1)).close();
        verifyNoMoreInteractions(mockOutput);
    } catch (Throwable t) {
        errorType.verifyExpected(t);
    } finally {
        errorType.tearDown(expectedFiles);
    }

    assertThatNotificationsAreValid(listener, pathArgument.getAllValues(), expectedEntryToFile, errorType);
}

From source file:dk.dma.ais.lib.FileConvert.java

/** {@inheritDoc} */
@Override/*from  w  w w  .  j  a  v  a2s.  c o m*/
protected void run(Injector injector) throws Exception {
    configureFileEnding();

    final EConsumer<String> consumer = new EConsumer<String>() {

        @Override
        public void accept(String s) throws IllegalArgumentException, IllegalAccessException,
                NoSuchFieldException, SecurityException, IOException, InterruptedException {
            Path path = Paths.get(s);
            LOG.debug("Started processing file " + path);

            Path endPath;
            if (keepFileStructure) {
                Path relative;
                relative = path;
                endPath = Paths.get(Paths.get(convertTo).toString(), relative.toString());
                new File(endPath.toString()).mkdirs();
            } else {
                endPath = Paths.get("");
            }

            String filename = path.getFileName().toString();
            if (!filename.endsWith(fileEnding))
                filename = FilenameUtils.removeExtension(filename) + fileEnding;
            Path filePath = Paths.get(endPath.toString(), filename);

            LOG.debug("Output File: " + filePath.toString());

            final OutputStream fos = new FileOutputStream(filePath.toString()); // 2

            final boolean createSituationFolder = !StringUtils.isBlank(kmzSnapshotAt);
            final long snapshotAtEpochMillis = createSituationFolder
                    ? LocalDateTime.parse(kmzSnapshotAt, formatter).toInstant(ZoneOffset.UTC).toEpochMilli()
                    : -1;

            OutputStreamSink<AisPacket> sink;
            if ("kmz".equals(outputSinkFormat)) {
                //AisPacketKMZOutputSink(filter, createSituationFolder, createMovementsFolder, createTracksFolder, isPrimaryTarget, isSecondaryTarget, triggerSnapshot, snapshotDescriptionSupplier, movementInterpolationStep, supplyTitle, supplyDescription, iconHrefSupplier);
                sink = AisPacketOutputSinks.newKmzSink(e -> true, // this.filter = e -> true;
                        createSituationFolder, // this.createSituationFolder = true;
                        true, // createMovementsFolder = true;
                        true, // this.createTracksFolder = true;
                        e -> kmzPrimaryMmsi <= 0 ? false : e.tryGetAisMessage().getUserId() == kmzPrimaryMmsi, // this.isPrimaryTarget = e -> false;
                        e -> kmzSecondaryMmsi <= 0 ? false
                                : e.tryGetAisMessage().getUserId() == kmzSecondaryMmsi, // this.isSecondaryTarget = e -> false;
                        e -> e.getBestTimestamp() >= snapshotAtEpochMillis, // this.triggerSnapshot = e -> false;
                        () -> "Situation at " + kmzSnapshotAt, // this.snapshotDescriptionSupplier = null;
                        () -> 10, // this.title = defaultTitleSupplier;
                        () -> "description", // this.description = defaultDescriptionSupplier;
                        () -> "10", //this.movementInterpolationStep = defaultMovementInterpolationStepSupplier;
                        (shipTypeCargo, navigationalStatus) -> "" // this.iconHrefSupplier = defaultIconHrefSupplier;
                );

            } else
                sink = AisPacketOutputSinks.getOutputSink(outputSinkFormat, columns);

            sink.closeWhenFooterWritten();

            AisPacketReader apis = AisPacketReader.createFromFile(path, false);

            apis.writeTo(fos, sink);
            apis.close();
            fos.close();
        }
    };

    /*
     * Creates a pool of executors, 4 threads. Each thread will open a file using an aispacket reader, 10000 files can be
     * submitted to the queue, afterwards the calling thread will execute the job instead.
     */
    ThreadPoolExecutor threadpoolexecutor = new ThreadPoolExecutor(4, 4, 1, TimeUnit.SECONDS,
            new ArrayBlockingQueue<>(10000), new ThreadPoolExecutor.CallerRunsPolicy());
    for (final String s : sources) {
        threadpoolexecutor.execute(() -> {
            try {
                consumer.accept(s);
            } catch (Exception e) {
                e.printStackTrace();
            }

        });
    }

    threadpoolexecutor.shutdown();
    threadpoolexecutor.awaitTermination(999, TimeUnit.DAYS);
}

From source file:gov.usgs.cida.coastalhazards.rest.data.util.MetadataUtilTest.java

@Test
public void testFilePathBuilder() throws FactoryException, IOException {
    final String TEMP_FILE_SUBDIRECTORY_PATH = "cch-temp";
    //java.nio.file.Path TEMP_FILE_SUBDIRECTORY =Files.createDirectory(Paths.get(TEMP_FILE_SUBDIRECTORY_PATH));
    //  TEMP_FILE_SUBDIRECTORY = Files.createDirectory(Paths.get(TEMP_FILE_SUBDIRECTORY_PATH));
    String tempPath = FileUtils.getTempDirectoryPath() + TEMP_FILE_SUBDIRECTORY_PATH;
    java.nio.file.Path TEMP_FILE_SUBDIRECTORY = Paths.get(tempPath);

    String fileDir = TEMP_FILE_SUBDIRECTORY.toFile().toString();
    System.out.println("getTempFileUtils: " + fileDir);

    String tempDir = System.getProperty("java.io.tmpdir") + "cch-temp";
    System.out.println("get System temp file: " + tempDir);

    java.nio.file.Path path = Paths.get(System.getProperty("java.io.tmpdir"));
    try (DirectoryStream<java.nio.file.Path> newDirectoryStream = Files.newDirectoryStream(path,
            TEMP_FILE_SUBDIRECTORY_PATH + "*")) {
        for (final java.nio.file.Path newDirectoryStreamItem : newDirectoryStream) {
            System.out.println("DIR that would be DELETED: " + newDirectoryStreamItem.getFileName());
            // Files.delete(newDirectoryStreamItem);
        }//from  w  ww .  j av  a  2s.c  om
    } catch (final Exception e) { // empty
        System.out.println(e);
    }

    assertEquals(fileDir, tempDir);
}