# java.nio.file

***

**1. Create a Path:**

```java
Path path = Paths.get("my/file.txt");
```

**2. Get Absolute Path:**

```java
Path absolutePath = path.toAbsolutePath();
```

**3. Get Directory Name:**

```java
Path directory = path.getParent();
```

**4. Get File Name:**

```java
Path fileName = path.getFileName();
```

**5. Check if File Exists:**

```java
boolean exists = Files.exists(path);
```

**6. Create a Directory:**

```java
Files.createDirectories(path);
```

**7. Create a File:**

```java
Files.createFile(path);
```

**8. Delete a File:**

```java
Files.delete(path);
```

**9. Copy a File:**

```java
Files.copy(sourcePath, targetPath);
```

**10. Move a File:**

```java
Files.move(sourcePath, targetPath);
```

**11. Read a Text File:**

```java
List<String> lines = Files.readAllLines(path);
```

**12. Write a Text File:**

```java
Files.write(path, lines);
```

**13. Create a Random Access File:**

```java
RandomAccessFile raf = new RandomAccessFile(path, "rw");
```

**14. Read Bytes from a File:**

```java
byte[] bytes = Files.readAllBytes(path);
```

**15. Write Bytes to a File:**

```java
Files.write(path, bytes);
```

**16. Check if File is Regular:**

```java
boolean isRegularFile = Files.isRegularFile(path);
```

**17. Check if File is Hidden:**

```java
boolean isHidden = Files.isHidden(path);
```

**18. Get File Size:**

```java
long size = Files.size(path);
```

**19. Get File Last Modified Time:**

```java
Instant lastModified = Files.getLastModifiedTime(path);
```

**20. Set File Last Modified Time:**

```java
Files.setLastModifiedTime(path, Instant.now());
```

**21. Create a Temp File:**

```java
Path tempFile = Files.createTempFile("prefix", "suffix");
```

**22. Create a Temp Directory:**

```java
Path tempDir = Files.createTempDirectory("prefix");
```

**23. Walk a File Tree:**

```java
Files.walk(path)
  .forEach(System.out::println);
```

**24. Find Files with a Glob:**

```java
Files.find(path, 10,
  (p, a) -> p.getFileName().toString().startsWith("file"))
  .forEach(System.out::println);
```

**25. Find Files with a Filter:**

```java
Files.newDirectoryStream(path, p -> p.toString().endsWith(".txt"))
  .forEach(System.out::println);
```

**26. Copy a Directory:**

```java
Files.walk(sourceDir)
  .forEach(p -> Files.copy(p, targetDir.resolve(p)));
```

**27. Zip a Directory:**

```java
try (ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream(zipFile))) {
  Files.walk(dir)
    .filter(Files::isRegularFile)
    .forEach(p -> {
      zos.putNextEntry(new ZipEntry(p.toString()));
      Files.copy(p, zos);
    });
}
```

**28. Unzip a File:**

```java
try (ZipInputStream zis = new ZipInputStream(Files.newInputStream(zipFile))) {
  ZipEntry entry;
  while ((entry = zis.getNextEntry()) != null) {
    Path targetPath = Paths.get(entry.getName());
    Files.copy(zis, targetPath);
  }
}
```

**29. Download a File from a URL:**

```java
URL url = new URL("http://example.com/file.txt");
Path targetPath = Paths.get("file.txt");
Files.copy(url.openStream(), targetPath);
```

**30. Upload a File to a Server:**

```java
URLConnection urlConnection = new URL("http://example.com/upload").openConnection();
urlConnection.setDoOutput(true);
Files.copy(sourcePath, urlConnection.getOutputStream());
```

**31. Create a Symbolic Link:**

```java
Files.createSymbolicLink(targetPath, linkPath);
```

**32. Get File Owner:**

```java
UserPrincipal owner = Files.getOwner(path);
```

**33. Set File Owner:**

```java
Files.setOwner(path, owner);
```

**34. Get File Permissions:**

```java
Set<PosixFilePermission> permissions = Files.getPosixFilePermissions(path);
```

**35. Set File Permissions:**

```java
Files.setPosixFilePermissions(path, permissions);
```

**36. Check if File is Symbolic Link:**

```java
boolean isSymbolicLink = Files.isSymbolicLink(path);
```

**37. Get File Symbolic Link Target:**

```java
Path target = Files.readSymbolicLink(path);
```

**38. Create a File Lock:**

```java
try (FileLock lock = Files.newOutputStream(path).lock()) {
  // Do something with the file
}
```

**39. Check if File is Open:**

```java
boolean isOpen = Files.isOpen(path);
```

**40. Create a Watch Service:**

```java
try (WatchService watchService = FileSystems.getDefault().newWatchService()) {
  path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE);
  while (true) {
    WatchKey key = watchService.take();
    for (WatchEvent<?> event : key.pollEvents()) {
      // Handle event
    }
  }
}
```

**41. Create a File Attribute View:**

```java
try (FileAttributeView view = Files.getFileAttributeView(path, BasicFileAttributeView.class)) {
  // Get or set file attributes
}
```

**42. Get File System Root Directory:**

```java
Path root = path.getFileSystem().getRootDirectories().iterator().next();
```

**43. Get File System Separator:**

```java
String separator = path.getFileSystem().getSeparator();
```

**44. Get File System Name:**

```java
String name = path.getFileSystem().name();
```

**45. Create a Secure Channel:**

```java
try (FileChannel channel = FileChannel.open(path, StandardOpenOption.READ, StandardOpenOption.WRITE)) {
  channel.write(ByteBuffer.wrap("Hello World".getBytes()));
}
```

**46. Map a File to Memory:**

```java
try (FileChannel channel = FileChannel.open(path, StandardOpenOption.READ)) {
  MappedByteBuffer mbb = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
}
```

**47. Create a ByteChannel:**

```java
try (ByteChannel channel = FileChannel.open(path, StandardOpenOption.READ)) {
  // Read or write bytes
}
```

**48. Get File Store:**

```java
FileStore store = Files.getFileStore(path);
```

**49. Get Usable Space on File Store:**

```java
long usableSpace = store.getUsableSpace();
```

**50. Get Total Space on File Store:**

```java
long totalSpace = store.getTotalSpace();
```
