Java File Handling In Hindi

📔 : Java 🔗

File handling , किसी भी application के लिए important होता है , Java में file handle करने के लिए कई सारे methods define किये गए हैं जिनकी help से आप easily files को create / update या delete कर सकते हैं।

Java में file handle करने के लिए java.io package की File class का use किया जाता है। File class वो सभी useful methods provide कराती है जिनकी help से आप easily files को need के according handle कर सकते हैं।

// import File class from java.io package.
import java.io.File;
File myObj = new File("myfile.txt");

Java File class methods

file handling में use किये जाने वाले कुछ methods की list नीचे दी गयी है -

MethodReturn TypeDescription
canRead()BooleanTests whether the file is readable or not.
canWrite()BooleanTests whether the file is writable or not.
createNewFile()BooleanCreates an empty file.
delete()BooleanDeletes a file.
exists()BooleanTests whether the file exists.
getName()StringReturns the name of the file
getAbsolutePath()StringReturns the absolute path name of the file.
length()LongReturns the size of the file in bytes.
list()String[]Returns an array of the files in the directory.
mkdir()BooleanCreates a directory.

Java file handling example

File : FileTest.java

CopyFullscreenClose FullscreenRun
import java.io.File;
public class FileTest {
  public static void main(String[] args) {
    String filename = "demo.txt";
    File fObj = new File(filename);
    
    // check if file exists.
    if(fObj.exists()) {
      System.out.println(filename + " file exists.");
    } else {
      System.out.println(filename + " file does not exists.");
    }
    
    // check if file is readable readable.
    if(fObj.canRead()) {
      System.out.println(filename + " file is not readable.");
    } else {
      System.out.println(filename + " file is not readable.");
    }
    
    // delete file.
    if(fObj.delete()) {
      System.out.println(filename + " file deleted successfully.");
    } else {
      System.out.println(filename + " file could not delete.");
    }
  }       
}
Output
demo.txt file does not exists.
demo.txt file is not readable.
demo.txt file could not delete.

ऊपर दिए गए Example में , कुछ basic methods का use किया गया है , बाकी file को create , read write करना आप next topics में पढ़ेंगे।

Related Topics :

Rahul Kumar

Rahul Kumar

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

b2eprogrammers