Skip to content

Commit 76b0f29

Browse files
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

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

Section23JavaIOStreams/src/FileClassExample.java

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44
public class FileClassExample {
55
public static void main(String[] args)throws Exception {
66
//Accessing properties of file.
7-
8-
File f=new File("C://Users\\somes\\Downloads\\JAVA SE\\Section23JavaIOStreams\\src\\MyJAVA");
7+
File f = new File("/Users/somesh/Java SE/JavaEvolution-Learning-Growing-Mastering/Section23JavaIOStreams/src/MyJAVA");
98

109
/*
11-
1210
f.setReadOnly();
1311
f.setWritable(true);
1412
FileOutputStream fos = new FileOutputStream("C://Users\\\\somes\\\\Downloads\\\\JAVA SE\\\\Section23JavaIOStreams\\\\src\\\\MyJAVA");
@@ -17,10 +15,8 @@ public static void main(String[] args)throws Exception {
1715
f.isFile();
1816
f.isDirectory();
1917
f.isHidden();
20-
2118
*/
2219

23-
2420
System.out.println(f.isDirectory());
2521

2622
/*
@@ -33,10 +29,40 @@ public static void main(String[] args)throws Exception {
3329
*/
3430

3531
File list[]=f.listFiles();
36-
for(File x:list)
37-
{
32+
33+
for(File x:list) {
3834
System.out.println(x.getName()+" ");
3935
System.out.println(x.getPath());
4036
}
4137
}
42-
}
38+
}
39+
40+
/*
41+
1. File Class:
42+
- java.io.File ek abstract representation hai file ya directory path ka.
43+
- Actual data read/write nahi karta, sirf properties aur metadata handle karta hai.
44+
45+
2. Common Methods:
46+
- f.isFile() → check karta hai ki given path ek file hai ya nahi.
47+
- f.isDirectory() → check karta hai ki given path ek directory hai.
48+
- f.isHidden() → check karta hai ki file hidden hai ya nahi.
49+
- f.setReadOnly(), f.setWritable(true/false) → permissions change karte hain.
50+
- f.setLastModified(time) → last modified timestamp set karta hai.
51+
- f.list() → directory ke andar ke file/directory names string array me return karta hai.
52+
- f.listFiles() → directory ke andar ke files ko File objects ke array me return karta hai.
53+
54+
3. Code Flow:
55+
- File object banaya pointing to directory `"MyJAVA"`.
56+
- `f.isDirectory()` se check kiya directory hai ya nahi.
57+
- `listFiles()` se directory ke andar ki files iterate karke unke `getName()` aur `getPath()` print kiya.
58+
59+
4. Use Cases:
60+
- File explorer ya directory traversal banane ke liye.
61+
- Files ki properties (read-only, hidden, permissions) check/update karne ke liye.
62+
- File search, log management, aur serialization me kaafi common.
63+
64+
65+
✔ File class → metadata aur properties ke liye.
66+
✔ Read/Write actual data ke liye alag streams (FileInputStream, FileOutputStream).
67+
✔ Directory traversal → `list()` ya `listFiles()`.
68+
*/

0 commit comments

Comments
 (0)