Java में NIO और Path
In this page:
import java.nio.file.*;
Path path = Paths.get("file_name");
List<String> lines = Files.readAllLines(path);
What is NIO and Path?
NIO (New I/O) Path interface पुराने File class से ज़्यादा flexibly एक filesystem location represent करता है, और Java में ज़्यादातर modern file operations के लिए Files utility class के साथ जोड़ा जाता है।
उदाहरण: What is NIO and Path?
// Import java.nio.file.Path so it can be used by its short name
import java.nio.file.Path;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
Path path = Path.of("data/notes.txt");
// Print a line to the console
System.out.println(path);
}
}
Login to try C/C++/Java/PHP code in the editor
Extracting Path Information
एक Path object आपको manually separator characters खुद parse किए बिना किसी location के pieces निकालने देता है — getFileName(), getParent(), getRoot()।
उदाहरण: Extracting Path Information
// Import java.nio.file.Path so it can be used by its short name
import java.nio.file.Path;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
Path path = Path.of("data/notes.txt");
// Print a line to the console
System.out.println(path.getFileName());
// Print a line to the console
System.out.println(path.getParent());
}
}
Login to try C/C++/Java/PHP code in the editor
Absolute vs Relative Paths
एक absolute path filesystem root से पूरी तरह एक location specify करता है, जबकि एक relative path current working directory के सापेक्ष interpret होता है — Path.isAbsolute() आपको बताता है आप किस तरह रखे हैं।
उदाहरण: Absolute vs Relative Paths
// Import java.nio.file.Path so it can be used by its short name
import java.nio.file.Path;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
Path relative = Path.of("notes.txt");
Path absolute = Path.of("/home/user/notes.txt");
// Print a line to the console
System.out.println(relative.isAbsolute());
// Print a line to the console
System.out.println(absolute.isAbsolute());
}
}
Login to try C/C++/Java/PHP code in the editor
Resolving Paths
resolve() एक base path को एक relative path segment के साथ combine करके एक नया, लंबा path बनाता है, जो piece by piece file locations बनाने का standard NIO तरीका है।
उदाहरण: Resolving Paths
import java.nio.file.Path;
public class Main {
public static void main(String[] args) {
Path base = Path.of("data");
Path full = base.resolve("notes.txt"); // combines pieces
System.out.println(full);
}
}
Login to try C/C++/Java/PHP code in the editor
Normalizing Paths
normalize() किसी path में redundant . और .. segments को असली filesystem छुए बिना साफ़ करता है, आपको comparison या display के लिए एक canonical, simplified representation देते हुए।
उदाहरण: Normalizing Paths
// Import java.nio.file.Path so it can be used by its short name
import java.nio.file.Path;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
Path messy = Path.of("data/../data/./notes.txt");
// Print a line to the console
System.out.println(messy.normalize());
}
}
Login to try C/C++/Java/PHP code in the editor
java.io.Fileकोjava.nio.file.Pathके साथ confuse करना।"C:\Users"जैसे Windows paths single backslashes के साथ लिखना, जो Java strings में escape sequences माने जाते हैं।- यह उम्मीद करना कि
normalize()जांचेगा कि path मौजूद है, जब यह सिर्फ path text से.और..साफ़ करता है।
- Java I/O streams data पढ़ते और लिखते हैं, और आप files बना और delete कर सकते हैं।
FileReader,FileWriter, buffered readers और writers, औरFileInputStreamऔरFileOutputStreamtext और byte data handle करते हैं।- Serialization objects save करता है, और NIO और
Pathfiles के साथ काम करने का एक modern तरीका प्रदान करते हैं।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: