Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

IN JAVA.... (Create a directory) Write a program that prompts the user to enter

ID: 664063 • Letter: I

Question

IN JAVA....

(Create a directory) Write a program that prompts the user to enter a directory name and creates a directory using the File’s mkdirs method. The program displays the message “Directory created successfully” if a directory is created or “Directory already exists” if the directory already exists.

(Write/read data) Write a program to create a file named Question2.txt if it does not exist. Write 100 integers created randomly into the file using text I/O. Integers are separated by spaces in the file. Read the data back from the file and display the data in increasing order.

Explanation / Answer


import java.io.*;

public class Directory {
public static void main(String[] args) {
    System.out.println("Creating Directory in Java");

    String dirname = "newDir";
    File dir = new File(dirname);
   if (dir.exists())
   {
   System.out.println("Directory already exists");
   }
   else
   {
    dir.mkdirs();
    System.out.println("Directory created successfully");
   }
  
   File file = new File("Question2.txt");

          if (file.createNewFile()){
            System.out.println("File is created!");
          }else{
            System.out.println("File already exists.");
          }
  
                FileWriter wr = new FileWriter(file);
                for (int i = 1; i<=100; i++)
                {
                    wr.write(i+System.getProperty( "line.separator" ));
                }             
                wr.close();
}
}