I want to find a relative path in a situation using Path

I don't think it's very frequent, but sometimes you want to start from one path and find the relative path of another. In Java7 or later, I think that java.nio.file.Path is often used for file operations (original path operations), but if you want to find a relative path while using __Path Use Path :: relativize for. __

The JavaDoc will be the following URL: https://docs.oracle.com/javase/jp/8/docs/api/java/nio/file/Path.html#relativize-java.nio.file.Path-

It is a little difficult to understand how to use the API, but specify the path for which you want to find the relative path as an argument and the path you want to use as the starting point for the receiver.

The following is a sample to find the relative path of the file / a / b / c / directory / e / file starting from the directory / a / b / c / directory. It is assumed that the file / a / b / c / directory / e / file is stored in the directory / a / b / c / directory.

package main;

import java.nio.file.Paths;

public class Main {
    public static void main(String[] args) {
        var directory = Paths.get("/a/b/c/directory");
        var file = Paths.get("/a/b/c/directory/e/file");
        
        System.out.println(directory.relativize(file)); // => e\file
    }    
}

Recommended Posts

I want to find a relative path in a situation using Path
I want to use a little icon in Rails
I want to define a function in Rails Console
I want to click a GoogleMap pin in RSpec
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
I want to create a Parquet file even in Ruby
[Ruby] I want to put an array in a variable. I want to convert to an array
I want to develop a web application!
I want to write a nice build.gradle
I want to write a unit test!
I want to use @Autowired in Servlet
I want to select multiple items with a custom layout in Dialog
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (PowerMockito edition)
[Note] I want to get in reverse order using afterLast with JdbcTemplate
I want to display a PDF in Chinese (Korean) with thin reports
I tried to make a talk application in Java using AI "A3RT"
I want to ForEach an array with a Lambda expression in Java
"Teacher, I want to implement a login function in Spring" ① Hello World
I want to send an email in Java.
I want to use arrow notation in Ruby
[Ruby] I want to do a method jump!
I want to pass APP_HOME to logback in Gradle
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
I want to simply write a repeating string
I want to design a structured exception handling
rsync4j --I want to touch rsync in Java.
[Xcode] I want to manage images in folders
I want to be eventually even in kotlin
I tried to implement a server using Netty
I want to get the value in Ruby
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (Javassist second decoction)
I want to be able to read a file using refile with administrate [rails6]
I made a sample of how to write delegate in SwiftUI 2.0 using MapKit
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (black magic edition)
I tried using a database connection in Android development
I want to use Combine in UIKit as well.
I want to call a method of another class
I want to do something like "cls" in Java
I want to embed any TraceId in the log
I want to use fish shell in Laradock too! !!
I want to use ES2015 in Java too! → (´ ・ ω ・ `)
I want to judge the range using the monthly degree
I tried to create a Clova skill in Java
I want to monitor a specific file with WatchService
I tried to make a login function in Java
How to convert A to a and a to A using AND and OR in Java
I want to stop snake case in table definition
I want to call the main method using reflection
I want to add a reference type column later
I want to connect to Heroku MySQL from a client
I want to create a generic annotation for a type
I want to add a delete function to the comment function
I tried to find out what changed in Java 9
I want to issue a connection when a database is created using Spring and MyBatis
I want to change the path after new registration after logging in with multiple devises.
I want to use PowerMock in a class that combines parameterized tests and ordinary tests
[Rails] I want to display the link destination of link_to in a separate tab
I want to implement it additionally while using kotlin on a site running Java
I want to find the MD5 checksum of a file in Java and get the result as a string in hexadecimal notation.
I want to set the conditions to be displayed in collection_check_boxes
I want to convert characters ...