Path and Files API Java In Hindi

Path and Files API Java In Hindi

जावा प्रोग्रामिंग लैंग्वेज में, NIO.2 ने Java 7 वर्जन में Path और Files APIs जैसे इम्पोर्टेन्ट जावा फीचर्स लांच किए थे। ये एडवांस कांसेप्ट जावा डेवलपर को अपने स्टोरेज सिस्टम में स्टोर क्रिएटेड कस्टम फ़ाइलों और डायरेक्टरी के साथ काम करने का एक मॉडर्न और इजी स्टेप्स या मेथड प्रोवाइड करते हैं।

Path and Files API Java In Hindi

यदि जावा प्रोग्राम में आप इन्हे यूज़ करना चाहते है, तो जावा डेवलपर को मुख्य रूप से निचे दिए गए जावा पैकेज लाइब्रेरी को पहले इसमें अप्लाई या टॉप में ऐड करना होगा।

import java.nio.file.*;

Path API in Java.

जावा फाइल हैंडलिंग कांसेप्ट में Path किसी यूजर डिफाइन क्रिएटेड या स्टोर फ़ाइल या डायरेक्टरी की स्टोरेज लोकेशन को सिस्टम में इंडीकेट करते है।

Creating a File Path in Java.

Path path = Paths.get(“samplefile.txt”);

To create a directory.

Path path = Paths.get(“C:/Users/Employee/Documents”);

Java users can also create their own path using this method.

Path path = Path.of(“samplefile.txt”);

जावा में (Path.of() फंक्शन मेथड को Java 11 वर्जन में ऐड किया गया था।)

Essential Java file directory path methods.

getFileName() File or directory function.

getFileName() फंक्शन जावा प्रोग्राम में फ़ाइल या डायरेक्टरी का नाम प्रोवाइड करता है।

Path path = Paths.get(“documents/samplefile.txt”);

System.out.println(path.getFileName());

Its output.

samplefile.txt

getParent() directory function.

getParent() फंक्शन जावा प्रोग्राम में पैरेंट डायरेक्टरी इनफार्मेशन को प्रोवाइड करता है।

System.out.println(path.getParent());

Its output.

documents

getRoot() file function.

getRoot() फंक्शन जावा प्रोग्राम में फाइल पाथ का एक डिटेल रूट इनफार्मेशन प्रोवाइड करता है।

Path path = Paths.get(“/home/employee/details.txt”);

System.out.println(path.getRoot());

Its output.

/

toAbsolutePath() file directory function.

toAbsolutePath() फंक्शन जावा प्रोग्राम में किसी स्टोरेज फाइल के पाथ को एब्सोल्यूट पाथ में कन्वर्ट करता है।

Path path = Paths.get(“samplefile.txt”);

System.out.println(path.toAbsolutePath());

Combining Java file paths.

resolve() फंक्शन का जावा प्रोग्राम में यूज़ फाइल पाथ को मिलाने में किया जाता है।

Path directory = Paths.get(“documents”);

Path file = directory.resolve(“samplefile.txt”);

System.out.println(file);

Its output.

documents/samplefile.txt

Comparing file paths in Java.

equals() file function.

equals() फंक्शन जावा फाइल प्रोग्राम में चेक करता है कि दो मौजूदा फाइल पाथ बराबर हैं, या नहीं है ।

Example of equals() file function.

import java.nio.file.Path;

import java.nio.file.Paths;

public class PathExplanation {

    public static void main(String[] args) {

        // here we used equals() function method to compare both file

        Path f1 = Paths.get(“samplefile.txt”);

        Path f2 = Paths.get(“samplefile.txt”);

        System.out.println(“equals(): ” + f1.equals(f2));

        // here we Creating a file path

        Path path = Paths.get(“documents/samplefile.txt”);

        // here we used startsWith() function method

        System.out.println(“startsWith(): ” +

                path.startsWith(“documents”));

        // here we endsWith() function method

        System.out.println(“endsWith(): ” +

                path.endsWith(“samplefile.txt”));

    }

}

Its output.

equals(): true

