I/O Débutant

Utilisez Path.of() — la méthode factory moderne dans l'interface Path.

✕ Java 8
Path path = Paths.get("src", "main",
    "java", "App.java");
✓ Java 11+
var path = Path.of("src", "main",
    "java", "App.java");
Un problème avec ce code ? Dites-le nous.
📐

API cohérente

Suit le pattern factory .of() comme List.of(), Set.of().

📖

Facile à découvrir

Se trouve dans le type Path lui-même, pas dans une classe Paths séparée.

🧹

Une classe de moins

Pas besoin d'importer la classe utilitaire Paths.

Ancienne Approche
Paths.get()
Approche Moderne
Path.of()
Depuis JDK
11
Difficulté
Débutant
Méthode factory Path.of()
Disponible

Disponible depuis JDK 11 (septembre 2018)

Path.of() est une méthode factory ajoutée directement à l'interface Path, remplaçant la classe utilitaire Paths séparée. Elle est plus facile à découvrir et cohérente avec List.of(), Map.of(), etc.

Partager 𝕏 🦋 in