import java.io.File; import java.io.IOException; import java.nio.file.DirectoryStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Scanner; public class FileAndDirectoryInfo{ public static void main(String[]args)throws IOException { Scanner input = new Scanner(System.in); System.out.println("Enter file or directory name"); Path path = Paths.get(input.nextLine()); if(Files.exists(path)){ // display file (or directory)information System.out.printf("%n%s", path.getFileName()); System.out.printf("Path:%n" , path); System.out.printf("%s,a directory%n", Files.isDirectory(path) , "Is:" +" Is not"); System.out.printf("Last modified: %s%n" , Files.getLastModifiedTime(path)); System.out.printf("Size:%s%n",Files.size(path)); if(Files.isDirectory(path)) { System.out.printf("%nDirectory contents:%n"); DirectoryStream<path> directoryStream = Files.newDirectoryStream(path); for (Path p : directoryStream) System.out.println(p); } } else { System.out.printf("%s does not exist%n" , path); } boolean success = false; System.out.println("Enter path of directory to create"); String dir = input.nextLine(); // Creating new directory in Java,if it doesn't exists File directory = new File(dir); if(directory.exists() && directory.isDirectory()){ System.out.println("Directory already exists ..."); }else{ System.out.println("Directory not exists,creating now"); success= directory.mkdir(); if(success) { System.out.printf("Successfully created new directory:%s%n",dir); }else{ System.out.printf("Failed to create new directory:%s%n",dir ); } } System.out.println("Enter file name to be renamed"); String filename = input.nextLine(); File f = new File(filename); if(f.exists()&& f.isFile()) { System.out.println("Rename File"); success = f.renameTo(f); System.out.println("The name is:%n"); String rename = input.nextLine(); File h = new File(filename); if(h.exists() && f.renameTo(f)) { System.out.println("The folder was renamed"); } if(success) { System.out.printf("Successfully created new name :%s%n", f); }else{ System.out.printf("Failed to created new name :%s%n",f); } } input.close(); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)