Java का Path और Files API
In this page:
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
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);
}
}
Login to try C/C++/Java/PHP code in the editor
Creating and Resolving Paths
Paths को file system को छुए बिना बनाया और inspect किया जा सकता है: resolve किसी base path को एक relative path से जोड़ता है, जबकि getParent और getFileName किसी मौजूदा path के individual हिस्से निकालते हैं।
उदाहरण: Creating and 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("report.txt"); // joins base with a relative path
System.out.println(full.getParent() + " " + full.getFileName());
}
}
Login to try C/C++/Java/PHP code in the editor
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
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);
}
}
Login to try C/C++/Java/PHP code in the editor
Writing Files with Files
Files एक call में file content बनाने या update करने के लिए writeString और दूसरे write methods भी प्रदान करता है, और StandardOpenOption.APPEND जैसे options नियंत्रित करते हैं कि नया content किसी मौजूदा file को overwrite करे या उसके आखिर में जोड़ा जाए।
उदाहरण: Writing Files with Files
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);
}
}
Login to try C/C++/Java/PHP code in the editor
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
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);
}
}
}
Login to try C/C++/Java/PHP code in the editor
Files.walkयाFiles.listसे मिली stream को बिना बंद किए छोड़ना, जिससे directory handles खुले रहते हैं; try-with-resources का उपयोग करें।Files.writeString(path, text)का उपयोग करना और पुराना content खो देना, क्योंकि default रूप से यह file को replace कर देता है; इसमें जोड़ने के लिएStandardOpenOption.APPENDपास करें।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: