Dans cet exemple, nous allons apprendre à obtenir le chemin relatif à partir de deux chemins absolus en Java à l'aide des méthodes String, de la classe URI et du package java.nio.file.
Exemple 1: obtenir un chemin relatif à partir de deux chemins absolus à l'aide de la classe URI
import java.io.File; import java.net.URI; class Main ( public static void main(String() args) ( try ( // Two absolute paths File absolutePath1 = new File("C:\Users\Desktop\Programiz\Java\Time.java"); System.out.println("Absolute Path1: " + absolutePath1); File absolutePath2 = new File("C:\Users\Desktop"); System.out.println("Absolute Path2: " + absolutePath2); // convert the absolute path to URI URI path1 = absolutePath1.toURI(); URI path2 = absolutePath2.toURI(); // create a relative path from the two paths URI relativePath = path2.relativize(path1); // convert the URI to string String path = relativePath.getPath(); System.out.println("Relative Path: " + path); ) catch (Exception e) ( e.getStackTrace(); ) ) )
Production
Chemin absolu1: C: Users Desktop Programiz Java Time.java Chemin absolu2: C: Users Desktop Chemin relatif: Programiz / Java / Time.java
Dans l'exemple ci-dessus, nous avons deux chemins absolus nommés absolutePath1 et absolutePath2. Nous avons utilisé la classe URI pour convertir les chemins absolus en chemin relatif.
- toURI () - convertit l'
File
objet en Uri - relativize () - extrait le chemin relatif en comparant deux chemins absolus entre eux
- getPath () - convertit l'URI en une chaîne
Lecture recommandée :
- Fichier Java
- Classe URI Java
Exemple 2: obtenir un chemin relatif à partir de deux chemins absolus à l'aide des méthodes String
import java.io.File; class Main ( public static void main(String() args) ( // Create file objects File file1 = new File("C:\Users\Desktop\Programiz\Java\Time.java"); File file2 = new File("C:\Users\Desktop"); // convert file objects to string String absolutePath1 = file1.toString(); System.out.println("Absolute Path1: " + absolutePath1); String absolutePath2 = file2.toString(); System.out.println("Absolute Path2: " + absolutePath2); // get the relative path String relativePath = absolutePath1.substring(absolutePath2.length()); System.out.println("Absolute Path: " + relativePath); ) )
Production
Chemin absolu1: C: Users Desktop Programiz Java Time.java Chemin absolu2: C: Users Desktop Chemin absolu: Programiz Java Time.java
Dans l'exemple ci-dessus, nous avons converti les chemins de fichiers en chaînes. Remarquez l'expression,
absolutePath1.substring(absolutePath2.length())
Ici, la substring()
méthode retourne la partie de absolutePath1 à partir de l' index égal à la longueur de absolutePath2. Autrement dit, la chaîne représentée par absolutePath2 est supprimée de absolutePath1.
Pour en savoir plus sur le fonctionnement de substring, visitez Java String substring ().
Exemple 3: obtenir un chemin relatif à partir de deux chemins absolus à l'aide du package java.nio.file
import java.nio.file.Path; import java.nio.file.Paths; class Main ( public static void main(String() args) ( // Create file objects Path absolutePath1 = Paths.get("C:\Users\Desktop\Programiz\Java\Time.java"); Path absolutePath2 = Paths.get("C:\Users\Desktop"); // convert the absolute path to relative path Path relativePath = absolutePath2.relativize(absolutePath1); System.out.println("Relative Path: " + relativePath); ) )
Production
Relative Path: ProgramizJavaTime.java
Dans l'exemple ci-dessus, nous avons utilisé la relativize()
méthode de l' Path
interface pour obtenir un chemin relatif à partir de deux chemins absolus.
Lectures recommandées :
- Classe Java Paths
- Interface du chemin Java