Program Java pro získání relativní cesty ze dvou absolutních cest

V tomto příkladu se naučíme získat relativní cestu ze dvou absolutních cest v Javě pomocí metod String, třídy URI a balíčku java.nio.file.

Příklad 1: Získejte relativní cestu ze dvou absolutních cest pomocí třídy 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(); ) ) )

Výstup

 Absolutní cesta 1: C: Users Desktop Programiz Java Time.java Absolutní cesta 2: C: Users Desktop Relativní cesta: Programiz / Java / Time.java

Ve výše uvedeném příkladu máme dvě absolutní cesty pojmenované absolutní cesta1 a absolutní cesta2. Třídu URI jsme použili k převodu absolutních cest na relativní cestu.

  • toURI () - převede Fileobjekt na Uri
  • relativize () - extrahuje relativní cestu porovnáním dvou absolutních cest mezi sebou
  • getPath () - převede Uri na řetězec

Doporučená literatura :

  • Soubor Java
  • Třída URI Java

Příklad 2: Získejte relativní cestu ze dvou absolutních cest pomocí String metod

 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); ) )

Výstup

 Absolutní cesta 1: C: Users Desktop Programiz Java Time.java Absolutní cesta 2: C: Users Desktop Absolutní cesta: Programiz Java Time.java

Ve výše uvedeném příkladu jsme převedli cesty k souborům na řetězce. Všimněte si výrazu,

 absolutePath1.substring(absolutePath2.length())

Zde substring()metoda vrací část absolutní cesty1 počínaje indexem rovným délce absolutní cesty2. To znamená, že řetězec reprezentovaný absolutní cestou2 je odstraněn z absolutní cesty1.

Chcete-li se dozvědět více o tom, jak funguje podřetězec, navštivte podřetězec Java String ().

Příklad 3: Získejte relativní cestu ze dvou absolutních cest pomocí balíčku 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); ) )

Výstup

 Relative Path: ProgramizJavaTime.java

Ve výše uvedeném příkladu jsme použili relativize()metodu Pathrozhraní k získání relativní cesty ze dvou absolutních cest.

Doporučené četby :

  • Třída cest Java
  • Rozhraní Java Path

Zajímavé články...