Java Utililty Methods Path File Name nio

List of utility methods to do Path File Name nio

Description

The list of methods to do Path File Name nio are organized into topic(s).

Method

StringreadFileFromPath(String filename)
Returns file content as string, reading from a path.
return readFileFromPath(filename, null);
StringrelativizePath(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();
...
voidrename(Path origin, Path destination)
Renames the origin path denoted by the new destination path.
Files.move(origin, destination, StandardCopyOption.REPLACE_EXISTING);
voidrenameFile(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();
voidrenameLogFile(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");
...
booleansaveFileFromByteArray(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();
...
FilesaveNamesToFile(Map namesMap, Path namesFile)
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;
voidsaveToFile(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();
PathshortenFileName(Path file, List dirs)
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;
voidstoreFile(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) {
...