Share your knowledge with other learners . . . Login Register

Java File Handling In Hindi

📔 : Java 🔗

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

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

// import File class from package.
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 :

CopyFullscreenClose FullscreenRun
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.");
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 ! My name is Rahul Kumar Rajput. I'm a back end web developer and founder of I live in Uttar Pradesh (UP), India and I love to talk about programming as well as writing technical tutorials and tips that can help to others.

Get connected with me. :) LinkedIn Twitter Instagram Facebook