Results 1 to 9 of 9

Thread: moving a file

  1. #1
    swati.jyoti is offline Member
    Join Date
    Mar 2009
    Posts
    42
    Rep Power
    0

    Default moving a file

    static void moveSuccessful(File f,File dir)
    {
    if(f.exists())
    {
    boolean success=f.renameTo(new File(dir, f.getName()));
    if (!success) {
    System.out.println(" File was not successfully moved to \"Successful folder\" ");
    }
    }
    else
    {
    System.out.println("File does not exists in the InProgress folder");
    }
    }

    I am trying to move a file MyFile.txt from "InProgress" folder to "Successful " folder.
    Here f is-->H:\file\user\InProgress\MyFile.txt
    and dir is --->H:\file\user\successful
    I dont know y its not moving the file MyFile.txt even if the values in f and dir are correct,I tried checking it out by displaying the values and the values are correct.Its displaying-- File was not successfully moved to "Successful folder"

  2. #2
    dinosoep is offline Senior Member
    Join Date
    Nov 2009
    Posts
    150
    Rep Power
    5

    Default

    boolean success=f.renameTo(new File(dir, f.getName()));
    maybe it has to become

    boolean success=f.renameTo(new File(dir + "/" + f.getName()));
    don't know, there isnt a jdk installed on this comp but i will try at home

  3. #3
    swati.jyoti is offline Member
    Join Date
    Mar 2009
    Posts
    42
    Rep Power
    0

    Default Moving a file

    Hey i want to add to this..Before moving the file to Successful folder ,m doing some validations of the file by opening the file using BufferedReader when the file is in InProgress folder itself

    BufferedReader br=new BufferedReader(new FileReader(H:\\file\\user\\inProgress\\MyFile.txt) )

    then i am trying to move the file to successful folder by opeing the file using boolean success=f.renameTo(new File(dir, f.getName()));
    ..But its not happening..
    But supposing if m not doing the validations and not opening the file using BufferedReader then the file moves to succesful folder..I think there is sum problem with BufferedReader..

    Pls Suggest a solution for this..Thanks

  4. #4
    dinosoep is offline Senior Member
    Join Date
    Nov 2009
    Posts
    150
    Rep Power
    5

    Default

    f.canread()
    if you do that, what happens?

  5. #5
    swati.jyoti is offline Member
    Join Date
    Mar 2009
    Posts
    42
    Rep Power
    0

    Default

    f.canread() doesnt help..
    Moreover it says whtr a file is readable or not..(no context with my problem)

  6. #6
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

    Default

    If you open a file in ms-word, ms-excel etc, and now if you try to do a cut/copy paste, does it work? Probably no. I hope this gives you the answer. You probably need to close the readers before moving the file.

    Let me know if I am going wrong.

  7. #7
    swati.jyoti is offline Member
    Join Date
    Mar 2009
    Posts
    42
    Rep Power
    0

    Thumbs up moving a file

    Hey thanks so much..I have closed everything in finally block and it worked..

    Thanks once again for ur valuable information...:)

  8. #8
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    Yep.
    Check you've closed the reader in a finally block before trying to move the file.

    ETA: Blimey...how long have I had this window open for??
    :)

  9. #9
    Join Date
    Nov 2009
    Posts
    22
    Rep Power
    0

    Default

    you try Scanner class
    :D I'm from vietnam - I hope you help me :D

Similar Threads

  1. Moving Box
    By anilanar in forum New To Java
    Replies: 2
    Last Post: 08-30-2009, 12:29 PM
  2. Need Help with Java 2d - moving train
    By rtm09 in forum New To Java
    Replies: 7
    Last Post: 04-15-2009, 12:28 AM
  3. Moving textboxes
    By GabWit in forum New To Java
    Replies: 2
    Last Post: 01-26-2009, 04:07 PM
  4. [SOLVED] File is Not Moving
    By jazz2k8 in forum New To Java
    Replies: 7
    Last Post: 07-07-2008, 10:58 AM
  5. moving a file
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-10-2007, 07:52 PM

Posting Permissions

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