startsWith(): true

endsWith(): true

Files API concept in Java file directory.

जावा फाइल डायरेक्टरी मैनेजमेंट में Files क्लास, फ़ाइलों और डायरेक्टरी पर ऑपरेशन अप्लाई करने के लिए एक स्टैटिक मेथड प्रोवाइड करता है।

Top Files API concept element.

  • Files.createFile(…) – इसका यूज़ फाइल क्रिएट करने में होता है.
  • Files.createDirectory(…) – इसका यूज़ फाइल डायरेक्टरी क्रिएट करने में होता है.
  • Files.delete(…) – इसका यूज़ फाइल डायरेक्टरी को डिलीट करने में होता है.
  • Files.copy(…) – इसका यूज़ कॉपी करने में होता है.
  • Files.move(…) – इसका यूज़ मूव करने में होता है.
  • Files.exists(…) – इसका यूज़ ऑब्जेक्ट मौजूद है, या नहीं है, ये पता करने में होता है.
  • Files.readAllLines(…) – इसका यूज़ फाइल की सभी लाइन को रीड करने में होता है.
  • Files.write(…) – इसका यूज़ फाइल इन्फो को राइट करने में होता है.

Checking whether a file exists in a Java file.

Path path = Paths.get(“samplefile.txt”);

if (Files.exists(path)) {

System.out.println(“Search File exists”);

}

Creating a file in Java.

Path path = Paths.get(“samplefile.txt”);

Files.createFile(path);

यदि सिस्टम में फ़ाइल पहले से मौजूद है, तो ये एक एक्सेप्शन को डिस्प्ले करता है।

Creating a directory in Java.

Path path = Paths.get(“documents”);

Files.createDirectory(path);

Method for creating multiple Java directories.

Path path =

Paths.get(“data/java/files”);

Files.createDirectories(path);

Writing to a file in Java.

Example of Writing to a file in Java.

import java.nio.file.Path;

import java.nio.file.Paths;

import java.nio.file.Files;

import java.io.IOException;

public class FileWriteIllustration {

    public static void main(String[] args) {

        try {

            // here we creating a file path

            Path path = Paths.get(“samplefile.txt”);

            // here we writing data into the file

            Files.write(

                path,

                “this is a sample line”.getBytes()

            );

          System.out.println(“File Data written task successfully done”);

        } catch (IOException ex) {

            System.out.println(“Display Error -: ” + ex.getMessage());

        }

    }

}

Its output.

File Data written task successfully done

Java users can also write a list of lines in the created file.

List<String> lines =

Example of Writing to a list of lines file in Java.

import java.nio.file.Path;

import java.nio.file.Paths;

import java.nio.file.Files;

import java.util.List;

import java.util.Arrays;

import java.io.IOException;

public class WriteMultipleText {

    public static void main(String[] args) {

        try {

            // here we creating file path

            Path path = Paths.get(“samplefile.txt”);

            // here we creating a list of strings

            List<String> text = Arrays.asList(

                “sample java file”,

                “sample java file text”,

                “complete sample java file data”

            );

            // here we writing multiple lines into the samplefile.txt file

            Files.write(path, text);

            System.out.println(“Number of file text Lines written successfully by me”);

        } catch (IOException ex) {

            System.out.println(“Display Error -: ” + ex.getMessage());

        }

    }

}

Its output.

Number of file text Lines written successfully by me

Reading a file in Java.

readAllLines() फंक्शन का यूज़ जावा प्रोग्राम में सभी फाइल लाइन को रीड करने में किया जाता है.

Path path = Paths.get(“samplefile.txt”);

List<String> lines =

Files.readAllLines(path);

for (String line : lines) {

System.out.println(line);

}

बहुत बड़ी जावा प्रोग्राम फ़ाइलों के लिए, स्ट्रीमिंग लाइनें अक्सर बेहतर होती हैं।

Example of Reading a file in Java.

import java.nio.file.Path;

import java.nio.file.Paths;

import java.nio.file.Files;

import java.util.List;

import java.util.stream.Stream;

import java.io.IOException;

