Click here to Skip to main content
15,896,153 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Java
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();
   }
}
Posted
Updated 21-Jul-15 0:22am
v2
Comments
[no name] 21-Jul-15 7:16am    
Learn to use your debugger. Perhaps
if(h.exists() && f.renameTo(f))
is really supposed to be
if(h.exists() && f.renameTo(rename))

1 solution

you declare the class public where main method is present.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900