If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
पिछले topic में, आपने सीखा कि file कैसे create & write करते हैं है , इस topic में आप सीखेंगे कि किसी file को read कैसे करते हैं।
Java programming language में किसी file को read को करने के लिए Scanner class के hasNextLine() और nextLine() methods का use किया जाता है।
पिछले topic में हमें testfile.txt name से file बनाकर कुछ dummy data write किया था , example में हम उसी file को read करेंगे।
File : ReadFile.java
// import File, FileNotFoundException class from java.io package.
import java.io.File;
import java.io.FileNotFoundException;
// import Scanner class from java.util package.
import java.util.Scanner;
public class ReadFile {
public static void main(String[] args) {
try {
File f = new File("testfile.txt");
Scanner readerObj = new Scanner(f);
while (readerObj.hasNextLine()) {
String data = readerObj.nextLine();
System.out.println(data);
}
// close file.
readerObj.close();
}
catch (FileNotFoundException e) {
System.out.println("An error occurred while reading file.");
e.printStackTrace();
}
}
}
some dummy text to write to the file
ऊपर दिए गए example में file read करने के बाद close() method use किया गया है। Actually में जब भी हम computer से कोई task perform कराते हैं तो उस task को complete करने के लिए कुछ resources assign कर दिए जाते हैं। और task complete होने पर उन्हें release करना भी important है , ताकि उस resource को बाकी task को finish करने में भी use में लिया जा सके। इसलिए किसी भी file को read करने के बाद close() method का use करके file stream को close किया जाता है ताकि system resources को release किया जा सके।
किसी भी file के बारे में information get करने के लिए File class के methods use कर सकते हैं जो आपने पिछले topics में पढ़े थे।
File : FileInfo.java
// import File class from java.io package.
import java.io.File;
public class FileInfo {
public static void main(String[] args) {
File f = new File("testfile.txt");
// check if exists.
if (f.exists()) {
System.out.println("File name : " + f.getName());
System.out.println("Absolute path : " + f.getAbsolutePath());
System.out.println("Writeable : " + f.canWrite());
System.out.println("Readable : " + f.canRead());
System.out.println("File size in bytes : " + f.length());
}
else {
System.out.println("The file does not exist.");
}
}
}
File name : testfile.txt Absolute path : C:\Users\verma\Desktop\ Writeable : true Readable : true File size in bytes : 36
इसके अलावा भी Java में कई classes जैसे FileReader, BufferedReader, Files, Scanner, FileInputStream, FileWriter, BufferedWriter, FileOutputStream का use file handling के लिए किया जाता है। हालाँकि वो depend करता है कि आपको need क्या है और कौन सा version use कर रहे हो।
Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)
Get connected with me. :) LinkedIn Twitter Instagram Facebook