public class FileReadIllustration {

    public static void main(String[] args) {

        Path path = Paths.get(“samplefile.txt”);

        try {

            // Method 1: here we using readAllLines() function for all file line reading

            System.out.println(“Here we use readAllLines() method.”);

            List<String> lines =

                    Files.readAllLines(path);

            for (String line : lines) {

                System.out.println(line);

            }

            // Method 2: here we using Files.lines() method

            System.out.println(“\n We use Files.lines() method.”);

            try (Stream<String> fileLines =

                    Files.lines(path)) {

                fileLines.forEach(System.out::println);

            }

        } catch (IOException ex) {

            System.out.println(“Display Error -: ” + ex.getMessage());

        }

    }

}

Its output.

Here we use readAllLines()method.

Display Error -: samplefile.txt

Copying a file in a Java program.

जावा यूजर कॉपी मेथड का यूज़ करके फाइल एलिमेंट को कॉपी कर सकते है.

Path source =

Example of Copying a file in a Java program.

import java.nio.file.Path;

import java.nio.file.Paths;

import java.nio.file.Files;

import java.nio.file.StandardCopyOption;

import java.io.IOException;

public class FileCopyIllustation {

    public static void main(String[] args) {

        try {

            // here is the source file

            Path source = Paths.get(“samplefile.txt”);

            // here is the destination file

            Path destination = Paths.get(“filebackup.txt”);

            // here is the copying the file

            Files.copy(source, destination);

            System.out.println(“File copied process successfully done”);

            // here is the replacing an existing destination file

            Files.copy(

                source,

                destination,

                StandardCopyOption.REPLACE_EXISTING

            );

            System.out.println(“Your desire Existing file replaced task successfully done”);

        } catch (IOException ex) {

            System.out.println(“Display Error -: ” + ex.getMessage());

        }

    }

}

Its output.

Your desire Existing file replaced task successfully done

Moving a File in a Java Program.

Path source =

Example of Moving a File in a Java Program.

import java.nio.file.Path;

import java.nio.file.Paths;

import java.nio.file.Files;

import java.io.IOException;

public class FileMoveIllustation {

    public static void main(String[] args) {

        try {

            // here is the source file

            Path source = Paths.get(“samplefile.txt”);

            // here is the destination file path

            Path destination =

                    Paths.get(“documents/samplefile.txt”);

            // here we Moving the file

            Files.move(source, destination);

            System.out.println(“File moved operation successfully done”);

        } catch (IOException ex) {

            System.out.println(“Display Error -: ” + ex.getMessage());

        }

    }

}

Its output.

File moved operation successfully done

Deleting a file in a Java program.

Example of a file in a Java program.

import java.nio.file.Path;

import java.nio.file.Paths;

import java.nio.file.Files;

import java.io.IOException;

public class FileDeleteIllustration {

    public static void main(String[] args) {

        try {

            // here we creating file path

            Path path = Paths.get(“samplefile.txt”);

            // Method 1: here we used it for Delete a file

            Files.delete(path);

            System.out.println(“Your desire File deleted task successfully done”);

            // Method 2: here we Delete a file only if it exists in system

            boolean output = Files.deleteIfExists(path);

            if (output) {

                System.out.println(“File deletion task successfully done”);

            } else {

                System.out.println(“Your File is does not exist in system”);

            }

        } catch (IOException ex) {

            System.out.println(“Display Error -: ” + ex.getMessage());

        }

    }

}

Its output.

Your desire File deleted task successfully done

Your File is does not exist in system

Checking a File’s Properties in Java.

जावा यूजर यह मौजूदा प्रोगाम में यह चेक कर सकते हैं कि कोई फाइल पाथ फ़ाइल है, या डायरेक्टरी लोकेशन है।

Path path = Paths.get(“samplefile.txt”);

System.out.println(Files.isRegularFile(path));

System.out.println(Files.isDirectory(path));

जावा यूजर इसे अन्य तरीके से भी यूज़ करे.

Files.isReadable(path);

Files.isWritable(path);

Files.isExecutable(path);

