Results 1 to 3 of 3

Thread: Move a file from one place to another

Hybrid View

  1. #1
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Sorry. Here is a better version of the code that i previously posted. I was in a rush to get to school. Since the source file is deleted when the JVM exists you will never see it in the source directory but by looking at the code you can see that it is created and moved into the dest directory providing that it does not already exist in the dest directory.
    Code:
     import java.io.*; 
    
     class G{
      public static void main(String[] args){
    
      try{
        File source  = new File("C:" + File.separator + "Hello.txt");
        File dest = new File("C:" + File.separator + "Java" + File.separator + "Hello.txt");
        File destdir = new File("C:" + File.separator + "Java" + File.separator + "Hello.txt"); 
     
        source.createNewFile();
        source.deleteOnExit(); 
      
        if(destdir.exists()){
         System.out.println(" Unable to move file " + " File exists in dest directory ");
         System.exit(0); 
        }
     
        source.renameTo(dest);
     
      }catch(IOException e){System.err.println(e);}
     }
    }

Posting Permissions

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



Click Here to Expand Forum to Full Width