Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Moving files in a Directory

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    19
    Thanks
    2
    Thanked 1 Time in 1 Post

    Wink Moving files in a Directory

    Hi,
    I would like to move all my files in a directory to another directory ...

    I used
    new File("source file path").renameTo(new File("destination file path"));

    but i have a trouble giving the path of source and destination files...(not accepting "/" in the path)

    can some 1 suggest me a better way to do this

    Thanks
    Sai


  2. #2
    Junior Member
    Join Date
    Mar 2010
    Posts
    19
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Moving files in a Directory

    I guess the issue is resolved
    FYI

     
    import java.io.*;
     
    import javax.swing.*;
     
     
     
    public class MovingFile{
     
    public static void main(String[] args) throws IOException{
     
      int a = 0;
     
     BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
      System.out.print("Enter the file or directory name that has to be moved : ");
     
     String src = in.readLine();
     
      if(src.equals("")){
     
       System.out.println("Invalid directory or file name.");
     
        System.exit(0);
     
     }
     
     File source = new File(src);
     
      if(!source.exists()){
     
       System.out.println("File or directory does not exist.");
     
        System.exit(0);
     
     }
     
    System.out.print("Enter the complete path where file or directory has to be moved: ");
     
      String dest = in.readLine();
     
      if(dest.equals("")){
     
        System.out.println("Invalid directory or file name.");
     
          System.exit(0);
     
     }
     
    File destination = new File(dest);
     
     
        if(!destination.exists()){
     
        System.out.print("Mentioned directory does not exist.\nDo you want to create a new directory(Y/N)? ");
     
        String chk = in.readLine();
     
     
        if(chk.equals("Y") || chk.equals("y")){
     
         destination.mkdir();
     
       copyDirectory(source, destination);
     
         a = 1;
     
      }
     
      else if(chk.equals("N") || chk.equals("n")){
     
         System.exit(0);
     
      }
     
    else{
     
        System.out.println("Invalid Entry!");
     
         System.exit(0);
     
      }
     
    }


  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Moving files in a Directory

    Glad you solved your issue Sai,

    You can always try \ instead of /
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. Java program which can list all files in a given directory
    By JavaPF in forum Java Programming Tutorials
    Replies: 8
    Last Post: July 9th, 2013, 03:38 AM
  2. How to read from all files in a directory & plot the contents?
    By 123 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: February 11th, 2010, 12:43 PM
  3. Tikal Eclipse - Moving system time
    By leguin in forum Java IDEs
    Replies: 2
    Last Post: December 3rd, 2009, 01:34 AM
  4. Problem in navigation in a txt file with specific direction
    By wendybird79 in forum Collections and Generics
    Replies: 1
    Last Post: May 10th, 2009, 07:54 AM