Skip to main content

Java Exception Handling MCQ Test

  Loading…

Creating Files and Directories

File I/O Classes: Creating Files and Directories

1. File Class:

   - Represents a file or directory path.

   - Provides methods for creating new files and directories.


   File file = new File("newFile.txt");

   try {

       if (file.createNewFile()) {

           System.out.println("File created: " + file.getName());

       } else {

           System.out.println("File already exists.");

       }

   } catch (IOException e) {

       System.out.println("An error occurred.");

       e.printStackTrace();

   }



2. mkdir() Method:

   - Creates a directory.



   File directory = new File("newDirectory");

   if (directory.mkdir()) {

       System.out.println("Directory created: " + directory.getName());

   } else {

       System.out.println("Directory already exists.");

   }



3. mkdirs() Method:

   - Creates a directory and its parent directories if they do not exist.


   File directories = new File("newDirectories/childDirectory");

   if (directories.mkdirs()) {

       System.out.println("Directories created: " + directories.getName());

   } else {

       System.out.println("Directories already exist.");

   }



4. Path Class (Java NIO):

   - Provides methods for manipulating file paths.

   - Can create files and directories using Files class.



   Path path = Paths.get("newFile.txt");

   try {

       Files.createFile(path);

       System.out.println("File created: " + path.getFileName());

   } catch (IOException e) {

       System.out.println("An error occurred.");

       e.printStackTrace();

   }



5. Files Class (Java NIO):

   - Provides methods for file operations.

   - Can create directories and files.


   Path directoryPath = Paths.get("newDirectory");

   try {

       Files.createDirectory(directoryPath);

       System.out.println("Directory created: " + directoryPath.getFileName());

   } catch (IOException e) {

       System.out.println("An error occurred.");

       e.printStackTrace();

   }


These examples illustrate different ways to create files and directories in Java using various file I/O classes and methods.

Comments

Popular posts from this blog

The Collection Interface.

  The Collection Interface. 

OracleJDK vs OpenJDK

Oracle JDK (Java Development Kit): Oracle JDK is the official reference implementation of the Java Platform, Standard Edition (Java SE). It included the JRE along with development tools. OpenJDK: An open-source alternative to Oracle JDK, OpenJDK is a community-driven project. It provides a free and open-source implementation of the Java Platform, and many other JDKs, including Oracle JDK, are derived from OpenJDK. Below is a simple table highlighting some key points of comparison between Oracle JDK and OpenJDK: Feature Oracle JDK OpenJDK Vendor Oracle Corporation OpenJDK Community Licensing Commercial (Paid) with Oracle Binary Code License Agreement Open Source (GNU General Public License, version 2, with the Classpath Exception) Support Commercial support available with Oracle Support subscription Community support, may have commercial support options from other vendors Updates and Patches Regular updates with security patches provided by Oracle Updates and patches contributed by the ...