name: portada class: portada-slide, center, middle # Fitxers .footnote[Joan Puigcerver Ibáñez] --- layout: true class: regular-slide .right[.logo[]] --- # Classe Path - Path - [Javadoc Path](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/nio/file/Path.html) >>> An object that may be used to locate a file in a file system. ``` Path path; ``` --- # Classe de funcions - Paths - Compendi de funcions estàtiques sobre paths (Path) - [Javadoc Paths](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/nio/file/Paths.html) - Files - Compendi de funcions estàtiques sobre fitxers i carpetes - [Javadoc Files](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/nio/file/Files.html) --- # Crear Path #### Javadoc Paths ``` static Path get(String first, String... more) ``` #### Exemple ``` Path path = Paths.get("/home/user/somefile.txt"); Path path2 = Paths.get("/home/user", "somefile.txt"); // Path realtiu Path relative = Paths.get("./somefile.txt"); Path relative2 = Paths.get("../somefile.txt"); ``` --- # Composar paths #### Javadoc Path ``` Path getParent() Path resolve(String other) Path resolve(Path other) ``` #### Exemple ``` Path parentPath = path.getParent(); Path subPath = path.resolve("some/sub/apth"); ``` --- # Existeix un path? #### Javadoc Path ``` static boolean exists(Path path, LinkOption... options) ``` #### Exemple ``` Path path = Path.of("/home/user/some_file"); boolean exists = Files.exists(path); System.out.println("exists = " + exists); ``` --- # Crear fitxers i directoris #### Javadoc Files ``` static Path createDirectories(Path dir, FileAttribute>... attrs) static Path createDirectory(Path dir, FileAttribute>... attrs) static Path createFile(Path path, FileAttribute>... attrs) ``` #### Exemple ``` Path newDirectory = Files.createDirectories(path); Path newFileDestination = newDirectory.resolve("emptyFile.txt"); Path newFile = Files.createFile(newFileDestination); ``` --- # Crear fitxer temporal #### Javadoc Files ``` static Path createTempDirectory(String prefix, FileAttribute>... attrs) static Path createTempDirectory(Path dir, String prefix, FileAttribute>... attrs) static Path createTempFile(String prefix, String suffix, FileAttribute>... attrs) static Path createTempFile(Path dir, String prefix, String suffix, FileAttribute>... attrs) ``` #### Exemple ``` Path tempFile = Files.createTempFile("somePrefix", ".jpg"); Path tmpDirectory = Files.createTempDirectory("prefix"); ``` --- # Moure fitxer #### Javadoc Files ``` static Path move(Path source, Path target, CopyOption... options) ``` #### Exemple ``` // target ha de ser el destí exacte, no la carpeta no posar el fitxer Files.move(source, target); // El següent és incorrecte Files.move(source, Path.get("file.txt", Path.of("/home/user")); ``` --- # Eliminar fitxer - No és recursiu (no elimina carpetes plenes). #### Javadoc Files ``` static void delete(Path path) ``` #### Exemple ``` Files.delete(tmpDir); ``` --- # Llistar fitxer a un directori #### Javadoc Files ``` static Stream
list(Path dir) ``` #### Exemple ``` try (Stream
filesStream = Files.list(tmpDirectory)) { List
files = filesStream.collect(toList()); } ``` --- # Llistar fitxers recursivament #### Javadoc Files ``` static Stream
walk(Path start, int maxDepth, FileVisitOption... options) static Stream
walk(Path start, FileVisitOption... options) ``` #### Exemple ``` try (Stream
filesStream = Files.walk(tmpDirectory)) { List
files = filesStream.collect(toList()); } ``` --- # Bibliografia - https://www.marcobehler.com/guides/java-files ??? # Paths relatius / absolut ``` // Consulta si és absolut boolean isAbsolute = path.isAbsolute(); // Convertir a absolut Path absolute = relative.toAbsolutePath(); // passar a relatiu Path relative = absolute.relativize(root_path); ```