Commit 76b0f29
committed
feat: add File class usage example to explore file and directory properties
WHAT:
- Implemented `FileClassExample` to demonstrate working with the `File` class in Java.
- Checked if a given path is a directory using `isDirectory()`.
- Retrieved and printed all files and subdirectories inside the specified directory using `listFiles()`.
- Displayed both the file name (`getName()`) and its full path (`getPath()`).
WHY:
- The `File` class is the entry point for interacting with the file system in Java.
- It provides metadata about files/directories (existence, permissions, visibility) and allows directory traversal.
- Understanding `File` is essential before using advanced APIs like `NIO` or `Files`.
HOW:
1. Created a `File` object pointing to a directory.
2. Used `isDirectory()` to check if the path is a directory.
3. Called `listFiles()` to get all files and directories inside the path.
4. Iterated over the list and printed details (name and absolute path).
ADDITIONAL CAPABILITIES (commented in code):
- `setReadOnly()` → make a file read-only.
- `setWritable(true)` → allow writing.
- `isFile()` → check if path points to a file.
- `isHidden()` → check if the file is hidden.
- `setLastModified()` → change last modified timestamp.
REAL-WORLD USE CASES:
- Building file explorers or directory tree visualizers.
- Implementing batch file processing (e.g., scanning `.txt` or `.log` files).
- Checking file permissions before performing read/write operations.
- Implementing utilities for cleanup, monitoring, or backup of directories.
- Pre-validation in applications (e.g., ensuring config files exist before startup).
NOTES:
- The `File` class only represents paths and metadata; it does not read/write file contents.
- For actual content operations, streams (`FileInputStream`, `FileOutputStream`) or `java.nio.file.Files` are needed.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 04247ce commit 76b0f29
1 file changed
+34
-8
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | | - | |
8 | | - | |
| 7 | + | |
9 | 8 | | |
10 | 9 | | |
11 | | - | |
12 | 10 | | |
13 | 11 | | |
14 | 12 | | |
| |||
17 | 15 | | |
18 | 16 | | |
19 | 17 | | |
20 | | - | |
21 | 18 | | |
22 | 19 | | |
23 | | - | |
24 | 20 | | |
25 | 21 | | |
26 | 22 | | |
| |||
33 | 29 | | |
34 | 30 | | |
35 | 31 | | |
36 | | - | |
37 | | - | |
| 32 | + | |
| 33 | + | |
38 | 34 | | |
39 | 35 | | |
40 | 36 | | |
41 | 37 | | |
42 | | - | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
0 commit comments