Listing directory contents in Java.

जावा प्रोगाम में Files.list() फंक्शन मेथड जावा यूजर को पाथ की एक स्ट्रीम प्रोवाइड करता है।

Path directory =

Paths.get(“documents”);

try (Stream<Path> files =

Files.list(directory)) {

files.forEach(System.out::println);

}

यह मौजूदा प्रोग्राम में डायरेक्टरी के तुरंत के कंटेंट को लिस्ट करता है।

Walking through the Java directory tree.

जावा प्रोग्राम में Files.walk() फ़ंक्शंन मेथड एक डायरेक्टरी और उसकी सब-डायरेक्टरी को पार कर सकता है।

Path directory =

Paths.get(“documents”);

try (Stream<Path> paths =

Files.walk(directory)) {

paths.forEach(System.out::println);

}

यह जावा फंक्शन तब यूज़ किया जाता है, जब जावा यूजर को फ़ाइलों को बार-बार प्रोसेस करने की ज़रूरत होती है।

Detail Compassion Between Directory Path Vs Files Path Location

#Directory Path ConceptFiles Path Concept
1.It Represents a file/directory storage locationit helps java use to performs operations on files/directories path
2.It Represents a specific unique directory pathIt Provides us static utility file path methods
3.We use it with Paths.get() / Path.of() directory functionWe use it with Files.createFile() function or method to create file
4.We used getFileName() function for directoryIn this we used Files.readAllLines() for reading all file lines
5.We used getParent() function to get parent directory detailHere we used Files.write() function to write data and info into file
6.We used resolve() function to group directory and file pathHere we used Files.copy() method to copy file element or data
7.We used toAbsolutePath() function to get absolute directory pathHere we used Files.delete() function to delete unwanted system file

An easy way to remember the path to a file directory in Java.

फाइल डायरेक्टरी पाथ

↓

फ़ाइल की स्टोरेज लोकेशन कहाँ है?

जावा फ़ाइलें

↓

जावा यूजर डिजायर फाइल ऑपरेशन अप्लाई कर सकते है?

Complete Java File Example.

import java.nio.file.*;

import java.util.*;

public class Main {

    public static void main(String[] args)

            throws Exception {

        // here we creating file path

        Path path = Paths.get(“samplefile.txt”);

        // 1. here we write data into the file

        Files.write(

            path,

            Arrays.asList(“Data”, “Html”, “Sample info” , “Welcome to java file concept”)

        );

        System.out.println(“User define custom file data task written successfully done”);

        // 2. here we check if the file exists or not

        System.out.println(“File exists: ” +

                Files.exists(path));

        // 3. here we read all lines from the file

        List<String> lines =

                Files.readAllLines(path);

        // 4. here we display file contents

        System.out.println(“The actual file data contents is-:”);

        lines.forEach(System.out::println);

    }

}

Its output.

User define custom file data task written successfully done

File exists: true

The actual file contents is-:

Data

Html

Sample info

Welcome to java file concept

Conclusion of Path and Files API In Java.

  • जावा फाइल डायरेक्टरी प्रोग्राम में Path किसी फ़ाइल या डायरेक्टरी की स्टोरेज लोकेशन को इंडीकेट करता है।
  • जावा प्रोग्राम Files में, फ़ाइलें क्रिएट करने, फाइल रीड करने, फाइल राइट करने, फाइल कॉपी करने, फाइल मूव करने, फाइल डाटा फाइल डिलीट करने और बाद में उसे इंस्पेक्ट करने के मल्टीप्ल फंक्शन मेथड प्रोवाइड करता है।
  • जावा में Files.list() फंक्शन मेथड का यूज़ डायरेक्टरी कंटेंट को लिस्ट करने के लिए किया जाता है।
  • Files.walk() फंक्शन मेथड का यूज़ डायरेक्टरी ट्री को ट्रैवर्स करने में किया जाता है।
  • जावा फाइल डायरेक्टरी में Path और Files, Java NIO.2 इसके इम्पोर्टेन्ट एलिमेंट  हैं।

Leave a Reply