← Back to Core Java Course | Chapter 12: File I/O | Lesson 9 of 9

Java में NIO और Path

NIO और Path एक file कहां रहता है यह describe करने का एक modern तरीका देते हैं, एक file के लिए एक address लिखने जैसा। आप इन addresses को join, clean up, और compare कर सकते हैं।
Syntax
java
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?

java
// 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);
	}
}

Extracting Path Information

एक Path object आपको manually separator characters खुद parse किए बिना किसी location के pieces निकालने देता है — getFileName(), getParent(), getRoot()।

उदाहरण: Extracting Path Information

java
// 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());
	}
}

Absolute vs Relative Paths

एक absolute path filesystem root से पूरी तरह एक location specify करता है, जबकि एक relative path current working directory के सापेक्ष interpret होता है — Path.isAbsolute() आपको बताता है आप किस तरह रखे हैं।

उदाहरण: Absolute vs Relative Paths

java
// 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());
	}
}

Resolving Paths

resolve() एक base path को एक relative path segment के साथ combine करके एक नया, लंबा path बनाता है, जो piece by piece file locations बनाने का standard NIO तरीका है।

उदाहरण: Resolving Paths

java
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);
	}
}

Normalizing Paths

normalize() किसी path में redundant . और .. segments को असली filesystem छुए बिना साफ़ करता है, आपको comparison या display के लिए एक canonical, simplified representation देते हुए।

उदाहरण: Normalizing Paths

java
// 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());
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. java.io.File को java.nio.file.Path के साथ confuse करना।
  2. "C:\Users" जैसे Windows paths single backslashes के साथ लिखना, जो Java strings में escape sequences माने जाते हैं।
  3. यह उम्मीद करना कि normalize() जांचेगा कि path मौजूद है, जब यह सिर्फ path text से . और .. साफ़ करता है।
चैप्टर सारांश
  • Java I/O streams data पढ़ते और लिखते हैं, और आप files बना और delete कर सकते हैं।
  • FileReader, FileWriter, buffered readers और writers, और FileInputStream और FileOutputStream text और byte data handle करते हैं।
  • Serialization objects save करता है, और NIO और Path files के साथ काम करने का एक modern तरीका प्रदान करते हैं।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.