Results 1 to 9 of 9
  1. #1
    Join Date
    Feb 2016
    Posts
    3
    Rep Power
    0

    Default Creating file manager in Java

    Please help!
    "Create an application that functions as file manager . The application must have the following functions:
    review folders ,
    information display files / folders in tabular form ,
    name,
    way
    size,
    creation date ,
    last modification date ,
    creating folders ,
    option for renaming files / folders ,
    option to copy files / folders full
    option for moving files / folders full
    option for deleting files / folders .
    We advise you to use the following commands for these operations, in this order: LIST, INFO , CREATE_DIR ,
    RENAME , COPY , MOVE , DELETE.
    Once you enter a command in the console , the application displays the message about ordering recognized ( or
    unrecognized , if the order is wrong ) and continues to collect information necessary to perform
    All the operations mentioned above"


    This is what I did so far:

    import java.io.File;
    import java.io.IOException;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    import java.nio.file.attribute.BasicFileAttributeView;
    import java.nio.file.attribute.BasicFileAttributes;
    import java.nio.file.attribute.FileTime;
    import java.text.SimpleDateFormat;
    import java.time.Instant;
    import java.time.LocalDateTime;
    import java.time.ZoneId;
    public class FileCreationTime {
    public static FileTime getCreationTime(File file) throws IOException {
    Path p = Paths.get(file.getAbsolutePath());
    BasicFileAttributes view
    = Files.getFileAttributeView(p, BasicFileAttributeView.class)
    .readAttributes();
    FileTime fileTime=view.creationTime();
    // also available view.lastAccessTine and view.lastModifiedTime
    return fileTime;
    }
    public static void main(String[] args) throws IOException {
    File file = new File("C:\\CiprianAssignment.txt");
    System.out.println(file + " creation time :"
    + new SimpleDateFormat("dd/MM/yyyy HH:mm:ss")
    .format(FileCreationTime.getCreationTime(file).toM illis()));

    Instant instantLastmodified = Instant.ofEpochMilli(file.lastModified());
    LocalDateTime dateTimeLastModified = LocalDateTime.ofInstant(instantLastmodified, ZoneId.systemDefault());
    System.out.println("Last acces = " + dateTimeLastModified);
    System.out.println("Absolute path = " + file.getAbsolutePath());
    System.out.println("Name = " + file.getName());
    System.out.println("Lenght = " + file.length() + " Bytes");

    File newDirectory = new File("C:\\Melisa");
    try {
    if(!newDirectory.exists())
    {
    newDirectory.mkdir();
    System.out.println("Created a directory called " + newDirectory.getName());
    }
    else
    {
    System.out.println("Directory called " + newDirectory.getName() + " already exists.");
    }

    } catch (Exception e) {
    System.out.println("Couldn't create a directory called "
    + newDirectory.getName());
    }
    try{

    File afile =new File("C:\\Melisa\\Nature.jpg");

    if(afile.renameTo(new File("C:\\test\\" + afile.getName()))){
    System.out.println("File is moved successful!");
    }else{
    System.out.println("File is failed to move!");
    }

    }catch(Exception e){
    e.printStackTrace();
    }
    File oldfile = new File("C:\\Melisa\\Rahela.txt");
    File newfile = new File("C:\\Melisa\\Ciprian.txt");

    if(!oldfile.exists())
    {
    System.out.println("File doesn't exists!");
    return;
    }
    if(newfile.exists())
    {
    System.out.println("File with desired name already exists!");
    return;
    }
    if (oldfile.renameTo(newfile)) {
    System.out.println("Rename succesful");
    } else {
    System.out.println("Rename failed");
    }





    }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Creating file manager in Java

    Please edit your post and wrap your code with code tags:
    [code]
    **YOUR CODE GOES HERE**
    [/code]
    to get highlighting and preserve formatting.

    Do you have any specific java programming questions about your assignment?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Join Date
    Feb 2016
    Posts
    3
    Rep Power
    0

    Default Re: Creating file manager in Java

    Quote Originally Posted by Norm View Post
    Please edit your post and wrap your code with code tags:
    [code]
    **YOUR CODE GOES HERE**
    [/code]
    to get highlighting and preserve formatting.

    Do you have any specific java programming questions about your assignment?
    .
    This is my progress so far but I have to comply with the assignment mentioned earlier. I know this code is wrong but I need to perfect it according to the Assignment mentioned earlier.

    Java Code:
    import java.io.File;
    import java.io.IOException;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    import java.nio.file.attribute.BasicFileAttributeView;
    import java.nio.file.attribute.BasicFileAttributes;
    import java.nio.file.attribute.FileTime;
    import java.text.SimpleDateFormat;
    import java.time.Instant;
    import java.time.LocalDateTime;
    import java.time.ZoneId;
    public class FileCreationTime {
    public static FileTime getCreationTime(File file) throws IOException {
    Path p = Paths.get(file.getAbsolutePath());
    BasicFileAttributes view
    = Files.getFileAttributeView(p, BasicFileAttributeView.class)
    .readAttributes();
    FileTime fileTime=view.creationTime();
    // also available view.lastAccessTine and view.lastModifiedTime
    return fileTime;
    }
    public static void main(String[] args) throws IOException {
    File file = new File("C:\\CiprianAssignment.txt");
    System.out.println(file + " creation time :"
    + new SimpleDateFormat("dd/MM/yyyy HH:mm:ss")
    .format(FileCreationTime.getCreationTime(file).toM illis()));
    
    Instant instantLastmodified = Instant.ofEpochMilli(file.lastModified());
    LocalDateTime dateTimeLastModified = LocalDateTime.ofInstant(instantLastmodified, ZoneId.systemDefault());
    System.out.println("Last acces = " + dateTimeLastModified);
    System.out.println("Absolute path = " + file.getAbsolutePath());
    System.out.println("Name = " + file.getName());
    System.out.println("Lenght = " + file.length() + " Bytes");
    
    File newDirectory = new File("C:\\Melisa");
    try {
    if(!newDirectory.exists())
    {
    newDirectory.mkdir();
    System.out.println("Created a directory called " + newDirectory.getName());
    }
    else
    {
    System.out.println("Directory called " + newDirectory.getName() + " already exists.");
    }
    
    } catch (Exception e) {
    System.out.println("Couldn't create a directory called "
    + newDirectory.getName());
    }
    try{
    
    File afile =new File("C:\\Melisa\\Nature.jpg");
    
    if(afile.renameTo(new File("C:\\test\\" + afile.getName()))){
    System.out.println("File is moved successful!");
    }else{
    System.out.println("File is failed to move!");
    }
    
    }catch(Exception e){
    e.printStackTrace();
    }
    File oldfile = new File("C:\\Melisa\\Rahela.txt");
    File newfile = new File("C:\\Melisa\\Ciprian.txt");
    
    if(!oldfile.exists())
    {
    System.out.println("File doesn't exists!");
    return;
    }
    if(newfile.exists())
    {
    System.out.println("File with desired name already exists!");
    return;
    }
    if (oldfile.renameTo(newfile)) {
    System.out.println("Rename succesful");
    } else {
    System.out.println("Rename failed");
    }
    
    
    
    
    
    }
    }
    Last edited by Norm; 02-03-2016 at 11:17 PM. Reason: Removed noparse tags

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Creating file manager in Java

    The code has lost all of its indentations making it hard to read and understand. Could you fix that?

    Also you left off any specific questions you have about it.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Creating file manager in Java

    All your code is in one big lump in main().
    You should break it out into individual methods.

    Also, break down the task.

    Normally I would say "start from the top", but in this case I would start with the prompt and user input.
    So create a method that prompts the user for a command and returns something (an int, or preferably an enum if you've done them) that the rest of your code can use to determine what action to perform.

    Once that bit is done and works then (and only then) move onto the actions, one at a time in the order the task suggests, so LIST first.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Creating file manager in Java

    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    Join Date
    Feb 2016
    Posts
    3
    Rep Power
    0

    Default Re: Creating file manager in Java

    Quote Originally Posted by Tolls View Post
    All your code is in one big lump in main().
    You should break it out into individual methods.

    Also, break down the task.

    Normally I would say "start from the top", but in this case I would start with the prompt and user input.
    So create a method that prompts the user for a command and returns something (an int, or preferably an enum if you've done them) that the rest of your code can use to determine what action to perform.

    Once that bit is done and works then (and only then) move onto the actions, one at a time in the order the task suggests, so LIST first.
    Java Code:
    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)){
               
                       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)) // output directory listing
                       {
                               System.out.printf("%nDirectory contents:%n");
                               // object for iterating through a directory's contents
                               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();
       }
    }

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Creating file manager in Java

    You left off any specific questions you have about the code.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Creating file manager in Java

    And you still have the code in one big lump in main().
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Creating Task manager using java
    By Imahan in forum New To Java
    Replies: 4
    Last Post: 03-27-2014, 05:47 PM
  2. Creating a shortcut file from Java
    By MemoNick in forum Advanced Java
    Replies: 3
    Last Post: 10-30-2012, 08:18 PM
  3. Replies: 1
    Last Post: 11-27-2011, 04:25 AM
  4. Creating a file is Holding up java.
    By rizowski in forum New To Java
    Replies: 0
    Last Post: 04-20-2011, 05:47 PM
  5. Question about creating a Game State Manager
    By nolsen01 in forum New To Java
    Replies: 1
    Last Post: 03-05-2010, 10:23 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •