List of utility methods to do Path File Name nio
String | readFileFromPath(String filename) Returns file content as string, reading from a path. return readFileFromPath(filename, null);
|
String | relativizePath(Path relativeTo, String filename) relativize Path if (relativeTo == null) { return filename; Path path = getAbsolutePath(filename); if (path.startsWith(relativeTo)) { return relativeTo.relativize(path).toString(); } else { return path.toString(); ... |
void | rename(Path origin, Path destination) Renames the origin path denoted by the new destination path. Files.move(origin, destination, StandardCopyOption.REPLACE_EXISTING); |
void | renameFile(Path filePath, String postfix) rename File try { Path targetPath = Paths.get(filePath.toString() + postfix); Files.move(filePath, targetPath, REPLACE_EXISTING); System.out.println("INFO: File " + filePath.toString() + "is renamed to " + targetPath.toString()); } catch (IOException ioe) { ioe.printStackTrace(); |
void | renameLogFile(Path original) This method renames the file denoted by the file parameter by appending the current date to it and a counter if there is already a file with that name. if (!Files.exists(original)) { return; String fullFileName = original.getFileName().toString(); int lastDot = fullFileName.lastIndexOf("."); String nameBeforeDot = fullFileName.substring(0, lastDot); String nameAfterDot = fullFileName.substring(lastDot + 1, fullFileName.length()); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd"); ... |
boolean | saveFileFromByteArray(String filename, String pathDirectory, byte[] data) save File From Byte Array Path path = Paths.get(pathDirectory); if (Files.isDirectory(path)) { Path completePathToSaveFile = Paths.get(path.toString(), filename); if (!Files.exists(completePathToSaveFile)) { try { Files.write(completePathToSaveFile, data); } catch (Exception e) { e.printStackTrace(); ... |
File | saveNamesToFile(Map save Names To File final File names = namesFile.toFile(); try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(names))) { for (Map.Entry<String, Integer> entry : namesMap.entrySet()) { bufferedWriter.write(String.valueOf(entry.getValue())); bufferedWriter.write("\t"); bufferedWriter.write(entry.getKey()); bufferedWriter.newLine(); return names; |
void | saveToFile(String content, String directoryPath, String filename) save To File createDirIfDoesNotExist(directoryPath); try { Files.write(Paths.get(directoryPath + filename), content.getBytes()); } catch (IOException e) { e.printStackTrace(); |
Path | shortenFileName(Path file, List Converts an absolute file to a relative one, if possible. if (!file.isAbsolute()) { return file; for (Path dir : dirs) { if (file.startsWith(dir)) { return dir.relativize(file); return file; |
void | storeFile(String fileName, InputStream inputStream, Path targetPath) store File if (fileName == null) { fileName = targetPath.toString(); String extension = fileName.substring(fileName.lastIndexOf(".")); switch (extension) { case ".gno": { try (ZipInputStream zipInputStream = new ZipInputStream(inputStream)) { while (zipInputStream.getNextEntry() != null) { ... |