← Back to Advanced Java Course | Chapter 13: Java NIO | Lesson 2 of 5

Java का Path और Files API

NIO.2 का Path interface file locations को platform-independent तरीके से represent करता है, और Files utility class files पढ़ने, लिखने, और file trees walk करने के लिए सुविधाजनक methods प्रदान करती है।
Syntax
java
Path path = Paths.get("file_name");
Files.exists(path);
Files.readAllLines(path);
Files.write(path, lines);

The Path Interface

Path interface, जो Java 7 में introduced NIO.2 API का हिस्सा है, किसी file या directory location को platform-independent तरीके से represent करता है, और Path.of path segments की एक series से एक बनाने के लिए modern factory method है।

उदाहरण: The Path Interface

java
import java.nio.file.Path;
public class Main {
	public static void main(String[] args) {
		Path path = Path.of("data", "report.txt"); // platform-independent, built from segments
		System.out.println(path);
	}
}

Creating and Resolving Paths

Paths को file system को छुए बिना बनाया और inspect किया जा सकता है: resolve किसी base path को एक relative path से जोड़ता है, जबकि getParent और getFileName किसी मौजूदा path के individual हिस्से निकालते हैं।

उदाहरण: Creating and 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("report.txt"); // joins base with a relative path
		System.out.println(full.getParent() + " " + full.getFileName());
	}
}

Reading Files with Files

Files utility class readString और readAllLines जैसे सुविधाजनक static methods प्रदान करती है जो किसी file के पूरे content को एक single call में एक String या lines की एक List में पढ़ते हैं, बिना streams या readers manually manage किए।

उदाहरण: Reading Files with Files

java
import java.nio.file.*;
public class Main {
	public static void main(String[] args) throws Exception {
		Path file = Path.of("sample.txt");
		Files.writeString(file, "hello world");
		String content = Files.readString(file); // whole file in one call
		System.out.println(content);
		Files.delete(file);
	}
}

Writing Files with Files

Files एक call में file content बनाने या update करने के लिए writeString और दूसरे write methods भी प्रदान करता है, और StandardOpenOption.APPEND जैसे options नियंत्रित करते हैं कि नया content किसी मौजूदा file को overwrite करे या उसके आखिर में जोड़ा जाए।

उदाहरण: Writing Files with Files

java
import java.nio.file.*;
public class Main {
	public static void main(String[] args) throws Exception {
		Path file = Path.of("log.txt");
		Files.writeString(file, "first line\n");
		Files.writeString(file, "second line\n", StandardOpenOption.APPEND); // added, not overwritten
		System.out.println(Files.readString(file));
		Files.delete(file);
	}
}

Walking a Directory Tree

Files.list और Files.walk किसी directory के contents को represent करने वाले Path objects की एक lazy Stream return करते हैं, walk के subdirectories में recurse करने के साथ, standard Stream operations का उपयोग करके पूरे file tree को filter, count, या process करना आसान बनाते हुए।

उदाहरण: Walking a Directory Tree

java
import java.nio.file.*;
import java.util.stream.*;
public class Main {
	public static void main(String[] args) throws Exception {
		Path dir = Files.createTempDirectory("demo");
		Files.createFile(dir.resolve("a.txt"));
		try (Stream<Path> paths = Files.walk(dir)) { // recurses into subdirectories
			paths.forEach(System.out::println);
		}
	}
}
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. Files.walk या Files.list से मिली stream को बिना बंद किए छोड़ना, जिससे directory handles खुले रहते हैं; try-with-resources का उपयोग करें।
  2. Files.writeString(path, text) का उपयोग करना और पुराना content खो देना, क्योंकि default रूप से यह file को replace कर देता है; इसमें जोड़ने के लिए StandardOpenOption.APPEND पास करें।
  3. Path.of("data", "report.txt") के बजाय "data\\report.txt" जैसे separators को hard-code करना, जो दूसरे operating systems पर टूट जाता है।
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

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.